Class: BungieClient::Client

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

Overview

Class Client for GET/POST requests to Bungie. For specific HTTP operations you can use @agent [Mechanzie].

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)
  • :cookies (Array|CookieJar)

    with [HTTP::Cookie] or [CookieJar]

  • :username (String)

    username for authentication, necessary if set :authenticate

  • :password (String)

    password of user, necessary if set :authenticate

  • :type (String)

    of account, it can be ‘psn’ or ‘live’

See Also:



51
52
53
54
55
56
57
58
59
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
# File 'lib/bungie_client/client.rb', line 51

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

  # init @agent
  @agent = Mechanize.new do |config|
    config.read_timeout = 5
  end

  # merge cookies with options
  if BungieClient::Auth.valid_cookies? options[:cookies], true
    cookies = (options[:cookies].is_a? CookieJar) ? options[:cookies].cookies : options[:cookies]

    cookies.each do |cookie|
      @agent.cookie_jar.add cookie
    end
  end unless options[:cookies].nil?

  # make authentication and save new cookies in client
  unless options[:username].nil? || options[:password].nil?
    jar = BungieClient::Auth.auth options[:username].to_s, options[:password].to_s, (options[:type].to_s || 'psn')

    if jar.nil?
      raise "Wrong authentication. Check your account data."
    else
      jar.cookies.each do |cookie|
        @agent.cookie_jar.add cookie
      end
    end
  end
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



37
38
39
# File 'lib/bungie_client/client.rb', line 37

def agent
  @agent
end

#api_keyObject (readonly)

Returns the value of attribute api_key.



33
34
35
# File 'lib/bungie_client/client.rb', line 33

def api_key
  @api_key
end

#passwordObject (readonly)

Returns the value of attribute password.



35
36
37
# File 'lib/bungie_client/client.rb', line 35

def password
  @password
end

#typeObject (readonly)

Returns the value of attribute type.



36
37
38
# File 'lib/bungie_client/client.rb', line 36

def type
  @type
end

#usernameObject (readonly)

Returns the value of attribute username.



34
35
36
# File 'lib/bungie_client/client.rb', line 34

def username
  @username
end

Class Method Details

.parse_response(response) ⇒ Hashie::Mash

Format answer from bungie

Parameters:

Returns:

  • (Hashie::Mash)


19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bungie_client/client.rb', line 19

def self.parse_response(response)
  if !response.nil? && response != ''
    response = JSON.parse response

    if response.is_a?(Hash) && !response['Response'].nil? && response['ErrorCode'] == 1
      response = Hashie::Mash.new response

      return response['Response']
    end
  end

  Hashie::Mash.new
end

.request_uri(uri) ⇒ String

Form uri for requests

Parameters:

Returns:



10
11
12
# File 'lib/bungie_client/client.rb', line 10

def self.request_uri(uri)
  "#{BUNGIE_URI}/#{uri.sub(/^\//, '').sub(/\/$/, '')}/"
end

Instance Method Details

#get(uri, parameters = {}) ⇒ String|nil

Get response from bungie services

Parameters:

  • uri (String)
  • parameters (Hash|Array) (defaults to: {})

    for http-query

Returns:

See Also:



97
98
99
# File 'lib/bungie_client/client.rb', line 97

def get(uri, parameters = {})
  @agent.get(self.class.request_uri(uri), parameters, nil, headers).body rescue nil
end

#get_response(uri, parameters = {}) ⇒ Hashie::Mash

Get Response field after sending GET request to bungie

Parameters:

  • uri (String)
  • parameters (Hash|Array) (defaults to: {})

    for http-query

Returns:

  • (Hashie::Mash)


107
108
109
# File 'lib/bungie_client/client.rb', line 107

def get_response(uri, parameters = {})
  self.class.parse_response get(uri, parameters)
end

#post(uri, query = {}) ⇒ String|nil

Post data to bungie services

Parameters:

  • uri (String)
  • query (Hash) (defaults to: {})

Returns:



117
118
119
# File 'lib/bungie_client/client.rb', line 117

def post(uri, query = {})
  @agent.post(self.class.request_uri(uri), query, headers).body rescue nil
end

#post_response(uri, query = {}) ⇒ Hashie::Mash

Get Response field after post request to bungie

Parameters:

  • uri (String)
  • query (Hash) (defaults to: {})

    for post

Returns:

  • (Hashie::Mash)


127
128
129
# File 'lib/bungie_client/client.rb', line 127

def post_response(uri, query = {})
  self.class.parse_response post(uri, query)
end