Class: Playlyfe

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Playlyfe

Returns a new instance of Playlyfe.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/playlyfe.rb', line 34

def initialize(options = {})
  if options[:type].nil?
    err = PlaylyfeError.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 = PlaylyfeError.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.



23
24
25
# File 'lib/playlyfe.rb', line 23

def sdk_version
  @sdk_version
end

Class Method Details

.createJWT(options) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/playlyfe.rb', line 25

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



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.rb', line 123

def api(method, route, query = {}, body = {}, raw = false)
  query = check_token(query)
  begin
    res = RestClient::Request.execute(
      :method => method,
      :url => "https://api.playlyfe.com/#{@version}#{route}?#{query}",
      :headers => {
        :content_type => :json,
        :accepts => :json
      },
      :payload => body.to_json,
      :ssl_version => 'SSLv23'
    )
    if raw == true
      return res.body
    else
      if res.body == 'null'
        return nil
      else
        return JSON.parse(res.body)
      end
    end
  rescue => e
    raise PlaylyfeError.new(e.response)
  end
end

#check_token(query) ⇒ Object



112
113
114
115
116
117
118
119
120
121
# File 'lib/playlyfe.rb', line 112

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.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.rb', line 187

def exchange_code(code)
  if code.nil?
    err = PlaylyfeError.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

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



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

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

#get_access_tokenObject



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
109
110
# File 'lib/playlyfe.rb', line 63

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 PlaylyfeError.new(e.response)
  end
end

#get_login_urlObject



178
179
180
181
# File 'lib/playlyfe.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.rb', line 183

def get_logout_url
  ""
end

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



154
155
156
# File 'lib/playlyfe.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.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.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.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.rb', line 162

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