Class: GoApiClient::HttpFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/go_api_client/http_fetcher.rb

Defined Under Namespace

Classes: QuietStatusReporter, StatusReporter

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options={})
  @username = options[:username]
  @password = options[:password]
  @status_reporter = options[:status_reporter] || ENV['QUIET'] ? QuietStatusReporter.new : StatusReporter.new
end

Instance Attribute Details

#status_reporterObject (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, options={})
  uri = URI.parse(url)

  password = options[:password] || uri.password || @password
  username = options[:username] || uri.user     || @username
  params   = options[: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, options)

  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, options.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, options={})
  get(url, options).body
end