playlyfe 0.4.5 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/playlyfe.rb +61 -59
  2. metadata +1 -1
data/lib/playlyfe.rb CHANGED
@@ -20,17 +20,6 @@ end
20
20
  class Playlyfe
21
21
  @@api = 'https://api.playlyfe.com/v1'
22
22
 
23
- # You can initiate a client by giving the client_id and client_secret params
24
- # This will authorize a client and get the token
25
- #
26
- # @param [Hash] options the options to make the request with
27
- # @param options
28
- # [Boolean] :type where where type can be 'code', 'client' for
29
- # for the oauth2 auth code flow and client credentials flow
30
- # [lambda] :store a method that persists the access_token into
31
- # a database
32
- # [lambda] :retrieve a method that is used by the sdk internally
33
- # to read the access_token and use it in all your requests
34
23
  def self.init(options = {})
35
24
  puts 'Playlyfe Initializing...............................................'
36
25
  if options[:type].nil?
@@ -44,6 +33,7 @@ class Playlyfe
44
33
  @@secret = options[:client_secret]
45
34
  @@store = options[:store]
46
35
  @@retrieve = options[:retrieve]
36
+ @@redirect_uri = options[:redirect_uri]
47
37
  if @@store.nil?
48
38
  @@store = lambda { |token| puts 'Storing Token' }
49
39
  end
@@ -55,30 +45,36 @@ class Playlyfe
55
45
  err.name = 'init_failed'
56
46
  err.message = 'You must pass in a redirect_uri for the auth code flow'
57
47
  raise err
58
- else
59
- puts 'CLIENT'
60
- #RestClient.get("https://playlyfe.com/auth?redirect_uri=#{options[:redirect_uri]}&response_type=code&client_id=#{@@id}")
61
- #:authorize_url =>
62
- #'response_type' => 'code', 'client_id' => @@id
63
- #auth_url = @@client.auth_code.authorize_url(:redirect_uri => 'http://localhost:8080/oauth2/callback')
64
- #@@client.auth_code.get_token('code_value', :redirect_uri => 'http://localhost:8080/oauth2/callback') #check query.code then make post request
65
48
  end
66
49
  end
67
- #RestClient.log = Logger.new(STDOUT)
68
50
  end
69
51
 
70
52
  def self.get_access_token
71
53
  puts 'Getting Access Token'
72
54
  begin
73
- access_token = RestClient.post('https://playlyfe.com/auth/token',
74
- {
75
- :client_id => @@id,
76
- :client_secret => @@secret,
77
- :grant_type => 'client_credentials'
78
- }.to_json,
79
- :content_type => :json,
80
- :accept => :json
81
- )
55
+ if @@type == 'client'
56
+ access_token = RestClient.post('https://playlyfe.com/auth/token',
57
+ {
58
+ :client_id => @@id,
59
+ :client_secret => @@secret,
60
+ :grant_type => 'client_credentials'
61
+ }.to_json,
62
+ :content_type => :json,
63
+ :accept => :json
64
+ )
65
+ else
66
+ access_token = RestClient.post("https://playlyfe.com/auth/token",
67
+ {
68
+ :client_id => @@id,
69
+ :client_secret => @@secret,
70
+ :grant_type => 'authorization_code',
71
+ :code => @@code,
72
+ :redirect_uri => @@redirect_uri
73
+ }.to_json,
74
+ :content_type => :json,
75
+ :accept => :json
76
+ )
77
+ end
82
78
  access_token = JSON.parse(access_token)
83
79
  expires_at ||= Time.now.to_i + access_token['expires_in']
84
80
  access_token.delete('expires_in')
@@ -86,41 +82,56 @@ class Playlyfe
86
82
  @@store.call access_token
87
83
  if @@retrieve.nil?
88
84
  @@retrieve = lambda { return access_token }
85
+ else
86
+ old_token = @@retrieve.call
87
+ if access_token != old_token
88
+ @@retrieve = lambda { return access_token }
89
+ end
89
90
  end
90
91
  rescue => e
