Class: APIGeoloc::Requester
- Inherits:
-
Object
- Object
- APIGeoloc::Requester
- Defined in:
- lib/apigeoloc/requester.rb
Overview
This is a Net HTTP requester class.
Instance Method Summary collapse
-
#basic_auth(request) ⇒ Object
Set the authentication data.
-
#format_header(request) ⇒ Object
Set the headers given as argument.
-
#initialize(method, url, opts) ⇒ Requester
constructor
Class constructor method.
-
#request ⇒ Object
Select request method.
-
#rest_request_get ⇒ Object
GET method.
-
#rest_request_post ⇒ Object
POST method.
Constructor Details
#initialize(method, url, opts) ⇒ Requester
Class constructor method
25 26 27 28 29 30 31 32 |
# File 'lib/apigeoloc/requester.rb', line 25 def initialize(method, url, opts) @method = method @uri = URI.parse(url) @opts = opts @http = Net::HTTP.new(@uri.host, @uri.port) @http.use_ssl = @uri.scheme.eql?('https') @http.verify_mode = OpenSSL::SSL::VERIFY_NONE unless @opts['insecure'] end |
Instance Method Details
#basic_auth(request) ⇒ Object
Set the authentication data
35 36 37 38 39 40 |
# File 'lib/apigeoloc/requester.rb', line 35 def basic_auth(request) unless @opts['user'].nil? || @opts['password'].nil? request.basic_auth(@opts['user'], @opts['password']) end request end |
#format_header(request) ⇒ Object
Set the headers given as argument
43 44 45 46 47 48 49 50 51 |
# File 'lib/apigeoloc/requester.rb', line 43 def format_header(request) unless @opts['header'].nil? @opts['header'].split(',').each do |h| (key, value) = h.split(':') request[key] = value end end request end |
#request ⇒ Object
Select request method
54 55 56 57 58 59 |
# File 'lib/apigeoloc/requester.rb', line 54 def request case @method.upcase when 'GET' then rest_request_get when 'POST' then rest_request_post end end |
#rest_request_get ⇒ Object
GET method
62 63 64 65 66 |
# File 'lib/apigeoloc/requester.rb', line 62 def rest_request_get req = Net::HTTP::Get.new(@uri.request_uri) req = format_header(req) @http.request(req) end |
#rest_request_post ⇒ Object
POST method
69 70 71 72 73 74 |
# File 'lib/apigeoloc/requester.rb', line 69 def rest_request_post req = Net::HTTP::Post.new(@uri.request_uri) req = format_header(req) req = basic_auth(req) @http.request(req) end |