Class: Io::Flow::V0::HttpClient::DefaultHttpHandlerInstance

Inherits:
HttpHandlerInstance show all
Defined in:
lib/flow_commerce/flow_api_v0_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_uri) ⇒ DefaultHttpHandlerInstance

Returns a new instance of DefaultHttpHandlerInstance.



64583
64584
64585
64586
64587
64588
64589
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 64583

def initialize(base_uri)
  @base_uri = Preconditions.assert_class('base_uri', base_uri, URI)
  @client = Net::HTTP.new(@base_uri.host, @base_uri.port)
  if @base_uri.scheme == "https"
    configure_ssl
  end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



64581
64582
64583
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 64581

def client
  @client
end

Instance Method Details

#configure_sslObject

Called to configure SSL if the base uri requires it



64612
64613
64614
64615
64616
64617
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 64612

def configure_ssl
  @client.use_ssl = true
  @client.verify_mode = OpenSSL::SSL::VERIFY_PEER
  @client.cert_store = OpenSSL::X509::Store.new
  @client.cert_store.set_default_paths
end

#execute(request) ⇒ Object



64591
64592
64593
64594
64595
64596
64597
64598
64599
64600
64601
64602
64603
64604
64605
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 64591

def execute(request)
  response = begin
               @client.request(request)
             rescue SocketError => e
               raise StandardError.new("Error accessing uri[#{full_uri(request.path)}]: #{e}")
             end

  case response
  when Net::HTTPSuccess
    response.body
  else
    body = response.body rescue nil
    raise HttpClient::ServerError.new(response.code.to_i, response.message, :body => body, :uri => full_uri(request.path).to_s)
  end
end

#full_uri(path) ⇒ Object



64607
64608
64609
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 64607

def full_uri(path)
  path.start_with?(@base_uri.to_s) ? path : File.join(@base_uri.to_s, path)
end