Class: Grifter::HTTPService
- Includes:
- JsonHelpers
- Defined in:
- lib/grifter/http_service.rb
Constant Summary collapse
- RequestLogSeperator =
'-'*40
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#http ⇒ Object
readonly
allow stubbing http if we are testing.
-
#last_request ⇒ Object
readonly
this is useful for testing apis, and other times you want to interrogate the http details of a response.
-
#last_response ⇒ Object
readonly
this is useful for testing apis, and other times you want to interrogate the http details of a response.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #delete(path, options = {}) ⇒ Object
-
#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).
- #get(path, options = {}) ⇒ Object
- #head(path, options = {}) ⇒ Object
-
#initialize(config) ⇒ HTTPService
constructor
A new instance of HTTPService.
- #make_headers(options) ⇒ Object
-
#make_path(path_suffix, base_uri = nil) ⇒ Object
add base uri to request.
- #options(path, options = {}) ⇒ Object
- #patch(path, obj, options = {}) ⇒ Object
- #post(path, obj, options = {}) ⇒ Object
- #post_form(path, params, options = {}) ⇒ Object
- #put(path, obj, options = {}) ⇒ Object
- #req_args(path, options) ⇒ Object
Methods included from JsonHelpers
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
#config ⇒ Object (readonly)
Returns the value of attribute config.
40 41 42 |
# File 'lib/grifter/http_service.rb', line 40 def config @config end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
40 41 42 |
# File 'lib/grifter/http_service.rb', line 40 def headers @headers end |
#http ⇒ Object (readonly)
allow stubbing http if we are testing
38 39 40 |
# File 'lib/grifter/http_service.rb', line 38 def http @http end |
#last_request ⇒ Object (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_response ⇒ Object (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 |
#name ⇒ Object (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, ={} req = Net::HTTP::Delete.new(*req_args(path, )) do_request req, 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)
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, ={} 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 .has_key? :timeout cur_timeout = @http.read_timeout Log.debug "Overriding timeout to: #{[:timeout]}" @http.read_timeout = [: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, ={} req = Net::HTTP::Get.new(*req_args(path, )) do_request req, end |
#head(path, options = {}) ⇒ Object
111 112 113 114 |
# File 'lib/grifter/http_service.rb', line 111 def head path, ={} req = Net::HTTP::Head.new(*req_args(path, )) do_request req, 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 if [:additional_headers] @headers.merge [:additional_headers] elsif [:headers] [: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 path, ={} req = Net::HTTP::Options.new(*req_args(path, )) do_request req, end |
#patch(path, obj, options = {}) ⇒ Object
138 139 140 141 142 |
# File 'lib/grifter/http_service.rb', line 138 def patch path, obj, ={} req = Net::HTTP::Patch.new(*req_args(path, )) req.body = jsonify(obj) do_request req, end |
#post(path, obj, options = {}) ⇒ Object
126 127 128 129 130 |
# File 'lib/grifter/http_service.rb', line 126 def post path, obj, ={} req = Net::HTTP::Post.new(*req_args(path, )) req.body = jsonify(obj) do_request req, 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, ={} request_obj = Net::HTTP::Post.new(*req_args(path, )) request_obj.set_form_data params do_request request_obj, end |
#put(path, obj, options = {}) ⇒ Object
132 133 134 135 136 |
# File 'lib/grifter/http_service.rb', line 132 def put path, obj, ={} req = Net::HTTP::Put.new(*req_args(path, )) req.body = jsonify(obj) do_request req, end |
#req_args(path, options) ⇒ Object
102 103 104 |
# File 'lib/grifter/http_service.rb', line 102 def req_args path, [make_path(path, [:base_uri]), make_headers()] end |