playlyfe_client 1.0.2

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 (40) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +27 -0
  3. data/Gemfile +9 -0
  4. data/Gemfile.lock +50 -0
  5. data/LICENSE.txt +21 -0
  6. data/README.md +97 -0
  7. data/Rakefile +10 -0
  8. data/lib/playlyfe_client/action.rb +32 -0
  9. data/lib/playlyfe_client/connection.rb +200 -0
  10. data/lib/playlyfe_client/errors.rb +42 -0
  11. data/lib/playlyfe_client/game.rb +33 -0
  12. data/lib/playlyfe_client/leaderboard.rb +27 -0
  13. data/lib/playlyfe_client/metric.rb +14 -0
  14. data/lib/playlyfe_client/player.rb +80 -0
  15. data/lib/playlyfe_client/team.rb +26 -0
  16. data/lib/playlyfe_client/v2/action.rb +59 -0
  17. data/lib/playlyfe_client/v2/collection/action_collection.rb +30 -0
  18. data/lib/playlyfe_client/v2/collection/leaderboard_collection.rb +43 -0
  19. data/lib/playlyfe_client/v2/collection/metric_collection.rb +30 -0
  20. data/lib/playlyfe_client/v2/collection/player_collection.rb +30 -0
  21. data/lib/playlyfe_client/v2/collection/team_collection.rb +31 -0
  22. data/lib/playlyfe_client/v2/collection.rb +60 -0
  23. data/lib/playlyfe_client/v2/connection.rb +109 -0
  24. data/lib/playlyfe_client/v2/game.rb +71 -0
  25. data/lib/playlyfe_client/v2/leaderboard/players_leaderboard.rb +30 -0
  26. data/lib/playlyfe_client/v2/leaderboard/teams_leaderboard.rb +30 -0
  27. data/lib/playlyfe_client/v2/leaderboard/unknown_leaderboard.rb +23 -0
  28. data/lib/playlyfe_client/v2/leaderboard.rb +38 -0
  29. data/lib/playlyfe_client/v2/metric/compound_metric.rb +31 -0
  30. data/lib/playlyfe_client/v2/metric/point_metric.rb +27 -0
  31. data/lib/playlyfe_client/v2/metric/set_metric.rb +41 -0
  32. data/lib/playlyfe_client/v2/metric/state_metric.rb +26 -0
  33. data/lib/playlyfe_client/v2/metric.rb +47 -0
  34. data/lib/playlyfe_client/v2/player.rb +135 -0
  35. data/lib/playlyfe_client/v2/team.rb +58 -0
  36. data/lib/playlyfe_client/version.rb +3 -0
  37. data/lib/playlyfe_client.rb +11 -0
  38. data/lib/string_addons.rb +9 -0
  39. data/playlyfe_client.gemspec +30 -0
  40. metadata +194 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6d2a35f5b9c9682634b330792419663b08449c8e
