Class: CloudMade::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudmade/connection.rb

Overview

This Connection class provides a connection to CloudMade online services (HTTP API). Normally you don’t need to create it directly. Construct a ‘Client’ from parameters instead.

Connection examples:

conn = CloudMade::Connection.new(‘cloudmade.com’, 80, ‘FAKE_API_KEY’) CloudMade.Client.new(conn)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key = nil, base_url = 'cloudmade.com', port = nil) ⇒ Connection

Initializes connection

  • base_url should not start with ‘www’

  • port integer value of port for CloudMade portal, if nil then default 80 port is used

  • api_key your API key to connect to CloudMade services



35
36
37
38
39
40
# File 'lib/cloudmade/connection.rb', line 35

def initialize(api_key = nil, base_url = 'cloudmade.com', port = nil)
  self.api_key = api_key
  self.base_url = base_url
  self.base_url = 'cloudmade.com' if self.base_url == nil or self.base_url.empty?
  self.port = port
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



29
30
31
# File 'lib/cloudmade/connection.rb', line 29

def api_key
  @api_key
end

#base_urlObject

Returns the value of attribute base_url.



27
28
29
# File 'lib/cloudmade/connection.rb', line 27

def base_url
  @base_url
end

#portObject

Port number of this Connection



65
66
67
68
# File 'lib/cloudmade/connection.rb', line 65

def port
  return 80 if @port == nil
  @port
end

Instance Method Details

#connect(server_url, request) ⇒ Object

Make a HTTP connection and send a request. Called by the cloudmade ‘Client’ object internally



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cloudmade/connection.rb', line 43

def connect(server_url, request)
  #sputs "#{server_url} #{request}"
  result = nil
  Net::HTTP.start(server_url, self.port) {|http|
    req = Net::HTTP::Get.new("#{request}")
    response = http.request(req)
    case response
    when Net::HTTPSuccess, Net::HTTPRedirection
      result = response.body
    else
      raise HTTPError.new("Couldn't read data. HTTP status: #{response}")
    end
  }
  return result
end

#urlObject

Convenience method. Return the base URL and port of this Connection



60
61
62
# File 'lib/cloudmade/connection.rb', line 60

def url
  return "#{@base_url}#{@port != nil ? ':' + port.to_s : ''}"
end