91
92
  raise PlaylyfeError.new(e.response)
92
93
  end
93
94
  end
94
95
 
95
- def self.check_expired(access_token)
96
- if access_token['expires_at'] < Time.now.to_i
97
- puts 'Access Token Expired'
96
+ def self.get_auth_url
97
+ query = { response_type: 'code', redirect_uri: @@redirect_uri, client_id: @@id }
98
+ "https://playlyfe.com/auth?#{self.hash_to_query(query)}"
99
+ end
100
+
101
+ def self.exchange_code(code)
102
+ if code.nil?
103
+ err = PlaylyfeError.new("")
104
+ err.name = 'init_failed'
105
+ err.message = 'You must pass in a code in exchange_code for the auth code flow'
106
+ raise err
107
+ else
108
+ @@code = code
98
109
  self.get_access_token()
99
110
  end
100
111
  end
101
112
 
102
- # def refresh!(params = {})
103
- # params.merge!(:client_id => @client.id,
104
- # :client_secret => @client.secret,
105
- # :grant_type => 'refresh_token',
106
- # :refresh_token => refresh_token)
107
- # new_token = @client.get_token(params)
108
- # new_token.options = options
109
- # new_token.refresh_token = refresh_token unless new_token.refresh_token
110
- # new_token
111
- # end
112
-
113
- def self.login
113
+ def self.check_token(options)
114
+ puts 'Checking Token'
115
+ if @@retrieve.nil?
116
+ err = PlaylyfeError.new("")
117
+ err.name = 'api_request_failed'
118
+ err.message = 'You must pass in a code in exchange_code for the auth code flow'
119
+ raise err
120
+ end
121
+ access_token = @@retrieve.call
122
+ if access_token['expires_at'] < Time.now.to_i
123
+ puts 'Access Token Expired'
124
+ self.get_access_token()
125
+ access_token = @@retrieve.call
126
+ end
127
+ options[:query][:access_token] = access_token['access_token']
114
128
  end
115
129
 
116
130
  def self.get(options = {})
117
131
  options[:route] ||= ''
118
132
  options[:query] ||= {}
119
133
  options[:raw] ||= false
120
-
121
- access_token = @@retrieve.call
122
- self.check_expired(access_token)
123
- options[:query][:access_token] = access_token['access_token']
134
+ self.check_token(options)
124
135
 
125
136
  begin
126
137
  res = RestClient.get("#{@@api}#{options[:route]}",
@@ -140,10 +151,7 @@ class Playlyfe
140
151
  options[:route] ||= ''
141
152
  options[:query] ||= {}
142
153
  options[:body] ||= {}
143
-
144
- access_token = @@retrieve.call
145
- self.check_expired(access_token)
146
- options[:query][:access_token] = access_token['access_token']
154
+ self.check_token(options)
147
155
 
148
156
  begin
149
157
  res = RestClient.post("#{@@api}#{options[:route]}?#{self.hash_to_query(options[:query])}",
@@ -161,10 +169,7 @@ class Playlyfe
161
169
  options[:route] ||= ''
162
170
  options[:query] ||= {}
163
171
  options[:body] ||= {}
164
-
165
- access_token = @@retrieve.call
166
- self.check_expired(access_token)
167
- options[:query][:access_token] = access_token['access_token']
172
+ self.check_token(options)
168
173
 
169
174
  begin
170
175
  res = RestClient.patch("#{@@api}#{options[:route]}?#{self.hash_to_query(options[:query])}",
@@ -181,10 +186,7 @@ class Playlyfe
181
186
  def self.delete(options = {})
182
187
  options[:route] ||= ''
183
188
  options[:query] ||= {}
184
-
185
- access_token = @@retrieve.call
186
- self.check_expired(access_token)
187
- options[:query][:access_token] = access_token['access_token']
189
+ self.check_token(options)
188
190
 
189
191
  begin
190
192
  res = RestClient.delete("#{@@api}#{options[:route]}",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: playlyfe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.5
4
+ version: 0.5.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: