Class: Summon::Transport::Http

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

Constant Summary collapse

DEFAULTS =
{:url => "http://api.summon.serialssolutions.com"}

Instance Method Summary collapse

Methods included from Qstring

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

Constructor Details

#initialize(options = {:url => nil, :access_id => nil, :secret_key => nil, :client_key => nil, :session_id => nil, :log => nil}) ⇒ Http

Returns a new instance of Http.



10
11
12
13
14
15
16
17
18
19
# File 'lib/summon/transport/http.rb', line 10

def initialize(options = {:url => nil, :access_id => nil, :secret_key => nil, :client_key => nil, :session_id => nil, :log => nil})
  @options    = DEFAULTS.merge options
  @access_id  = @options[:access_id]
  @secret_key = @options[:secret_key]
  @client_key = @options[:client_key]
  @session_id = @options[:session_id] || "SUMMON-SESSION-#{Socket.gethostname}-#{$$}-#{sidalloc}"
  @url        = @options[:url]
  @log        = Summon::Log.new(options[:log])
  @benchmark  = @options[:benchmark] || Summon::Service::Pass.new
end

Instance Method Details

#error(response) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/summon/transport/http.rb', line 92

def error(response)
  case response.content_type
  when "application/json"
    JSON.parse(response.body)["errors"].first["message"]
  else
    status(response)
  end
end

#get(path, params = {}) ⇒ Object



21
22
23
24
25
# File 'lib/summon/transport/http.rb', line 21

def get(path, params = {})
  session_id = params["s.session.id"]
  params.delete "s.session.id"
  urlget "#{@url}#{path}?#{to_query_string(params, true)}", params, session_id
end

#parse(response) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/summon/transport/http.rb', line 75

def parse(response)
  case response.content_type
  when "application/json"
    JSON.parse(response.body).tap do |json|
      @log.debug("ruby-summon::transport") { "JSON RESPONSE: #{json.inspect}" }
    end
  
  when "text/plain"
    response.body.tap do |text|
      @log.debug("ruby-summon::transport") { "TEXT RESPONSE: #{text.inspect}" }
    end
    
  else
    raise ServiceError, "service returned unexpected #{response.content_type} : #{response.body}"
  end
end

#status(response) ⇒ Object



101
102
103
# File 'lib/summon/transport/http.rb', line 101

def status(response)
  "#{response.code}: #{response.message}"
end

#urlget(url, params = nil, session_id = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/summon/transport/http.rb', line 27

def urlget(url, params = nil, session_id = nil)
  @benchmark.report("total:") do
    uri = URI.parse url
    params ||= from_query_string(uri.query)      
    session_id ||= @session_id
    headers = @benchmark.report("calculate headers") do
      Headers.new(
        :url => url,
        :params => params,
        :access_id => @access_id,
        :secret_key => @secret_key,
        :client_key => @client_key,
        :session_id => session_id,
        :log => @log
      )
    end
    @log.info("ruby-summon:transport") {
      "GET: #{url}"
    }
    result = nil
      http = Net::HTTP.new(uri.host, uri.port)
      http.start do
        get = Net::HTTP::Get.new("#{uri.path}#{'?' + uri.query if uri.query && uri.query != ''}")
        get.merge! headers
        response = @benchmark.report("http request") do
          http.request(get)
        end
        case response
          when Net::HTTPSuccess
            @benchmark.report("parse response") do
              result = parse(response)
            end
          when Net::HTTPUnauthorized
            raise AuthorizationError, status(response)
          when Net::HTTPClientError
            raise RequestError, error(response)
          when Net::HTTPServerError
            raise ServiceError, error(response)
          else
            raise UnknownResponseError, "Unknown response: #{response}"
        end
      end

    ###
    result
  end
end