Class: Hoth::Transport::HttpHmac

Inherits:
Http
  • Object
show all
Defined in:
lib/hoth/transport/http_hmac.rb

Direct Known Subclasses

HttpsHmac

Instance Attribute Summary collapse

Attributes inherited from Base

#encoder

Instance Method Summary collapse

Methods inherited from Http

#call_remote_with, #handle_response

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Hoth::Transport::Base

Instance Attribute Details

#hmac_access_idObject

Returns the value of attribute hmac_access_id.



8
9
10
# File 'lib/hoth/transport/http_hmac.rb', line 8

def hmac_access_id
  @hmac_access_id
end

#hmac_secretObject

Returns the value of attribute hmac_secret.



8
9
10
# File 'lib/hoth/transport/http_hmac.rb', line 8

def hmac_secret
  @hmac_secret
end

#reqObject

just for testing request



10
11
12
# File 'lib/hoth/transport/http_hmac.rb', line 10

def req
  @req
end

Instance Method Details

#post_payload(payload, use_ssl = false) ⇒ Object

Manually construct the HTTP request because it needs to be signed with hmac before. This function is more or less a copy of HTTP.post_form

Prerequisites

you must set both instance variables: hmac_access_id, hmac_secret before the call can be made

Returns

<Object>:. Net::HTTPResponse

Raises:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/hoth/transport/http_hmac.rb', line 19

def post_payload(payload, use_ssl=false)
  raise TransportError, 'no hmac credentials set for hmac transport' unless hmac_access_id && hmac_secret
  uri = URI.parse(self.endpoint.to_url)
  # construct request object
  self.req = Net::HTTP::Post.new(uri.path)
  # add its form data
  self.req.form_data = {'name'   => self.name.to_s,
                        'params' => encoder.encode(payload) }
  # ensure a date header is set, needed for hmac
  self.req['Date'] = Time.now.httpdate if self.req['Date'].nil?
  # sign request object => set Authorisation header
  KingHmac::Auth.sign!(self.req, hmac_access_id, hmac_secret)
  #create request
  request = Net::HTTP.new(uri.host, uri.port)
  request.use_ssl = use_ssl
  response = request.start {|http| http.request(self.req) }
  case response
  when Net::HTTPSuccess, Net::HTTPRedirection
    response
  else
    response.error!
  end
end