Class: MaponClient::Client

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

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.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mapon_client/client.rb', line 8

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

  @base_url = (
    base_url || MaponClient::DEFAULT_BASE_URL
  )

  @format = (
    format || MaponClient::DEFAULT_FORMAT
  )

  @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.



6
7
8
# File 'lib/mapon_client/client.rb', line 6

def api_key
  @api_key
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



6
7
8
# File 'lib/mapon_client/client.rb', line 6

def base_url
  @base_url
end

#formatObject (readonly)

Returns the value of attribute format.



6
7
8
# File 'lib/mapon_client/client.rb', line 6

def format
  @format
end

#resource_baseObject (readonly)

Returns the value of attribute resource_base.



6
7
8
# File 'lib/mapon_client/client.rb', line 6

def resource_base
  @resource_base
end

Instance Method Details

#[](*args) ⇒ Object



26
27
28
29
30
31
# File 'lib/mapon_client/client.rb', line 26

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

#append_format_to_url(*args) ⇒ Object



41
42
43
44
# File 'lib/mapon_client/client.rb', line 41

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

#resourcesObject



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

def resources
  @resources ||= Resources.new(self)
end

#with_key(params) ⇒ Object



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

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