Class: PlaylyfeClient::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/playlyfe_client/connection.rb

Overview

based on playlyfe-rub-sdk (github.com/playlyfe/playlyfe-ruby-sdk)

Direct Known Subclasses

V2::Connection

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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
# File 'lib/playlyfe_client/connection.rb', line 23

def initialize(options = {})
  if options[:type].nil?
    err = PlaylyfeClient::ConnectionError.new("")
    err.name = 'init_failed'
    err.message = "You must pass in a type whether 'client' for client credentials flow or 'code' for auth code flow"
    raise err
  end
  @version = options[:version] ||= 'v2'
  @type = options[:type]
  @id = options[:client_id]
  @secret = options[:client_secret]
  @store = options[:store]
  @load = options[:load]
  @redirect_uri = options[:redirect_uri]
  if @store.nil?
    @store = lambda { |token| puts 'Storing Token' }
  end
  if @type == 'client'
    get_access_token()
  else
    if options[:redirect_uri].nil?
      err = PlaylyfeClient::ConnectionError.new("")
      err.name = 'init_failed'
      err.message = 'You must pass in a redirect_uri for the auth code flow'
      raise err
    end
  end
end

Instance Attribute Details

#sdk_versionObject (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(options)
  options[:scopes] ||= []
  options[:expires] ||= 3600
  expires = Time.now.to_i + options[:expires]
  token = JWT.encode({:player_id => options[:player_id], :scopes => options[:scopes], :exp => expires}, options[:client_secret], 'HS256')
  token = "#{options[:client_id]}:#{token}"
  return token
end

Instance Method Details

#api(method, route, query = {}, body = {}, raw = false) ⇒ Object



120
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
# File 'lib/playlyfe_client/connection.rb', line 120

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)
      end
    end
  rescue => e
    raise PlaylyfeClient::Error.build(e.response, "#{method} #{uri}")
  end
end

#api_versionObject



52
53
54
# File 'lib/playlyfe_client/connection.rb', line 52

def api_version
  @version
end

#check_token(query) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/playlyfe_client/connection.rb', line 109

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



170
171
172
# File 'lib/playlyfe_client/connection.rb', line 170

def delete(route, query = {})
  api(:delete, route, query, {}, false)
end

#exchange_code(code) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
# File 'lib/playlyfe_client/connection.rb', line 187

def exchange_code(code)
  if code.nil?
    err = PlaylyfeClient::ConnectionError.new("")
    err.name = 'init_failed'
    err.message = 'You must pass in a code in exchange_code for the auth code flow'
    raise err
  else
    @code = code
    get_access_token()
  end
end

#gameObject



56
57
58
# File 'lib/playlyfe_client/connection.rb', line 56

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



150
151
152
# File 'lib/playlyfe_client/connection.rb', line 150

def get(route, query = {})
  api(:get, route, query, {}, false)
end

#get_access_tokenObject



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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/playlyfe_client/connection.rb', line 60

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_urlObject



178
179
180
181
# File 'lib/playlyfe_client/connection.rb', line 178

def 
  query = { response_type: 'code', redirect_uri: @redirect_uri, client_id: @id }
  "https://playlyfe.com/auth?#{hash_to_query(query)}"
end

#get_logout_urlObject



183
184
185
# File 'lib/playlyfe_client/connection.rb', line 183

def get_logout_url
  ""
end

#get_raw(route, query = {}) ⇒ Object



154
155
156
# File 'lib/playlyfe_client/connection.rb', line 154

def get_raw(route, query = {})
  api(:get, route, query, {}, true)
end

#hash_to_query(hash) ⇒ Object



174
175
176
# File 'lib/playlyfe_client/connection.rb', line 174

def hash_to_query(hash)
  return URI.encode(hash.map{|k,v| "#{k}=#{v}"}.join("&"))
end

#patch(route, query = {}, body = {}) ⇒ Object



166
167
168
# File 'lib/playlyfe_client/connection.rb', line 166

def patch(route, query = {}, body = {})
  api(:patch, route, query, body, false)
end

#post(route, query = {}, body = {}) ⇒ Object



158
159
160
# File 'lib/playlyfe_client/connection.rb', line 158

def post(route, query = {}, body = {})
  api(:post, route, query, body, false)
end

#put(route, query = {}, body = {}) ⇒ Object



162
163
164
# File 'lib/playlyfe_client/connection.rb', line 162

def put(route, query = {}, body = {})
  api(:put, route, query, body, false)
end