Class: Rackspace::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/rackspace-apps.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_key, secret_hash, opts = {}) ⇒ Client

Returns a new instance of Client.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rackspace-apps.rb', line 18

def initialize(user_key, secret_hash, opts={})
  options = {
    :server => 'api.emailsrvr.com',
    :version_prefix => '/v0'
  }.merge! opts
  @server = options[:server]
  @version_prefix = options[:version_prefix]
  @user_key = user_key
  @secret_hash = secret_hash
  @connection = false
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



16
17
18
# File 'lib/rackspace-apps.rb', line 16

def connection
  @connection
end

Instance Method Details

#connect!Object

Connect to the remote system.



136
137
138
139
140
141
142
143
# File 'lib/rackspace-apps.rb', line 136

def connect!
  @connection = Net::HTTP.new(@server, 80)
  if @ssl
    @connection.use_ssl = true
    @connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  @connection.start
end

#connected?Boolean

Check to see if we have an HTTP/S Connection

Returns:

  • (Boolean)


129
130
131
132
133
# File 'lib/rackspace-apps.rb', line 129

def connected?
  return false unless @connection
  return false unless @connection.started?
  true
end

#delete(url_string) ⇒ Object



51
52
53
54
55
# File 'lib/rackspace-apps.rb', line 51

def delete(url_string)
  uri = full_uri(url_string)
  request = Net::HTTP::Delete.new(request_uri(uri), prepared_headers)
  http_response = make_request request, uri
end

#full_uri(url_string) ⇒ Object



97
98
99
# File 'lib/rackspace-apps.rb', line 97

def full_uri(url_string)
  URI.parse('http://' + @server + @version_prefix + url_string)
end

#get(url_string, format) ⇒ Object

HTTP Request Verbs



43
44
45
46
47
48
49
# File 'lib/rackspace-apps.rb', line 43

def get(url_string, format)
  uri = full_uri(url_string)
  headers = prepared_headers
  headers['Accept'] = format
  request = Net::HTTP::Get.new(request_uri(uri), headers)
  http_response = make_request request, uri
end

#headers_auth_creds(apiKey, secretKey) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rackspace-apps.rb', line 116

def headers_auth_creds(apiKey, secretKey)
  userAgent = 'Ruby Test Client'
  timestamp = DateTime.now.strftime('%Y%m%d%H%M%S')
  
  data_to_sign = apiKey + userAgent + timestamp + secretKey
  
  hash = Base64.encode64(Digest::SHA1.digest(data_to_sign))
  signature = apiKey + ":" + timestamp + ":" + hash
  
  headers = Hash['User-Agent' => userAgent, 'X-Api-Signature' => signature]
end

#json_formatObject



36
37
38
# File 'lib/rackspace-apps.rb', line 36

def json_format
  'application/json'
end

#make_request(request, uri) ⇒ Object

HTTP Request Helpers



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rackspace-apps.rb', line 74

def make_request request, uri
  connect! unless connected?
  response = @connection.request(request)

  case response
   when Net::HTTPOK
     if response.body.length > 0
       return JSON.parse(response.body) 
     else
       return true
     end
   when Net::HTTPForbidden
     if response['x-error-message'] =~ /Exceeded request limits/
       sleep 5
       make_request request, uri 
     else
       raise RuntimeError, "HTTP Forbidden - Are you logged in?"
     end
   else 
     raise RuntimeError, "Can't handle response #{response['x-error-message']}" 
   end
end

#post(url_string, fields_hash) ⇒ Object



64
65
66
67
68
69
# File 'lib/rackspace-apps.rb', line 64

def post(url_string, fields_hash)
  uri = full_uri(url_string)
  request = Net::HTTP::Post.new(request_uri(uri), prepared_headers)
  request.set_form_data(fields_hash)
  http_response = make_request request, uri
end

#prepared_headersObject



109
110
111
112
113
114
# File 'lib/rackspace-apps.rb', line 109

def prepared_headers
  headers = Hash.new
  headers.merge! headers_auth_creds(@user_key, @secret_hash)
  headers['Accept'] = xml_format
  headers
end

#put(url_string, fields_hash) ⇒ Object



57
58
59
60
61
62
# File 'lib/rackspace-apps.rb', line 57

def put(url_string, fields_hash)
  uri = full_uri(url_string)
  request = Net::HTTP::Put.new(request_uri(uri), prepared_headers)
  request.set_form_data(fields_hash)
  http_response = make_request request, uri
end

#request_uri(uri) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/rackspace-apps.rb', line 101

def request_uri(uri)
  request = uri.path
  if ! uri.query.nil?
    request = request + '?' + uri.query
  end
  request
end

#xml_formatObject

Response Type Enums



32
33
34
# File 'lib/rackspace-apps.rb', line 32

def xml_format
  'text/xml'
end