Class: BungieClient::Wrapper

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Wrapper

Initialize wrapper with client

This initializer create wrapper object with client. If you ‘options` contain `:client` key, it will be taken as client object [BungieClient::Client]. Otherwise it will be passed in client initializer.

Parameters:

  • client (BungieClient::Client)

    initialized for wrapper

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

    for initialization of client

See Also:



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

def initialize(options = {})
  if options[:client].nil?
    @options = options
  elsif options[:client].is_a? BungieClient::Client
    @client = options[:client]
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object



68
69
70
# File 'lib/bungie_client/wrapper.rb', line 68

def method_missing(*args)
  call_service args[0].to_s, args[1] || {}, args[2] || {}, args[3] || {}
end

Instance Method Details

#call_service(service, params = {}, options = {}, headers = {}) ⇒ Hashie::Mash

Call needed service from services list

Parameters:

  • service (String)

    name in snake case

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

    service parameters

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

    for client request (get/post)

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

Returns:

  • (Hashie::Mash)


54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bungie_client/wrapper.rb', line 54

def call_service(service, params = {}, options = {}, headers = {})
  service = begin
    BungieClient::Service.new service
  rescue StandardError
    raise NoMethodError
  end

  # change url
  url = fill_url service.endpoint, params

  # send request
  client.send service.type, url, options, headers
end

#clientBungieClient::Client

Get wrapper client



26
27
28
29
30
# File 'lib/bungie_client/wrapper.rb', line 26

def client
  return @client unless @client.nil?

  @client = BungieClient::Client.new @options
end

#fill_url(url, params) ⇒ String

Change all url parameters to hash value

Parameters:

Returns:



38
39
40
41
42
43
44
# File 'lib/bungie_client/wrapper.rb', line 38

def fill_url(url, params)
  params.each do |key, value|
    url = url.gsub "{#{key}}", value.to_s
  end

  url
end