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)

    with [HTTP::Cookie]

  • :cache (BungieClient::Cache)

    client for saving respones

  • :authentication (Boolean)

    makes authentication with next three field for getting cookies for private requests

  • :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:



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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# 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

  # init @cache

  unless options[:cache].nil?
    if options[:cache].is_a? BungieClient::Cache
      @cache = options[:cache]
    else
      raise 'Cache client must be inhereted from [BungieClient::Cache].'
    end
  end

  # init @agent

  @agent = Mechanize.new

  # make authentication

  if options[:authentication]
    @username = options[:username]  if options[:username].is_a? String
    @password = options[:password]  if options[:password].is_a? String
    @type     = options[:type].to_s if ['psn', 'live'].include? options[:type].to_s

    cookies = BungieClient::Auth.auth @username, @password, (@type || 'psn')

    if cookies.nil?
      raise "Wrong authentication. Check your account data."
    else
      @agent.cookie_jar = cookies
    end
  end

  # merge cookies with options

  if BungieClient::Auth.valid_cookies? options[:cookies], true
    cookies = (options[:cookies].is_a? Array) ? cookies : cookies.cookies

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

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



18
19
20
# File 'lib/bungie_client/client.rb', line 18

def agent
  @agent
end

#api_keyObject (readonly)

Returns the value of attribute api_key.



14
15
16
# File 'lib/bungie_client/client.rb', line 14

def api_key
  @api_key
end

#cacheObject (readonly)

Returns the value of attribute cache.



19
20
21
# File 'lib/bungie_client/client.rb', line 19

def cache
  @cache
end

#passwordObject (readonly)

Returns the value of attribute password.



16
17
18
# File 'lib/bungie_client/client.rb', line 16

def password
  @password
end

#typeObject (readonly)

Returns the value of attribute type.



17
18
19
# File 'lib/bungie_client/client.rb', line 17

def type
  @type
end

#usernameObject (readonly)

Returns the value of attribute username.



15
16
17
# File 'lib/bungie_client/client.rb', line 15

def username
  @username
end

Class Method Details

.request_uri(uri) ⇒ String

Form uri for requests

Parameters:

  • uri (String)

Returns:

  • (String)


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

#allow_get_cache(options = {}) ⇒ Boolean

Check options for allowing getting of cache

Parameters:

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

    of cache

Returns:

  • (Boolean)


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

def allow_get_cache(options = {})
  allow_set_cache(options) && options[:cache_rewrite] != true
end

#allow_set_cache(options = {}) ⇒ Boolean

Check options for allowing setting of cache

Parameters:

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

    of cache

Returns:

  • (Boolean)


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

def allow_set_cache(options = {})
  !@cache.nil? && options[:cache_none] != true
end

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

Get response from bungie services

Parameters:

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

    for http-query

Returns:

  • (String|nil)

See Also:



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

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

#get_response(uri, parameters = {}, options = {}) ⇒ Array|Hash|nil

Get Response field after get request to bungie

Parameters:

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

    for http-query

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

    for cache such as:

Options Hash (options):

  • :cache_none (Boolean)
    • disable response caching

  • :cache_rewrite (Boolean)
    • update cache value

  • :cache_ttl (Integer)
    • special cache ttl

Returns:

  • (Array|Hash|nil)


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/bungie_client/client.rb', line 122

def get_response(uri, parameters = {}, options = {})
  if allow_get_cache options
    result = @cache.get "#{uri}+#{parameters}"

    return result unless result.nil?
  end

  result = get uri, parameters

  if !result.nil? && result != ''
    result = JSON.parse result

    if !result['Response'].nil? && !result['ErrorCode'].nil? && result['ErrorCode'] == 1
      @cache.set "#{uri}+#{parameters}", result['Response'], options[:cache_ttl] if allow_set_cache options

      result['Response']
    end
  end
end

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

Post data to bungie services

Parameters:

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

Returns:

  • (String|nil)


148
149
150
# File 'lib/bungie_client/client.rb', line 148

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