Class: BungieClient::Client

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

Constant Summary collapse

BUNGIE_URI =
'https://www.bungie.net/Platform'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Init client

Initialize client for bungie api throw hash:

Parameters:

  • options (Hash)

Options Hash (options):

  • :api_key (String)
  • :token (String)

    is authorization token from oauth2

See Also:



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
62
# File 'lib/bungie_client/client.rb', line 35

def initialize(options)
  # Checking options and @api_key
  raise 'Wrong options: It must be Hash.' unless options.is_a? Hash

  if options[:api_key].nil?
    raise 'The API-key required for every request to bungie.'
  else
    @api_key = options[:api_key].to_s
  end

  # Set token
  @token = options[:token].to_s unless options[:token].nil?

  # Init connection
  @conn = Faraday.new url: BUNGIE_URI do |builder|
    builder.headers['Content-Type']  = 'application/json'
    builder.headers['Accept']        = 'application/json'
    builder.headers['X-API-Key']     = @api_key
    builder.headers['Authorization'] = "Bearer #{@token}" unless @token.nil?

    builder.options.timeout      = 5
    builder.options.open_timeout = 2

    builder.use FaradayMiddleware::FollowRedirects, limit: 5

    builder.adapter :httpclient
  end
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



24
25
26
# File 'lib/bungie_client/client.rb', line 24

def api_key
  @api_key
end

#connObject (readonly)

Returns the value of attribute conn.



24
25
26
# File 'lib/bungie_client/client.rb', line 24

def conn
  @conn
end

#tokenObject (readonly)

Returns the value of attribute token.



24
25
26
# File 'lib/bungie_client/client.rb', line 24

def token
  @token
end

Class Method Details

.parse(response) ⇒ Mash

Format answer from Bungie

Parameters:

Returns:

  • (Mash)


14
15
16
17
18
19
20
21
22
# File 'lib/bungie_client/client.rb', line 14

def self.parse(response)
  response = begin
    MultiJson.load response
  rescue StandardError
    {}
  end

  Hashie::Mash.new response
end

Instance Method Details

#get(url, parameters = {}, headers = {}) ⇒ Mash

Get request to bungie service

Parameters:

  • url (String)
  • parameters (Hash) (defaults to: {})

    for http-query

  • headers (Hash) (defaults to: {})

Returns:

  • (Mash)

See Also:



73
74
75
76
77
# File 'lib/bungie_client/client.rb', line 73

def get(url, parameters = {}, headers = {})
  self.class.parse @conn.get(url, parameters, headers).body
rescue StandardError
  Hashie::Mash.new
end

#post(url, query = {}, headers = {}) ⇒ Mash

Post data to Bungie services

Parameters:

  • url (String)
  • query (Hash) (defaults to: {})
  • headers (Hash) (defaults to: {})

Returns:

  • (Mash)


86
87
88
89
90
# File 'lib/bungie_client/client.rb', line 86

def post(url, query = {}, headers = {})
  self.class.parse @conn.post(url, query, headers).body
rescue StandardError
  Hashie::Mash.new
end