Class: Server

Inherits:
Object
  • Object
show all
Includes:
Test::Unit::Assertions
Defined in:
lib/racked/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(server = 'api.emailsrvr.com', version_prefix = '/v0', user_key = 'xxxxxxxxxxxxxxxxxxxx', secret_hash = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') ⇒ Server

Returns a new instance of Server.



12
13
14
15
16
17
# File 'lib/racked/server.rb', line 12

def initialize(server='api.emailsrvr.com', version_prefix='/v0', user_key='xxxxxxxxxxxxxxxxxxxx', secret_hash='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
  @server = server
  @version_prefix = version_prefix
  @user_key = user_key
  @secret_hash = secret_hash
end

Instance Method Details

#delete(url_string, format) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/racked/server.rb', line 40

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

#full_uri(url_string) ⇒ Object



83
84
85
# File 'lib/racked/server.rb', line 83

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

#get(url_string, format) ⇒ Object

HTTP Request Verbs



32
33
34
35
36
37
38
# File 'lib/racked/server.rb', line 32

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



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/racked/server.rb', line 103

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



25
26
27
# File 'lib/racked/server.rb', line 25

def json_format
  'application/json'
end

#make_request(request, uri) ⇒ Object

HTTP Request Helpers



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/racked/server.rb', line 69

def make_request request, uri
  response = Net::HTTP::start(uri.host, uri.port)  do |http|
    begin
      http.request request
    rescue Exception => e
      puts e.message
      puts e.backtrace.inspect
      retry
    end
  end
  
  response
end

#post(url_string, fields_hash, format) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/racked/server.rb', line 57

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

#prepared_headersObject



96
97
98
99
100
101
# File 'lib/racked/server.rb', line 96

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, format) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/racked/server.rb', line 48

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

#request_uri(uri) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/racked/server.rb', line 87

def request_uri uri
  request = uri.path
  if ! uri.query.nil?
    request = request + '?' + uri.query
  end
  p [:request_uri, request]
  request
end

#xml_formatObject

Response Type Enums



21
22
23
# File 'lib/racked/server.rb', line 21

def xml_format
  'text/xml'
end