Class: Cloudmate::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



8
9
10
# File 'lib/cloudmate/client.rb', line 8

def initialize(options={})
  @config = Configuration.new(options)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

Instance Method Details

#configure {|config| ... } ⇒ Object

Yields:



12
13
14
# File 'lib/cloudmate/client.rb', line 12

def configure
  yield config
end

#connectionObject



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

def connection
  @connection ||= Connection.new
end

#geocode(options) ⇒ Object



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

def geocode(options)
  url = "http://geocoding.cloudmade.com/#{config.api_key}/geocoding/v2/find.js"

  options.each do |name, value|
    if value.respond_to?(:join)
      options[name] = value.join(',')
    elsif value.kind_of?(Hash)
      options[name] = value.map { |k, v| "#{k}:#{v}" }.join(';')
    end
  end

  connection.request(:get, url, options)
end

#route(options) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cloudmate/client.rb', line 34

def route(options)
  points = options.delete(:points)
  route_type = options.delete(:type) { 'car' }
  route_type_modifier = options.delete(:type_modifier)

  coords = points[1..-2].flatten.join(',')
  coords = [points.first, coords.empty? ? nil : "%5B#{coords}%5D", points.last].compact.flatten.join(',')

  url = "http://routes.cloudmade.com/#{config.api_key}/api/0.3"
  url = [url, coords, route_type, route_type_modifier].compact.join('/')
  url << '.js'

  connection.request(:get, url, options)
end