4
+ data.tar.gz: 03e9ac4e72b4124a8beafddf2b55a1dbfda9c215
5
+ SHA512:
6
+ metadata.gz: 96022c5547d3be058617ada499686c9485b778844cf901b0fb1d281a0d6bf5f7f3d88bb8b35655647444afbfbced744c8e09bfaaed2e55dc15bd298aecb22a06
7
+ data.tar.gz: 102dbdef6471321d301688694b12f9a7040383b983d46f8a04c16eb6aaa64dd9bd61392a2628c40ef0533bd85957a2f02587507d97874b6682ff84403b076d94
data/.gitignore ADDED
@@ -0,0 +1,27 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /bin/
11
+
12
+ ## Environment normalization:
13
+ /.bundle/
14
+ /vendor/bundle
15
+ /lib/bundler/man/
16
+
17
+ # for a library or gem, you might want to ignore these files since the code is
18
+ # intended to run in multiple environments; otherwise, check them in:
19
+ # Gemfile.lock
20
+ .ruby-version
21
+ .ruby-gemset
22
+
23
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
24
+ .rvmrc
25
+
26
+ .tags
27
+ .gemtags
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ #production dependecies
4
+ gem 'rest_client'
5
+ gem 'json'
6
+ gem 'jwt'
7
+
8
+ # Specify your gem's dependencies in playlyfe_client.gemspec
9
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,50 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ playlyfe_client (1.0.2)
5
+ json
6
+ jwt
7
+ rest_client
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ ansi (1.5.0)
13
+ builder (3.2.2)
14
+ coderay (1.1.1)
15
+ json (1.8.3)
16
+ jwt (1.5.0)
17
+ method_source (0.8.2)
18
+ minitest (5.8.4)
19
+ minitest-reporters (1.1.8)
20
+ ansi
21
+ builder
22
+ minitest (>= 5.0)
23
+ ruby-progressbar
24
+ netrc (0.7.9)
25
+ pry (0.10.3)
26
+ coderay (~> 1.1.0)
27
+ method_source (~> 0.8.1)
28
+ slop (~> 3.4)
29
+ rake (10.4.2)
30
+ rest_client (1.7.2)
31
+ netrc (~> 0.7.7)
32
+ ruby-progressbar (1.8.0)
33
+ slop (3.6.0)
34
+
35
+ PLATFORMS
36
+ ruby
37
+
38
+ DEPENDENCIES
39
+ bundler (~> 1.10)
40
+ json
41
+ jwt
42
+ minitest
43
+ minitest-reporters
44
+ playlyfe_client!
45
+ pry
46
+ rake (~> 10.0)
47
+ rest_client
48
+
49
+ BUNDLED WITH
50
+ 1.10.6
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,97 @@
1
+ # PlaylyfeClient
2
+
3
+ Model/Object oriented PlaylyfeClient client based on PlaylyfeClient SDK.
4
+ It's focus is abstract direct calls with hand written routes into calling methods on `game`.
5
+ All aspects of game is defined at PlaylyfeClient, this client serves only for sending actions and reading results and stats.
6
+
7
+ ## Base Classes
8
+ * Game
9
+ * Action
10
+ * Player
11
+ * Team
12
+ * Leaderboard
13
+ * Metric
14
+
15
+ ### Game
16
+ base client class to interact with PlaylyfeClient game
17
+ * has many available actions
18
+ * has many players
19
+ * has many teams
20
+ * has many metrics
21
+ * has many leaderboards
22
+
23
+ ### Action
24
+ Equivalent to PlaylyfeClient action (or some complicated rule)
25
+
26
+ ### Player
27
+ Player of game , his profile and scores
28
+
29
+ ### Leaderboard
30
+ Leaderboards of the Game
31
+
32
+
33
+ ## Installation
34
+
35
+ Add this line to your application's Gemfile:
36
+
37
+ ```ruby
38
+ gem 'playlyfe_client'
39
+ ```
40
+
41
+ And then execute:
42
+
43
+ $ bundle
44
+
45
+ Or install it yourself as:
46
+
47
+ $ gem install playlyfe_client
48
+
49
+ ## Usage
50
+
51
+ You have to setup a client at PlaylyfeClient which can access API (only paid plans) form your backend.
52
+ Client shoul have 'Client Credential Flow' , all checks at config page should be on YES and should have all scopes with RW access.
53
+
54
+ Then in your code initialize connection:
55
+
56
+ $ conn= PlaylyfeClient::V2::Connection.new(
57
+ version: 'v2',
58
+ client_id: CLIENT_ID,
59
+ client_secret: CLIENT_SECRET,
60
+ type: 'client'
61
+ )
62
+
63
+ game=connection.game
64
+
65
+ With game in hand you can start to ask:
66
+
67
+ pl1=game.player.first
68
+ pl1.scores
69
+ game.teams.all
70
+ game.metrics
71
+ a=game.actions.find("my_action_id")
72
+
73
+ or play
74
+
75
+ pl1.play(a)
76
+ a.play_by(pl1) #equivalent to previous line
77
+
78
+ or add players
79
+
80
+ PlaylyfeClient::V2::Player.create({id: "batman", alias: "Bruce Wayne"}, game)
81
+
82
+
83
+ ## Development
84
+
85
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
86
+
87
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
88
+
89
+ ## Contributing
90
+
91
+ Bug reports and pull requests are welcome on GitHub at https://github.com/foton/playlyfe_client.
92
+
93
+
94
+ ## License
95
+
96
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
97
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,32 @@
1
+ require_relative "./errors.rb"
2
+
3
+ module PlaylyfeClient
4
+ class Action
5
+
6
+ attr_reader :game
7
+
8
+ def self.all(game)
9
+ game.avaliable_actions
10
+ end
11
+
12
+ def play_by(player)
13
+ player.play(self)
14
+ end
15
+
16
+ def apply_rewards_on_scores(scores)
17
+ new_scores=scores.dup
18
+ self.rewards.each do |reward|
19
+ reward[:metric].apply_reward(reward, new_scores)
20
+ end
21
+ new_scores
22
+ end
23
+
24
+ private
25
+
26
+ def initialize(game)
27
+ @game=game
28
+ end
29
+
30
+ end
31
+ end
32
+
@@ -0,0 +1,200 @@
1
+ require 'uri'
2
+ require 'json'
3
+ require 'rest_client'
4
+ require 'jwt'
5
+
6
+ require_relative "./errors.rb"
7
+ require_relative "./v2/game.rb"
8
+
9
+ module PlaylyfeClient
10
+ #based on playlyfe-rub-sdk (https://github.com/playlyfe/playlyfe-ruby-sdk)
11
+ class Connection
12
+ attr_reader :sdk_version
13
+
14
+ def self.createJWT(options)
15
+ options[:scopes] ||= []
16
+ options[:expires] ||= 3600
17
+ expires = Time.now.to_i + options[:expires]
18
+ token = JWT.encode({:player_id => options[:player_id], :scopes => options[:scopes], :exp => expires}, options[:client_secret], 'HS256')
19
+ token = "#{options[:client_id]}:#{token}"
20
+ return token
21
+ end
22
+
23
+ def initialize(options = {})
24
+ if options[:type].nil?
25
+ err = PlaylyfeClient::ConnectionError.new("")
26
+ err.name = 'init_failed'
27
+ err.message = "You must pass in a type whether 'client' for client credentials flow or 'code' for auth code flow"
28
+ raise err
29
+ end
30
+ @version = options[:version] ||= 'v2'
31
+ @type = options[:type]
32
+ @id = options[:client_id]
33
+ @secret = options[:client_secret]
34
+ @store = options[:store]
35
+ @load = options[:load]
36
+ @redirect_uri = options[:redirect_uri]
37
+ if @store.nil?
38
+ @store = lambda { |token| puts 'Storing Token' }
39
+ end
40
+ if @type == 'client'
41
+ get_access_token()
42
+ else
43
+ if options[:redirect_uri].nil?
44
+ err = PlaylyfeClient::ConnectionError.new("")
45
+ err.name = 'init_failed'
46
+ err.message = 'You must pass in a redirect_uri for the auth code flow'
47
+ raise err
48
+ end
49
+ end
50
+ end
51
+
52
+ def api_version
53
+ @version
54
+ end
55
+
56
+ def game
57
+ fail PlaylyfeClient::GameError.new("{\"error\": \"unsupported version of API\", \"error_description\": \"'#{self.api_version}' of API is unsupported by playlyfe_client\"}")
58
+ end
59
+
60
+ def get_access_token
61
+ begin
62
+ if @type == 'client'
63
+ access_token = RestClient::Request.execute(:method => :post, :url => 'https://playlyfe.com/auth/token',
64
+ :payload => {
65
+ :client_id => @id,
66
+ :client_secret => @secret,
67
+ :grant_type => 'client_credentials'
68
+ }.to_json,
69
+ :headers => {
70
+ :content_type => :json,
71
+ :accepts => :json
72
+ },
73
+ :ssl_version => 'SSLv23'
74
+ )
75
+ else
76
+ access_token = RestClient::Request.execute(:method => :post, :url => "https://playlyfe.com/auth/token",
77
+ :payload => {
78
+ :client_id => @id,
79
+ :client_secret => @secret,
80
+ :grant_type => 'authorization_code',
81
+ :code => @code,
82
+ :redirect_uri => @redirect_uri
83
+ }.to_json,
84
+ :headers => {
85
+ :content_type => :json,
86
+ :accepts => :json
87
+ },
88
+ :ssl_version => 'SSLv23'
89
+ )
90
+ end
91
+ access_token = JSON.parse(access_token)
92
+ expires_at ||= Time.now.to_i + access_token['expires_in']
93
+ access_token.delete('expires_in')
94
+ access_token['expires_at'] = expires_at
95
+ @store.call access_token
96
+ if @load.nil?
97
+ @load = lambda { return access_token }
98
+ else
99
+ old_token = @load.call
100
+ if access_token != old_token
101
+ @load = lambda { return access_token }
102
+ end
103
+ end
104
+ rescue => e
105
+ raise PlaylyfeClient::ConnectionError.new(e.response)
106
+ end
107
+ end
108
+
109
+ def check_token(query)
110
+ access_token = @load.call
111
+ if access_token['expires_at'] < Time.now.to_i
112
+ puts 'Access Token Expired'
113
+ get_access_token()
114
+ access_token = @load.call
115
+ end
116
+ query[:access_token] = access_token['access_token']
117
+ query = hash_to_query(query)
118
+ end
119
+
120
+ def api(method, route, query = {}, body = {}, raw = false)
121
+ query = check_token(query)
122
+ begin
123
+ uri="https://api.playlyfe.com/#{@version}#{route}?#{query}"
124
+ puts "doing real query to api.uri : #{uri} with body #{body.to_json}"
125
+ response = RestClient::Request.execute(
126
+ :method => method,
127
+ :url => uri,
128
+ :headers => {
129
+ :content_type => :json,
130
+ :accepts => :json
131
+ },
132
+ :payload => body.to_json,
133
+ :ssl_version => 'SSLv23'
134
+ )
135
+
136
+ if raw == true
137
+ return response.body
138
+ else
139
+ if response.body == 'null'
140
+ return nil
141
+ else
142
+ return JSON.parse(response.body)
143
+ end
144
+ end
145
+ rescue => e
146
+ raise PlaylyfeClient::Error.build(e.response, "#{method} #{uri}")
147
+ end
148
+ end
149
+
150
+ def get(route, query = {})
151
+ api(:get, route, query, {}, false)
152
+ end
153
+
154
+ def get_raw(route, query = {})
155
+ api(:get, route, query, {}, true)
156
+ end
157
+
158
+ def post(route, query = {}, body = {})
159
+ api(:post, route, query, body, false)
160
+ end
161
+
162
+ def put(route, query = {}, body = {})
163
+ api(:put, route, query, body, false)
164
+ end
165
+
166
+ def patch(route, query = {}, body = {})
167
+ api(:patch, route, query, body, false)
168
+ end
169
+
170
+ def delete(route, query = {})
171
+ api(:delete, route, query, {}, false)
172
+ end
173
+
174
+ def hash_to_query(hash)
175
+ return URI.encode(hash.map{|k,v| "#{k}=#{v}"}.join("&"))
176
+ end
177
+
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
182
+
183
+ def get_logout_url
184
+ ""
185
+ end
186
+
187
+ def exchange_code(code)
188
+ if code.nil?
189
+ err = PlaylyfeClient::ConnectionError.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
197
+ end
198
+ end
199
+
200
+ end
@@ -0,0 +1,42 @@
1
+ require 'json'
2
+
3
+ module PlaylyfeClient
4
+ #grabbed from playlyfe-rub-sdk
5
+ class Error < StandardError; end
6
+ class ConnectionError < PlaylyfeClient::Error; end
7
+ class GameError < PlaylyfeClient::Error; end
8
+ class PlayerError < PlaylyfeClient::Error; end
9
+ class LeaderboardError < PlaylyfeClient::Error; end
10
+ class ActionError < PlaylyfeClient::Error; end
11
+ class MetricError < PlaylyfeClient::Error; end
12
+ class PlaylyfeClient::ActionRateLimitExceededError < PlaylyfeClient::ActionError; end
13
+ class PlaylyfeClient::PlayerExistsError < PlaylyfeClient::PlayerError; end
14
+
15
+ class Error < StandardError
16
+ attr_accessor :name, :message
17
+ def initialize(res=nil,uri=nil)
18
+ @raw = res
19
+ unless res.nil? || res == ""
20
+ res = JSON.parse(res)
21
+ @name = res['error']
22
+ @message = res['error_description']
23
+ @message+=" [request: #{uri}]" unless uri.nil?
24
+ end
25
+ end
26
+
27
+ def self.build(res=nil,uri=nil)
28
+ err_class= PlaylyfeClient::ConnectionError
29
+ unless res.nil? || res == ""
30
+ res_h = JSON.parse(res)
31
+ if res_h['error'] == "rate_limit_exceeded"
32
+ err_class= PlaylyfeClient::ActionRateLimitExceededError
33
+ elsif res_h['error'] == "player_exists"
34
+ err_class= PlaylyfeClient::PlayerExistsError
35
+
36
+ end
37
+ end
38
+ err_class.new(res, uri)
39
+ end
40
+ end
41
+
42
+ end
@@ -0,0 +1,33 @@
1
+
2
+ require_relative "./errors.rb"
3
+
4
+ module PlaylyfeClient
5
+ #Game is 1:1 to connection, so only one instance per connection
6
+ class Game
7
+
8
+ attr_reader :title, :connection
9
+
10
+
11
+ def to_hash
12
+ @game_hash || {}
13
+ end
14
+
15
+ def players
16
+ []
17
+ end
18
+
19
+ def actions
20
+ []
21
+ end
22
+
23
+ def leaderboards
24
+ []
25
+ end
26
+
27
+ private
28
+
29
+ def initialize(conn)
30
+ @connection=conn
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,27 @@
1
+ require_relative "./errors.rb"
2
+
3
+ module PlaylyfeClient
4
+ class Leaderboard
5
+
6
+ attr_reader :id, :name, :game, :positions
7
+
8
+ def self.all(game)
9
+ game.leaderboards
10
+ end
11
+
12
+ def table
13
+ positions
14
+ end
15
+
16
+ def results
17
+ positions
18
+ end
19
+
20
+ private
21
+
22
+ def initialize(game)
23
+ @game =game
24
+ @positions=[]
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,14 @@
1
+ require_relative "./errors.rb"
2
+
3
+ module PlaylyfeClient
4
+ class Metric
5
+ attr_reader :game
6
+
7
+ private
8
+
9
+ def initialize(game)
10
+ @game=game
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,80 @@
1
+ require_relative "./errors.rb"
2
+
3
+ module PlaylyfeClient
4
+ class Player
5
+
6
+ attr_reader :game
7
+
8
+ def self.create(player_hash, game)
9
+ nil
10
+ end
11
+
12
+ def self.all(game)
13
+ game.players
14
+ end
15
+
16
+ def play(action)
17
+ false
18
+ end
19
+
20
+ def scores
21
+ {points: {} ,sets: {}, states: {}, compound: {}}
22
+ end
23
+
24
+ def items_from_sets
25
+ items=[]
26
+ self.scores[:sets].each_pair do | key, value |
27
+ value.each do |item_h|
28
+ items << item_h.merge({metric_id: key.to_s}) if item_h[:count] > 0
29
+ end
30
+ end
31
+ items.sort! {|a,b| a[:name] <=> b[:name]}
32
+ items
33
+ end
34
+
35
+ def badges
36
+ self.items_from_sets
37
+ end
38
+
39
+ def points
40
+ points=[]
41
+ self.scores[:points].each_pair do | key, value |
42
+ points << {count: value, metric_id: key.to_s}
43
+ end
44
+ points
45
+ end
46
+
47
+ def states
48
+ states=[]
49
+ self.scores[:states].each_pair do | key, value |
50
+ states << {name: value, metric_id: key.to_s}
51
+ end
52
+ states
53
+ end
54
+
55
+ def levels
56
+ self.states
57
+ end
58
+
59
+ def teams
60
+ []
61
+ end
62
+
63
+ def players_leaderboards
64
+ []
65
+ end
66
+
67
+ def teams_leaderboards
68
+ []
69
+ end
70
+
71
+ def activities(start_time=nil,end_time=nil)
72
+ []
73
+ end
74
+
75
+ private
76
+ def initialize(game)
77
+ @game=game
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,26 @@
1
+ require_relative "./errors.rb"
2
+
3
+ module PlaylyfeClient
4
+ class Team
5
+
6
+ attr_reader :id, :name, :created_at, :game, :connection
7
+
8
+ def template
9
+ definition
10
+ end
11
+
12
+ def members
13
+ []
14
+ end
15
+
16
+ def leaderboards
17
+ []
18
+ end
19
+
20
+ private
21
+
22
+ def initialize(game)
23
+ @game =game
24
+ end
25
+ end
26
+ end