Class: GoApiClient::HttpFetcher
- Inherits:
-
Object
- Object
- GoApiClient::HttpFetcher
- Defined in:
- lib/go_api_client/http_fetcher.rb
Defined Under Namespace
Classes: QuietStatusReporter, StatusReporter
Instance Attribute Summary collapse
-
#status_reporter ⇒ Object
readonly
Returns the value of attribute status_reporter.
Instance Method Summary collapse
- #get(url, options = {}) ⇒ Object
- #get_response_body(url, options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ HttpFetcher
constructor
A new instance of HttpFetcher.
Constructor Details
#initialize(options = {}) ⇒ HttpFetcher
Returns a new instance of HttpFetcher.
26 27 28 29 30 |
# File 'lib/go_api_client/http_fetcher.rb', line 26 def initialize(={}) @username = [:username] @password = [:password] @status_reporter = [:status_reporter] || ENV['QUIET'] ? QuietStatusReporter.new : StatusReporter.new end |
Instance Attribute Details
#status_reporter ⇒ Object (readonly)
Returns the value of attribute status_reporter.
7 8 9 |
# File 'lib/go_api_client/http_fetcher.rb', line 7 def status_reporter @status_reporter end |
Instance Method Details
#get(url, options = {}) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/go_api_client/http_fetcher.rb', line 32 def get(url, ={}) uri = URI.parse(url) password = [:password] || uri.password || @password username = [:username] || uri.user || @username params = [:params] || {} uri.query = URI.encode_www_form(params) if params.any? http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = uri.scheme == 'https' status_reporter.on_request_start(uri, ) res = http.start do |http| req = Net::HTTP::Get.new(uri.request_uri) req.basic_auth(username, password) if username || password http.request(req) end case res when Net::HTTPSuccess status_reporter.on_request_success(res, uri, .merge(:url => uri)) return res end res.error! end |
#get_response_body(url, options = {}) ⇒ Object
60 61 62 |
# File 'lib/go_api_client/http_fetcher.rb', line 60 def get_response_body(url, ={}) get(url, ).body end |