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) ⇒ Object
- #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.
- #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 |
# 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
#config ⇒ Object (readonly)
Returns the value of attribute config.
38 39 40 |
# File 'lib/grifter/http_service.rb', line 38 def config @config end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
38 39 40 |
# File 'lib/grifter/http_service.rb', line 38 def headers @headers end |
#http ⇒ Object (readonly)
allow stubbing http if we are testing
36 37 38 |
# File 'lib/grifter/http_service.rb', line 36 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
42 43 44 |
# File 'lib/grifter/http_service.rb', line 42 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
42 43 44 |
# File 'lib/grifter/http_service.rb', line 42 def last_response @last_response end |
#name ⇒ Object (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
97 98 99 100 |
# File 'lib/grifter/http_service.rb', line 97 def delete path, ={} req = Net::HTTP::Delete.new(*req_args(path, )) do_request req end |
#do_request(req) ⇒ Object
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, ={} req = Net::HTTP::Get.new(*req_args(path, )) do_request req end |
#head(path, options = {}) ⇒ Object
92 93 94 95 |
# File 'lib/grifter/http_service.rb', line 92 def head path, ={} req = Net::HTTP::Head.new(*req_args(path, )) 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 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
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 |
#post(path, obj, options = {}) ⇒ Object
102 103 104 105 106 |
# File 'lib/grifter/http_service.rb', line 102 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
114 115 116 117 118 119 |
# File 'lib/grifter/http_service.rb', line 114 def post_form path, params, ={} request_obj = Net::HTTP::Post.new(*req_args(path, )) request_obj.set_form_data params request_obj.basic_auth([:username], [:password]) if [:username] do_request request_obj end |
#put(path, obj, options = {}) ⇒ Object
108 109 110 111 112 |
# File 'lib/grifter/http_service.rb', line 108 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
83 84 85 |
# File 'lib/grifter/http_service.rb', line 83 def req_args path, [make_path(path, [:base_uri]), make_headers()] end |