Class: Summon::Transport::Headers

Inherits:
Hash
  • Object
show all
Includes:
Qstring
Defined in:
lib/summon/transport/headers.rb

Instance Method Summary collapse

Methods included from Qstring

#encode_param, #from_query_string, #to_query_string, #urldecode, #urlencode

Constructor Details

#initialize(options = {}) ⇒ Headers

Returns a new instance of Headers.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/summon/transport/headers.rb', line 10

def initialize(options = {})
  @params       = options[:params] || {}
  @url          = options[:url]
  @uri          = URI.parse(@url)
  @access_id    = options[:access_id]
  @client_key   = options[:client_key]
  @secret_key   = options[:secret_key]
  @session_id   = options[:session_id]
  @log          = Summon::Log.new(options[:log])
  @accept       = "application/#{options[:accept] || 'json'}"
  @time         = Time.now.httpdate
  validate!
  merge!({
    "Content-Type" => "application/x-www-form-urlencoded; charset=utf8",
    "Accept" => @accept,
    "x-summon-date" => @time,
    "x-summon-session-id" => @session_id,
    "Authorization" => "Summon #{[@access_id, @client_key, digest].reject {|o| o.nil?}.join(';')}",
    "Host" => "#{@uri.host}:#{@uri.port}"
  }).reject! {|k,v| v.nil?}
  @log.debug {
    "#{self.class.name}\n#{self.map {|k,v| "#{k}: #{v}"}.join("\n")}"          
  }
end

Instance Method Details

#digestObject



35
36
37
38
39
40
41
# File 'lib/summon/transport/headers.rb', line 35

def digest
  id = [@accept, @time, "#{@uri.host}:#{@uri.port}", @uri.path, qstr].join("\n") + "\n"
  Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::SHA1.new, @secret_key, id)).chomp.tap {|digest|
    # @log.debug {"ID: #{id.inspect}"}
    # @log.debug {"DIGEST: #{digest}\n"}
  }
end

#qstrObject



43
44
45
# File 'lib/summon/transport/headers.rb', line 43

def qstr
  to_query_string(@params, false)      
end