/
sdk.rb
93 lines (82 loc) 路 2.84 KB
/
sdk.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# frozen_string_literal: true
# Scaffolding
require "spotify/sdk/base"
require "spotify/sdk/model"
# Components
require "spotify/sdk/connect"
require "spotify/sdk/me"
# Models
require "spotify/sdk/connect/device"
require "spotify/sdk/connect/playback_state"
require "spotify/sdk/me/info"
require "spotify/sdk/artist"
require "spotify/sdk/album"
require "spotify/sdk/image"
require "spotify/sdk/item"
module Spotify
##
# Spotify::SDK contains the complete Ruby DSL to interact with the Spotify Platform.
#
class SDK
##
# Initialize the Spotify SDK object.
#
# @example
# # Example 1: Load it in from an access token value.
# @sdk = Spotify::SDK.new("access_token_here")
#
# # Example 2: Load it in with values from your database.
# @sdk = Spotify::SDK.new({
# access_token: "access_token_here",
# expires_in: 3_000_000,
# refresh_token: "refresh_token_here"
# })
#
# # Example 4: Load it in from an OAuth2::AccessToken object.
# @sdk = Spotify::SDK.new(@auth.auth_code.get_token("auth code"))
#
# # Example 5: Load it from a query string or a fully qualified URL.
# @sdk = Spotify::SDK.new("https://localhost:8080/#token=...&expires_in=...")
# @sdk = Spotify::SDK.new("token=...&expires_in=...")
#
# @param [String,Hash,OAuth2::AccessToken] session Any supported object containing an access token.
#
def initialize(session)
raise "Invalid Spotify::Accounts::Session object" unless session.instance_of?(Spotify::Accounts::Session)
@session = session
mount_sdk_components
end
attr_reader :session
def inspect # :nodoc:
"#<%s:0x00%x>" % [self.class.name, (object_id << 1)]
end
##
# This is where we mount new SDK components to the Spotify::SDK object.
# Simply add a key (this is your identifier) with the value being the object.
#
# Notes:
# - Make sure your SDK component is being loaded at the top of this page.
# - You can name your identifier whatever you want:
# - This will be what people will use to call your code
# - For example: it would be the `connect` in `Spotify::SDK.new(@session).connect`
# - We'll call .new on your class, providing one parameter being the instance of this SDK (aka self).
# - Make sure to a test for it in spec/lib/spotify/sdk_spec.rb (see how we did it for others)
#
SDK_COMPONENTS = {
connect: Spotify::SDK::Connect,
me: Spotify::SDK::Me
}.freeze
SDK_COMPONENTS.each_key do |component|
attr_reader(component)
end
private
##
# This is where we map the SDK component classes to the SDK component vairables.
#
def mount_sdk_components # :nodoc:
SDK_COMPONENTS.map do |key, klass|
instance_variable_set "@#{key}".to_sym, klass.new(self)
end
end
end
end