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
# 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]

  @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.



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

def config
  @config
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#httpObject (readonly)

allow stubbing http if we are testing



36
37
38
# File 'lib/grifter/http_service.rb', line 36

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



42
43
44
# File 'lib/grifter/http_service.rb', line 42

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



42
43
44
# File 'lib/grifter/http_service.rb', line 42

def last_response
  @last_response
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

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



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

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

#do_request(req) ⇒ Object

Raises:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/grifter/http_service.rb', line 45

def do_request req
  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?
  response = @http.request(req)
  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



87
88
89
90
# File 'lib/grifter/http_service.rb', line 87

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

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



92
93
94
95
# File 'lib/grifter/http_service.rb', line 92

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

#make_headers(options) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/grifter/http_service.rb', line 73

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



64
65
66
67
68
69
70
71
# File 'lib/grifter/http_service.rb', line 64

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



97
98
99
100
# File 'lib/grifter/http_service.rb', line 97

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

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



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

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

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



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

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

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



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

def post_form path, params, options={}
  request_obj = Net::HTTP::Post.new(*req_args(path, options))
  request_obj.set_form_data params
  request_obj.basic_auth(options[:username], options[:password]) if options[:username]
  do_request request_obj
end

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



113
114
115
116
117
# File 'lib/grifter/http_service.rb', line 113

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

#req_args(path, options) ⇒ Object



83
84
85
# File 'lib/grifter/http_service.rb', line 83

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