class GoApiClient::HttpFetcher

Constants

NET_HTTP_EXCEPTIONS

Attributes

response[RW]

Public Class Methods

new(options={}) click to toggle source
# File lib/go_api_client/http_fetcher.rb, line 44
def initialize(options={})
  @username = options[:username]
  @password = options[:password]
  @ssl_verify_mode = options[:ssl_verify_mode]
  @read_timeout = options[:read_timeout]
end

Public Instance Methods

failure?() click to toggle source
# File lib/go_api_client/http_fetcher.rb, line 82
def failure?
  !success?
end
status() click to toggle source
# File lib/go_api_client/http_fetcher.rb, line 74
def status
  @response.code.to_i
end
success?() click to toggle source
# File lib/go_api_client/http_fetcher.rb, line 78
def success?
  (200..299).include?(status)
end

Private Instance Methods

call(method_name, url, options, limit) click to toggle source
# File lib/go_api_client/http_fetcher.rb, line 87
def call(method_name, url, options, limit)
  raise ArgumentError, 'HTTP redirect too deep' if limit == 0
  uri = URI.parse(url)

  password = options[:password] || uri.password || @password
  username = options[:username] || uri.user || @username
  ssl_verify_mode = options[:ssl_verify_mode] || @ssl_verify_mode
  read_timeout = options[:read_timeout] || @read_timeout || 60
  params = options[:params] || {}
  headers = options[:headers] || {}

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == 'https'
  http.verify_mode = ssl_verify_mode == 0 ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
  http.read_timeout = read_timeout

  class_name = method_name.slice(0, 1).capitalize + method_name.slice(1..-1)
  method_class = "Net::HTTP::#{class_name}".split('::').inject(Object) { |n, c| n.const_get c }
  req = method_class.new(uri.request_uri)

  headers.each do |header_name, value|
    req[header_name] = value
  end

  req.basic_auth(username, password) if username || password
  if headers['Content-Type'] && headers['Content-Type'] == 'application/json'
    req.body = params.to_json
  else
    req.set_form_data(params)
  end

  response = http.request(req)
  case response
    when Net::HTTPRedirection then
      @response = self.send(method_name.to_sym, response['location'], options, limit - 1)
    else
     @response = response
  end

  @response
end