Class: MaponClient::Client

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

Constant Summary collapse

DEFAULT_BASE_URL =
'https://mapon.com/api/v1/'.freeze
JSON =
'json'.freeze
XML =
'xml'.freeze
STRUCTURE =
'structure'.freeze
OBJECT =
'object'.freeze
AVAILABLE_FORMATS =
[JSON, XML, STRUCTURE, OBJECT].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, base_url: nil, format: nil) ⇒ Client

Returns a new instance of Client.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mapon_client/client.rb', line 17

def initialize(api_key: nil, base_url: nil, format: nil)
  @api_key  = api_key  || ENV['MAPON_CLIENT_API_KEY']
  @base_url = base_url || DEFAULT_BASE_URL
  @format   = format   || JSON

  set_request_format
  set_parser_class

  @resource_base ||= RestClient::Resource.new(
    @base_url, headers: { params: { key: @api_key } }
  )
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



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

def base_url
  @base_url
end

#formatObject (readonly)

Returns the value of attribute format.



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

def format
  @format
end

#parser_classObject (readonly)

Returns the value of attribute parser_class.



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

def parser_class
  @parser_class
end

#request_formatObject (readonly)

Returns the value of attribute request_format.



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

def request_format
  @request_format
end

#resource_baseObject (readonly)

Returns the value of attribute resource_base.



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

def resource_base
  @resource_base
end

Instance Method Details

#[](*args) ⇒ Object



30
31
32
33
34
35
# File 'lib/mapon_client/client.rb', line 30

def [](*args)
  @resource_base.public_send(
    :[],
    *append_format_to_url(*args)
  )
end

#append_format_to_url(*args) ⇒ Object



49
50
51
52
# File 'lib/mapon_client/client.rb', line 49

def append_format_to_url(*args)
  suburl = "#{args.shift}.#{@request_format}"
  args.unshift(suburl)
end

#with_key(params) ⇒ Object



45
46
47
# File 'lib/mapon_client/client.rb', line 45

def with_key(params)
  params.merge(key: @api_key)
end