Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit e47d787

Browse files
committed
added docs about creating a component
1 parent 61e586a commit e47d787

File tree

1 file changed

+114
-2
lines changed

1 file changed

+114
-2
lines changed

docs/documentation/contributing/index.md

Lines changed: 114 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ There's multiple ways you can contribute, and we'll cover all of them:
5858
- [Configuration](#configuration)
5959
- [SDK Folder Structure](#sdk-folder-structure)
6060
- [Adding a Component](#adding-a-component)
61-
- [Adding a Method](#adding-a-component)
6261
- [Adding a Model](#adding-a-model)
6362
- [Testing](#testing)
6463
- [Documentation](#documentation)
@@ -213,7 +212,119 @@ $ ls -l spec/**/*_spec.rb
213212

214213
### Adding a Component
215214

216-
### Adding a Method
215+
In the `bin/` folder, we have provided a Rails-like generator that will generate the relevant files for you to add a new component:
216+
217+
#### Generate
218+
219+
For example, if you'd like to generate a component called `Friends` run the following command:
220+
221+
```sh
222+
$ bin/generate_component friends
223+
```
224+
225+
It will then generate the following files for you:
226+
227+
```sh
228+
$ cat lib/spotify/sdk/friends.rb
229+
$ cat spec/lib/spotify/sdk/friends_spec.rb
230+
```
231+
232+
#### Mounting
233+
234+
Then you'll need to do two manual steps in `lib/spotify/sdk.rb`:
235+
236+
- Include the component at the top:
237+
238+
```ruby
239+
# Components
240+
require "spotify/sdk/friends"
241+
```
242+
243+
- Then mount the component as `friends` in `Spotify::SDK::SDK_COMPONENTS`:
244+
```ruby
245+
SDK_COMPONENTS = {
246+
...,
247+
friends: Spotify::SDK::Friends
248+
}.freeze
249+
```
250+
251+
That's it! We have setup a component. You can go ahead and write some fun logic! 🙂
252+
253+
#### Writing Logic
254+
255+
In our component, we can create a method called `hi`:
256+
257+
```ruby
258+
# frozen_string_literal: true
259+
260+
module Spotify
261+
class SDK
262+
class Friend < Base
263+
##
264+
# Hello world!
265+
#
266+
# @see https://bih.github.io/spotify-ruby/documentation/contributing/
267+
#
268+
# @param [Class] param_name Description
269+
# @return [String] text Description
270+
#
271+
def hi
272+
"Hello world!"
273+
end
274+
end
275+
end
276+
end
277+
```
278+
279+
The comments above are used by [YARD] to generate our [SDK Reference] on <https://rubydoc.info>.
280+
281+
#### Debugging Logic
282+
283+
As we've mounted our component as `friends` already, we can use the following code to test it in our console by running `bin/console`:
284+
285+
```
286+
$ bin/console
287+
[1] pry(main)> @sdk = Spotify::SDK.new(@session)
288+
[2] pry(main)> @sdk.friends
289+
=> #<Spotify::SDK::Friends:0x007fd7d31784e8>
290+
[3] pry(main)> @sdk.friends.hi
291+
=> "Hello world"
292+
```
293+
294+
#### Testing Logic
295+
296+
In our generated `spec/lib/spotify/sdk/friends_spec.rb` file, we can write some [RSpec] tests:
297+
298+
```ruby
299+
# frozen_string_literal: true
300+
301+
require "spec_helper"
302+
303+
RSpec.describe Spotify::SDK::Friends do
304+
let(:session) { build(:session, access_token: "access_token") }
305+
subject { Spotify::SDK.new(session).friends }
306+
307+
describe "#hi" do
308+
it "returns 'Hello world'" do
309+
expect(subject.hi).to eq "Hello world"
310+
end
311+
end
312+
end
313+
```
314+
315+
And then you can execute tests by running `rake ci` in the root directory.
316+
317+
#### Sample Implementation
318+
319+
For an example of a good implementation, see the following files for `Spotify::SDK::Me` component:
320+
321+
- Implementation: [lib/spotify/sdk/me.rb]
322+
- RSpec Tests: [spec/lib/spotify/sdk/me_spec.rb]
323+
- Fixture: [spec/support/fixtures/get/v1/me/object.json]
324+
325+
[lib/spotify/sdk/me.rb]: https://github.com/bih/spotify-ruby/blob/master/lib/spotify/sdk/me.rb
326+
[spec/lib/spotify/sdk/me_spec.rb]: https://github.com/bih/spotify-ruby/blob/master/spec/lib/spotify/sdk/me_spec.rb
327+
[spec/support/fixtures/get/v1/me/object.json]: https://github.com/bih/spotify-ruby/blob/master/spec/support/fixtures/get/v1/me/object.json
217328

218329
### Adding a Model
219330

@@ -262,6 +373,7 @@ That's all - we'll use your public GitHub avatar and give you some 💖!
262373
[fork]: https://github.com/bih/spotify-ruby/fork
263374
[semantic versioning]: https://semver.org
264375
[sass]: https://sass-lang.com/
376+
[sdk reference]: https://www.rubydoc.info/github/bih/spotify-ruby/master
265377
[yard]: https://yardoc.org
266378
[factorybot]: https://github.com/thoughtbot/factory_bot
267379
[rspec]: http://rspec.info

0 commit comments

Comments
 (0)