playlyfe 0.5.5 → 0.6.0

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 (3) hide show
  1. data/lib/playlyfe.rb +79 -96
  2. data/test/test.rb +34 -37
  3. metadata +2 -2
data/lib/playlyfe.rb CHANGED
@@ -20,8 +20,7 @@ end
20
20
  class Playlyfe
21
21
  @@api = 'https://api.playlyfe.com/v1'
22
22
 
23
- def self.init(options = {})
24
- puts 'Playlyfe Initializing...............................................'
23
+ def initialize(options = {})
25
24
  if options[:type].nil?
26
25
  err = PlaylyfeError.new("")
27
26
  err.name = 'init_failed'
@@ -32,13 +31,13 @@ class Playlyfe
32
31
  @@id = options[:client_id]
33
32
  @@secret = options[:client_secret]
34
33
  @@store = options[:store]
35
- @@retrieve = options[:retrieve]
34
+ @@load = options[:load]
36
35
  @@redirect_uri = options[:redirect_uri]
37
36
  if @@store.nil?
38
37
  @@store = lambda { |token| puts 'Storing Token' }
39
38
  end
40
39
  if @@type == 'client'
41
- self.get_access_token()
40
+ get_access_token()
42
41
  else
43
42
  if options[:redirect_uri].nil?
44
43
  err = PlaylyfeError.new("")
@@ -49,8 +48,7 @@ class Playlyfe
49
48
  end
50
49
  end
51
50
 
52
- def self.get_access_token
53
- puts 'Getting Access Token'
51
+ def get_access_token
54
52
  begin
55
53
  if @@type == 'client'
