Class: Grifter::HTTPService

Inherits:
Object show all
Includes:
JsonHelpers
Defined in:
lib/grifter/http_service.rb

Constant Summary collapse

RequestLogSeperator =
'-'*40

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from JsonHelpers

#jsonify, #objectify

Constructor Details

#initialize(config) ⇒ HTTPService

Returns a new instance of HTTPService.



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

def initialize config

  @config = config
  @name = config[:name]
  @base_uri = config[:base_uri]

  Log.debug "Configuring service '#{@name}' with:\n\t#{@config.inspect}"

  @http = Net::HTTP.new(@config[:hostname], @config[:port])
  @http.use_ssl = @config[:ssl]
  @http.verify_mode = OpenSSL::SSL::VERIFY_NONE if @config[:ignore_ssl_cert]
  @http.read_timeout = @config[:timeout] if @config[:timeout]
  p @config

  @headers = {
    'accept' => 'application/json',
    'content-type' => 'application/json',
  }
  if @config[:default_headers]
    Log.debug "Default headers configured: " + @config[:default_headers].inspect
    @config[:default_headers].each_pair do |k, v|
      @headers[k.to_s] = v.to_s
    end
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



40
41
42
# File 'lib/grifter/http_service.rb', line 40

def config
  @config
end

#headersObject (readonly)

Returns the value of attribute headers.



40
41
42
# File 'lib/grifter/http_service.rb', line 40

def headers
  @headers
end

#httpObject (readonly)

allow stubbing http if we are testing



38
39
40
# File 'lib/grifter/http_service.rb', line 38

def http
  @http
end

#last_requestObject (readonly)

this is useful for testing apis, and other times you want to interrogate the http details of a response



44
45
46
# File 'lib/grifter/http_service.rb', line 44

def last_request
  @last_request
end

#last_responseObject (readonly)

this is useful for testing apis, and other times you want to interrogate the http details of a response



44
45
46
# File 'lib/grifter/http_service.rb', line 44

def last_response
  @last_response
end

#nameObject (readonly)

Returns the value of attribute name.



40
41
42
# File 'lib/grifter/http_service.rb', line 40

def name
  @name
end

Instance Method Details

#delete(path, options = {}) ⇒ Object



121
122
123
124
# File 'lib/grifter/http_service.rb', line 121

def delete path, options={}
  req = Net::HTTP::Delete.new(*req_args(path, options))
  do_request req, options
end

#do_request(req, options = {}) ⇒ Object

do_request performs the actual request, and does associated logging options can include:

  • :timeout, which specifies num secs the request should timeout in (this turns out to be kind of annoying to implement)

Raises:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/grifter/http_service.rb', line 52

def do_request req, options={}
  Log.debug RequestLogSeperator
  Log.debug "#{req.class} #{req.path}"
  Log.debug "HEADERS: #{req.to_hash}"
  Log.debug "BODY:\n#{req.body}" if req.request_body_permitted?

  if options.has_key? :timeout
    cur_timeout = @http.read_timeout
    Log.debug "Overriding timeout to: #{options[:timeout]}"
    @http.read_timeout = options[:timeout]
  end

  response = @http.request(req)

  if cur_timeout
    @http.read_timeout = cur_timeout
  end

  Log.debug "RESPONSE CODE: #{response.code}"
  Log.debug "RESPONSE HEADERS: #{response.to_hash}"
  Log.debug "RESPONSE BODY:\n#{jsonify response.body}\n"

  @last_request = req
  @last_response = response

  raise RequestException.new(req, response) unless response.kind_of? Net::HTTPSuccess

  objectify response.body
end

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



106
107
108
109
# File 'lib/grifter/http_service.rb', line 106

def get path, options={}
  req = Net::HTTP::Get.new(*req_args(path, options))
  do_request req, options
end

#head(path, options = {}) ⇒ Object



111
112
113
114
# File 'lib/grifter/http_service.rb', line 111

def head path, options={}
  req = Net::HTTP::Head.new(*req_args(path, options))
  do_request req, options
end

#make_headers(options) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/grifter/http_service.rb', line 92

def make_headers options
  if options[:additional_headers]
    @headers.merge options[:additional_headers]
  elsif options[:headers]
    options[:headers]
  else
    @headers
  end
end

#make_path(path_suffix, base_uri = nil) ⇒ Object

add base uri to request



83
84
85
86
87
88
89
90
# File 'lib/grifter/http_service.rb', line 83

def make_path path_suffix, base_uri=nil
  base_uri_to_use = base_uri ? base_uri : @base_uri
  if base_uri_to_use
    base_uri_to_use + path_suffix
  else
    path_suffix
  end
end

#options(path, options = {}) ⇒ Object



116
117
118
119
# File 'lib/grifter/http_service.rb', line 116

def options path, options={}
  req = Net::HTTP::Options.new(*req_args(path, options))
  do_request req, options
end

#patch(path, obj, options = {}) ⇒ Object



138
139
140
141
142
# File 'lib/grifter/http_service.rb', line 138

def patch path, obj, options={}
  req = Net::HTTP::Patch.new(*req_args(path, options))
  req.body = jsonify(obj)
  do_request req, options
end

#post(path, obj, options = {}) ⇒ Object



126
127
128
129
130
# File 'lib/grifter/http_service.rb', line 126

def post path, obj, options={}
  req = Net::HTTP::Post.new(*req_args(path, options))
  req.body = jsonify(obj)
  do_request req, options
end

#post_form(path, params, options = {}) ⇒ Object



144
145
146
147
148
# File 'lib/grifter/http_service.rb', line 144

def post_form path, params, options={}
  request_obj = Net::HTTP::Post.new(*req_args(path, options))
  request_obj.set_form_data params
  do_request request_obj, options
end

#put(path, obj, options = {}) ⇒ Object



132
133
134
135
136
# File 'lib/grifter/http_service.rb', line 132

def put path, obj, options={}
  req = Net::HTTP::Put.new(*req_args(path, options))
  req.body = jsonify(obj)
  do_request req, options
end

#req_args(path, options) ⇒ Object



102
103
104
# File 'lib/grifter/http_service.rb', line 102

def req_args path, options
  [make_path(path, options[:base_uri]), make_headers(options)]
end