Class: PlaylyfeClient::Connection
- Inherits:
-
Object
- Object
- PlaylyfeClient::Connection
- Defined in:
- lib/playlyfe_client/connection.rb
Overview
based on playlyfe-rub-sdk (github.com/playlyfe/playlyfe-ruby-sdk)
Direct Known Subclasses
Instance Attribute Summary collapse
-
#sdk_version ⇒ Object
readonly
Returns the value of attribute sdk_version.
Class Method Summary collapse
Instance Method Summary collapse
- #api(method, route, query = {}, body = {}, raw = false) ⇒ Object
- #api_version ⇒ Object
- #check_token(query) ⇒ Object
- #delete(route, query = {}) ⇒ Object
- #exchange_code(code) ⇒ Object
- #game ⇒ Object
- #get(route, query = {}) ⇒ Object
- #get_access_token ⇒ Object
- #get_login_url ⇒ Object
- #get_logout_url ⇒ Object
- #get_raw(route, query = {}) ⇒ Object
- #hash_to_query(hash) ⇒ Object
-
#initialize(options = {}) ⇒ Connection
constructor
A new instance of Connection.
- #patch(route, query = {}, body = {}) ⇒ Object
- #post(route, query = {}, body = {}) ⇒ Object
- #put(route, query = {}, body = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Connection
Returns a new instance of Connection.
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 |
# File 'lib/playlyfe_client/connection.rb', line 23 def initialize( = {}) if [:type].nil? err = PlaylyfeClient::ConnectionError.new("") err.name = 'init_failed' err. = "You must pass in a type whether 'client' for client credentials flow or 'code' for auth code flow" raise err end @version = [:version] ||= 'v2' @type = [:type] @id = [:client_id] @secret = [:client_secret] @store = [:store] @load = [:load] @redirect_uri = [:redirect_uri] @skip_api_calls_limit_exceeded_error=[:skip_api_calls_limit_exceeded_error] || false if @store.nil? @store = lambda { |token| puts 'Storing Token' } end if @type == 'client' get_access_token() else if [:redirect_uri].nil? err = PlaylyfeClient::ConnectionError.new("") err.name = 'init_failed' err. = 'You must pass in a redirect_uri for the auth code flow' raise err end end end |
Instance Attribute Details
#sdk_version ⇒ Object (readonly)
Returns the value of attribute sdk_version.
12 13 14 |
# File 'lib/playlyfe_client/connection.rb', line 12 def sdk_version @sdk_version end |
Class Method Details
.createJWT(options) ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/playlyfe_client/connection.rb', line 14 def self.createJWT() [:scopes] ||= [] [:expires] ||= 3600 expires = Time.now.to_i + [:expires] token = JWT.encode({:player_id => [:player_id], :scopes => [:scopes], :exp => expires}, [:client_secret], 'HS256') token = "#{options[:client_id]}:#{token}" return token end |
Instance Method Details
#api(method, route, query = {}, body = {}, raw = false) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/playlyfe_client/connection.rb', line 121 def api(method, route, query = {}, body = {}, raw = false) query = check_token(query) begin uri="https://api.playlyfe.com/#{@version}#{route}?#{query}" puts "doing real query to api.uri : #{uri} with body #{body.to_json}" response = RestClient::Request.execute( :method => method, :url => uri, :headers => { :content_type => :json, :accepts => :json }, :payload => body.to_json, :ssl_version => 'SSLv23' ) if raw == true return response.body else if response.body == 'null' return nil else return JSON.parse(response.body)#, symbolize_names: true) end end rescue => e raise PlaylyfeClient::Error.build(e.response, "#{method} #{uri}") end end |
#api_version ⇒ Object
53 54 55 |
# File 'lib/playlyfe_client/connection.rb', line 53 def api_version @version end |
#check_token(query) ⇒ Object
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/playlyfe_client/connection.rb', line 110 def check_token(query) access_token = @load.call if access_token['expires_at'] < Time.now.to_i puts 'Access Token Expired' get_access_token() access_token = @load.call end query[:access_token] = access_token['access_token'] query = hash_to_query(query) end |
#delete(route, query = {}) ⇒ Object
171 172 173 |
# File 'lib/playlyfe_client/connection.rb', line 171 def delete(route, query = {}) api(:delete, route, query, {}, false) end |
#exchange_code(code) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/playlyfe_client/connection.rb', line 188 def exchange_code(code) if code.nil? err = PlaylyfeClient::ConnectionError.new("") err.name = 'init_failed' err. = 'You must pass in a code in exchange_code for the auth code flow' raise err else @code = code get_access_token() end end |
#game ⇒ Object
57 58 59 |
# File 'lib/playlyfe_client/connection.rb', line 57 def game fail PlaylyfeClient::GameError.new("{\"error\": \"unsupported version of API\", \"error_description\": \"'#{self.api_version}' of API is unsupported by playlyfe_client\"}") end |
#get(route, query = {}) ⇒ Object
151 152 153 |
# File 'lib/playlyfe_client/connection.rb', line 151 def get(route, query = {}) api(:get, route, query, {}, false) end |
#get_access_token ⇒ Object
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/playlyfe_client/connection.rb', line 61 def get_access_token begin if @type == 'client' access_token = RestClient::Request.execute(:method => :post, :url => 'https://playlyfe.com/auth/token', :payload => { :client_id => @id, :client_secret => @secret, :grant_type => 'client_credentials' }.to_json, :headers => { :content_type => :json, :accepts => :json }, :ssl_version => 'SSLv23' ) else access_token = RestClient::Request.execute(:method => :post, :url => "https://playlyfe.com/auth/token", :payload => { :client_id => @id, :client_secret => @secret, :grant_type => 'authorization_code', :code => @code, :redirect_uri => @redirect_uri }.to_json, :headers => { :content_type => :json, :accepts => :json }, :ssl_version => 'SSLv23' ) end access_token = JSON.parse(access_token) expires_at ||= Time.now.to_i + access_token['expires_in'] access_token.delete('expires_in') access_token['expires_at'] = expires_at @store.call access_token if @load.nil? @load = lambda { return access_token } else old_token = @load.call if access_token != old_token @load = lambda { return access_token } end end rescue => e raise PlaylyfeClient::ConnectionError.new(e.response) end end |
#get_login_url ⇒ Object
179 180 181 182 |
# File 'lib/playlyfe_client/connection.rb', line 179 def get_login_url query = { response_type: 'code', redirect_uri: @redirect_uri, client_id: @id } "https://playlyfe.com/auth?#{hash_to_query(query)}" end |
#get_logout_url ⇒ Object
184 185 186 |
# File 'lib/playlyfe_client/connection.rb', line 184 def get_logout_url "" end |
#get_raw(route, query = {}) ⇒ Object
155 156 157 |
# File 'lib/playlyfe_client/connection.rb', line 155 def get_raw(route, query = {}) api(:get, route, query, {}, true) end |
#hash_to_query(hash) ⇒ Object
175 176 177 |
# File 'lib/playlyfe_client/connection.rb', line 175 def hash_to_query(hash) return URI.encode(hash.map{|k,v| "#{k}=#{v}"}.join("&")) end |
#patch(route, query = {}, body = {}) ⇒ Object
167 168 169 |
# File 'lib/playlyfe_client/connection.rb', line 167 def patch(route, query = {}, body = {}) api(:patch, route, query, body, false) end |
#post(route, query = {}, body = {}) ⇒ Object
159 160 161 |
# File 'lib/playlyfe_client/connection.rb', line 159 def post(route, query = {}, body = {}) api(:post, route, query, body, false) end |
#put(route, query = {}, body = {}) ⇒ Object
163 164 165 |
# File 'lib/playlyfe_client/connection.rb', line 163 def put(route, query = {}, body = {}) api(:put, route, query, body, false) end |