56
54
  access_token = RestClient.post('https://playlyfe.com/auth/token',
@@ -80,12 +78,12 @@ class Playlyfe
80
78
  access_token.delete('expires_in')
81
79
  access_token['expires_at'] = expires_at
82
80
  @@store.call access_token
83
- if @@retrieve.nil?
84
- @@retrieve = lambda { return access_token }
81
+ if @@load.nil?
82
+ @@load = lambda { return access_token }
85
83
  else
86
- old_token = @@retrieve.call
84
+ old_token = @@load.call
87
85
  if access_token != old_token
88
- @@retrieve = lambda { return access_token }
86
+ @@load = lambda { return access_token }
89
87
  end
90
88
  end
91
89
  rescue => e
@@ -93,43 +91,51 @@ class Playlyfe
93
91
  end
94
92
  end
95
93
 
96
- def self.get_login_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
109
- self.get_access_token()
110
- end
111
- end
112
-
113
- def self.check_token(options)
114
- access_token = @@retrieve.call
94
+ def check_token(options)
95
+ access_token = @@load.call
115
96
  if access_token['expires_at'] < Time.now.to_i
116
97
  puts 'Access Token Expired'
117
- self.get_access_token()
118
- access_token = @@retrieve.call
98
+ get_access_token()
99
+ access_token = @@load.call
119
100
  end
120
101
  options[:query][:access_token] = access_token['access_token']
121
102
  end
122
103
 
123
- def self.get(options = {})
104
+ def api(options = {})
124
105
  options[:route] ||= ''
125
106
  options[:query] ||= {}
107
+ options[:body] ||= {}
126
108
  options[:raw] ||= false
127
- self.check_token(options)
128
-
109
+ check_token(options)
129
110
  begin
130
- res = RestClient.get("#{@@api}#{options[:route]}",
131
- {:params => options[:query] }
132
- )
111
+ case options[:method]
112
+ when 'GET'
113
+ res = RestClient.get("#{@@api}#{options[:route]}",
114
+ {:params => options[:query] }
115
+ )
116
+ when 'POST'
117
+ res = RestClient.post("#{@@api}#{options[:route]}?#{hash_to_query(options[:query])}",
118
+ options[:body].to_json,
119
+ :content_type => :json,
120
+ :accept => :json
121
+ )
122
+ when 'PUT'
123
+ res = RestClient.put("#{@@api}#{options[:route]}?#{hash_to_query(options[:query])}",
124
+ options[:body].to_json,
125
+ :content_type => :json,
126
+ :accept => :json
127
+ )
128
+ when 'PATCH'
129
+ res = RestClient.patch("#{@@api}#{options[:route]}?#{hash_to_query(options[:query])}",
130
+ options[:body].to_json,
131
+ :content_type => :json,
132
+ :accept => :json
133
+ )
134
+ when 'DELETE'
135
+ res = RestClient.delete("#{@@api}#{options[:route]}",
136
+ {:params => options[:query] }
137
+ )
138
+ end
133
139
  if options[:raw] == true
134
140
  return res.body
135
141
  else
@@ -140,76 +146,53 @@ class Playlyfe
140
146
  end
141
147
  end
142
148
 
143
- def self.post(options = {})
144
- options[:route] ||= ''
145
- options[:query] ||= {}
146
- options[:body] ||= {}
147
- self.check_token(options)
149
+ def get(options = {})
150
+ options[:method] = "GET"
151
+ api(options)
152
+ end
148
153
 
149
- begin
150
- res = RestClient.post("#{@@api}#{options[:route]}?#{self.hash_to_query(options[:query])}",
151
- options[:body].to_json,
152
- :content_type => :json,
153
- :accept => :json
154
- )
155
- return JSON.parse(res.body)
156
- rescue => e
157
- raise PlaylyfeError.new(e.response)
158
- end
154
+ def post(options = {})
155
+ options[:method] = "POST"
156
+ api(options)
159
157
  end
160
158
 
161
- def self.put(options = {})
162
- options[:route] ||= ''
163
- options[:query] ||= {}
164
- options[:body] ||= {}
165
- self.check_token(options)
159
+ def put(options = {})
160
+ options[:method] = "PUT"
161
+ api(options)
162
+ end
166
163
 
167
- begin
168
- res = RestClient.put("#{@@api}#{options[:route]}?#{self.hash_to_query(options[:query])}",
169
- options[:body].to_json,
170
- :content_type => :json,
171
- :accept => :json
172
- )
173
- return JSON.parse(res.body)
174
- rescue => e
175
- raise PlaylyfeError.new(e.response)
176
- end
164
+ def patch(options = {})
165
+ options[:method] = "PATCH"
166
+ api(options)
177
167
  end
178
168
 
179
- def self.patch(options = {})
180
- options[:route] ||= ''
181
- options[:query] ||= {}
182
- options[:body] ||= {}
183
- self.check_token(options)
169
+ def delete(options = {})
170
+ options[:method] = "DELETE"
171
+ api(options)
172
+ end
184
173
 
185
- begin
186
- res = RestClient.patch("#{@@api}#{options[:route]}?#{self.hash_to_query(options[:query])}",
187
- options[:body].to_json,
188
- :content_type => :json,
189
- :accept => :json
190
- )
191
- return JSON.parse(res.body)
192
- rescue => e
193
- raise PlaylyfeError.new(e.response)
194
- end
174
+ def hash_to_query(hash)
175
+ return URI.encode(hash.map{|k,v| "#{k}=#{v}"}.join("&"))
195
176
  end
196
177
 
197
- def self.delete(options = {})
198
- options[:route] ||= ''
199
- options[:query] ||= {}
200
- self.check_token(options)
178
+ def get_login_url
179
+ query = { response_type: 'code', redirect_uri: @@redirect_uri, client_id: @@id }
180
+ "https://playlyfe.com/auth?#{hash_to_query(query)}"
181
+ end
201
182
 
202
- begin
203
- res = RestClient.delete("#{@@api}#{options[:route]}",
204
- {:params => options[:query] }
205
- )
206
- JSON.parse(res.body)
207
- rescue => e
208
- raise PlaylyfeError.new(e.response)
209
- end
183
+ def get_logout_url
184
+ ""
210
185
  end
211
186
 
212
- def self.hash_to_query(hash)
213
- return URI.encode(hash.map{|k,v| "#{k}=#{v}"}.join("&"))
187
+ def exchange_code(code)
188
+ if code.nil?
189
+ err = PlaylyfeError.new("")
190
+ err.name = 'init_failed'
191
+ err.message = 'You must pass in a code in exchange_code for the auth code flow'
192
+ raise err
193
+ else
194
+ @@code = code
195
+ get_access_token()
196
+ end
214
197
  end
215
198
  end
data/test/test.rb CHANGED
@@ -6,7 +6,7 @@ class PlaylyfeTest < Test::Unit::TestCase
6
6
 
7
7
  def test_invalid_client
8
8
  begin
9
- Playlyfe.init(
9
+ Playlyfe.new(
10
10
  client_id: "wrong_id",
11
11
  client_secret: "wrong_secret",
12
12
  type: 'client'
@@ -19,7 +19,7 @@ class PlaylyfeTest < Test::Unit::TestCase
19
19
 
20
20
  def test_wrong_init
21
21
  begin
22
- Playlyfe.init(
22
+ Playlyfe.new(
23
23
  client_id: "Zjc0MWU0N2MtODkzNS00ZWNmLWEwNmYtY2M1MGMxNGQ1YmQ4",
24
24
  client_secret: "YzllYTE5NDQtNDMwMC00YTdkLWFiM2MtNTg0Y2ZkOThjYTZkMGIyNWVlNDAtNGJiMC0xMWU0LWI2NGEtYjlmMmFkYTdjOTI3"
25
25
  )
@@ -28,56 +28,54 @@ class PlaylyfeTest < Test::Unit::TestCase
28
28
  end
29
29
  end
30
30
 
31
- def test_wrong_api
31
+ def test_init_staging
32
+ pl = Playlyfe.new(
33
+ client_id: "Zjc0MWU0N2MtODkzNS00ZWNmLWEwNmYtY2M1MGMxNGQ1YmQ4",
34
+ client_secret: "YzllYTE5NDQtNDMwMC00YTdkLWFiM2MtNTg0Y2ZkOThjYTZkMGIyNWVlNDAtNGJiMC0xMWU0LWI2NGEtYjlmMmFkYTdjOTI3",
35
+ type: 'client'
36
+ )
37
+
32
38
  begin
33
- Playlyfe.init(
34
- client_id: "Zjc0MWU0N2MtODkzNS00ZWNmLWEwNmYtY2M1MGMxNGQ1YmQ4",
35
- client_secret: "YzllYTE5NDQtNDMwMC00YTdkLWFiM2MtNTg0Y2ZkOThjYTZkMGIyNWVlNDAtNGJiMC0xMWU0LWI2NGEtYjlmMmFkYTdjOTI3",
36
- type: 'client'
37
- )
38
- Playlyfe.get(route: '/gege', query: { player_id: 'student1' })
39
+ pl.get(route: '/gege', query: { player_id: 'student1' })
39
40
  rescue PlaylyfeError => e
40
41
  assert_equal e.name,'route_not_found'
41
42
  assert_equal e.message, 'This route does not exist'
42
43
  end
43
- end
44
44
 
45
- def test_init_staging
46
- Playlyfe.init(
47
- client_id: "Zjc0MWU0N2MtODkzNS00ZWNmLWEwNmYtY2M1MGMxNGQ1YmQ4",
48
- client_secret: "YzllYTE5NDQtNDMwMC00YTdkLWFiM2MtNTg0Y2ZkOThjYTZkMGIyNWVlNDAtNGJiMC0xMWU0LWI2NGEtYjlmMmFkYTdjOTI3",
49
- type: 'client'
50
- )
51
- players = Playlyfe.get(route: '/players', query: { player_id: 'student1', limit: 1 })
45
+ players = pl.api(method: 'GET', route: '/players', query: { player_id: 'student1', limit: 1 })
46
+ assert_not_nil players["data"]
47
+ assert_not_nil players["data"][0]
48
+
49
+ players = pl.get(route: '/players', query: { player_id: 'student1', limit: 1 })
52
50
  assert_not_nil players["data"]
53
51
  assert_not_nil players["data"][0]
54
52
 
55
53
  begin
56
- assert_nil Playlyfe.get(route: '/player')
54
+ pl.get(route: '/player')
57
55
  rescue PlaylyfeError => e
58
56
  assert_equal e.message, "The 'player_id' parameter should be specified in the query"
59
57
  end
60
58
 
61
59
  player_id = 'student1'
62
- player = Playlyfe.get(route: '/player', query: { player_id: player_id } )
60
+ player = pl.get(route: '/player', query: { player_id: player_id } )
63
61
  assert_equal player["id"], "student1"
64
62
  assert_equal player["alias"], "Student1"
65
63
  assert_equal player["enabled"], true
66
64
 
67
- Playlyfe.get(route: '/definitions/processes', query: { player_id: player_id } )
68
- Playlyfe.get(route:'/definitions/teams', query: { player_id: player_id } )
69
- Playlyfe.get(route: '/processes', query: { player_id: player_id } )
70
- Playlyfe.get(route: '/teams', query: { player_id: player_id } )
65
+ pl.get(route: '/definitions/processes', query: { player_id: player_id } )
66
+ pl.get(route:'/definitions/teams', query: { player_id: player_id } )
67
+ pl.get(route: '/processes', query: { player_id: player_id } )
68
+ pl.get(route: '/teams', query: { player_id: player_id } )
71
69
 
72
- processes = Playlyfe.get(route: '/processes', query: { player_id: 'student1', limit: 1, skip: 4 })
70
+ processes = pl.get(route: '/processes', query: { player_id: 'student1', limit: 1, skip: 4 })
73
71
  assert_equal processes["data"][0]["definition"], "module1"
74
72
  assert_equal processes["data"].size, 1
75
73
 
76
- new_process = Playlyfe.post(route: '/definitions/processes/module1', query: { player_id: player_id })
74
+ new_process = pl.post(route: '/definitions/processes/module1', query: { player_id: player_id })
77
75
  assert_equal new_process["definition"], "module1"
78
76
  assert_equal new_process["state"], "ACTIVE"
79
77
 
80
- patched_process = Playlyfe.patch(
78
+ patched_process = pl.patch(
81
79
  route: "/processes/#{new_process['id']}",
82
80
  query: { player_id: player_id },
83
81
  body: { name: 'patched_process', access: 'PUBLIC' }
@@ -86,43 +84,42 @@ class PlaylyfeTest < Test::Unit::TestCase
86
84
  assert_equal patched_process['name'], 'patched_process'
87
85
  assert_equal patched_process['access'], 'PUBLIC'
88
86
 
89
- deleted_process = Playlyfe.delete(route: "/processes/#{new_process['id']}", query: { player_id: player_id })
87
+ deleted_process = pl.delete(route: "/processes/#{new_process['id']}", query: { player_id: player_id })
90
88
  assert_not_nil deleted_process['message']
91
89
 
92
- #data = Playlyfe.put(route: "/players/#{player_id}/reset", query: { player_id: player_id })
90
+ #data = pl.put(route: "/players/#{player_id}/reset", query: { player_id: player_id })
93
91
  #puts data
94
92
 
95
- raw_data = Playlyfe.get(route: '/player', query: { player_id: player_id }, raw: true)
93
+ raw_data = pl.get(route: '/player', query: { player_id: player_id }, raw: true)
96
94
  assert_equal raw_data.class, String
97
95
  end
98
96
 
99
97
  def test_init_production
100
- Playlyfe.init(
98
+ pl = Playlyfe.new(
101
99
  client_id: "N2Y4NjNlYTItODQzZi00YTQ0LTkzZWEtYTBiNTA2ODg3MDU4",
102
100
  client_secret: "NDc3NTA0NmItMjBkZi00MjI2LWFhMjUtOTI0N2I1YTkxYjc2M2U3ZGI0MDAtNGQ1Mi0xMWU0LWJmZmUtMzkyZTdiOTYxYmMx",
103
101
  type: 'client'
104
102
  )
105
- #player = Playlyfe.get(route: '/players', query: { player_id: 'l54328754bddc332e0021a847', limit: 1 })
106
- #assert_equal player["data"][0]["email"], "peter@playlyfe.com"
103
+ players = pl.get(route: '/game/players', query: { limit: 1 })
107
104
  end
108
105
 
109
106
  def test_store
110
107
  redis = Redis.new
111
- Playlyfe.init(
108
+ pl = Playlyfe.new(
112
109
  client_id: "Zjc0MWU0N2MtODkzNS00ZWNmLWEwNmYtY2M1MGMxNGQ1YmQ4",
113
110
  client_secret: "YzllYTE5NDQtNDMwMC00YTdkLWFiM2MtNTg0Y2ZkOThjYTZkMGIyNWVlNDAtNGJiMC0xMWU0LWI2NGEtYjlmMmFkYTdjOTI3",
114
111
  type: 'client',
115
112
  store: lambda { |token| redis.set('token', JSON.generate(token)) },
116
- retrieve: lambda { return JSON.parse(redis.get('token')) }
113
+ load: lambda { return JSON.parse(redis.get('token')) }
117
114
  )
118
- players = Playlyfe.get(route: '/players', query: { player_id: 'student1', limit: 1 })
115
+ players = pl.get(route: '/players', query: { player_id: 'student1', limit: 1 })
119
116
  assert_not_nil players["data"]
120
117
  assert_not_nil players["data"][0]
121
118
  end
122
119
 
123
120
  def test_auth_code_error
124
121
  begin
125
- Playlyfe.init(
122
+ Playlyfe.new(
126
123
  client_id: "NGM2ZmYyNGQtNjViMy00YjQ0LWI0YTgtZTdmYWFlNDRkMmUx",
127
124
  client_secret: "ZTQ0OWI4YTItYzE4ZC00MWQ5LTg3YjktMDI5ZjAxYTBkZmRiZGQ0NzI4OTAtNGQ1My0xMWU0LWJmZmUtMzkyZTdiOTYxYmMx",
128
125
  type: 'code'
@@ -133,7 +130,7 @@ class PlaylyfeTest < Test::Unit::TestCase
133
130
  end
134
131
 
135
132
  def test_auth_code
136
- Playlyfe.init(
133
+ Playlyfe.new(
137
134
  client_id: "NGM2ZmYyNGQtNjViMy00YjQ0LWI0YTgtZTdmYWFlNDRkMmUx",
138
135
  client_secret: "ZTQ0OWI4YTItYzE4ZC00MWQ5LTg3YjktMDI5ZjAxYTBkZmRiZGQ0NzI4OTAtNGQ1My0xMWU0LWJmZmUtMzkyZTdiOTYxYmMx",
139
136
  type: 'code',
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.5.5
4
+ version: 0.6.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-10-09 00:00:00.000000000 Z
12
+ date: 2014-10-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest_client