Class: OAuth2::BungieClient

Inherits:
Client
  • Object
show all
Defined in:
lib/oauth2/bungie_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret, options = {}, &block) ⇒ BungieClient

Returns a new instance of BungieClient.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/oauth2/bungie_client.rb', line 3

def initialize(client_id, client_secret, options = {}, &block)
  opts = options.dup
  @id = client_id
  @secret = client_secret
  @site = 'https://www.bungie.net'
  ssl = opts.delete(:ssl)
  @options = {
    :authorize_url     => 'https://www.bungie.net',
    :token_url         => 'https://www.bungie.net/Platform/App/GetAccessTokensFromCode',
    :refresh_token_url => 'https://www.bungie.net/Platform/App/GetAccessTokensFromRefreshToken',
    :token_method      => :post,
    :connection_opts   => {},
    :connection_build  => block,
    :max_redirects     => 5,
    :raise_errors      => true}.merge(opts)
  @options[:connection_opts][:ssl] = ssl if ssl
end

Instance Method Details

#get_normalized_response(response) ⇒ Object

Transform response body to RFC specification



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/oauth2/bungie_client.rb', line 72

def get_normalized_response(response)
  response = response.parsed

  return nil unless response.is_a?(Hash)

  if response['ErrorCode'] == 1 && !response.dig('Response', 'accessToken').nil?
    {
      'access_token' => response.dig('Response', 'accessToken', 'value'),
      'token_type' => 'Bearer',
      'expires_in' => response.dig('Response', 'accessToken', 'expires'),
      'refresh_token' => response.dig('Response', 'refreshToken', 'value'),
      'refresh_expries_in' => response.dig('Response', 'refreshToken', 'expires')
    }
  else
    response
  end
end

#get_token(params, access_token_opts = {}, access_token_class = BungieAccessToken) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/oauth2/bungie_client.rb', line 21

def get_token(params, access_token_opts = {}, access_token_class = BungieAccessToken)
  opts = {:raise_errors => options[:raise_errors], :parse => params.delete(:parse)}

  if options[:token_method] == :post
    headers = params.delete(:headers)
    opts[:body] = params
    opts[:headers] = {'Content-Type' => 'application/json'}
    opts[:headers].merge!(headers) if headers
  else
    opts[:params] = params
  end

  response = request(options[:token_method], token_url, opts)

  error = Error.new(response)

  response = get_normalized_response(response)

  raise(error) if options[:raise_errors] && !(response.is_a?(Hash) && response['access_token'])

  access_token_class.from_hash(self, response.merge(access_token_opts))
end

#get_token_with_refresh(params, access_token_opts = {}, access_token_class = BungieAccessToken) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/oauth2/bungie_client.rb', line 44

def get_token_with_refresh(params, access_token_opts = {}, access_token_class = BungieAccessToken)
  opts = {:raise_errors => options[:raise_errors], :parse => params.delete(:parse)}

  if options[:token_method] == :post
    headers = params.delete(:headers)
    opts[:body] = params
    opts[:headers] = {'Content-Type' => 'application/json'}
    opts[:headers].merge!(headers) if headers
  else
    opts[:params] = params
  end

  response = request(
    options[:token_method],
    connection.build_url(options[:refresh_token_url]).to_s,
    opts
  )

  error = Error.new(response)

  response = get_normalized_response(response)

  raise(error) if options[:raise_errors] && !(response.is_a?(Hash) && response['access_token'])

  access_token_class.from_hash(self, response.merge(access_token_opts))
end