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.



28639
28640
28641
28642
28643
28644
28645
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 28639

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.



28637
28638
28639
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 28637

def client
  @client
end

Instance Method Details

#configure_sslObject

Called to configure SSL if the base uri requires it



28668
28669
28670
28671
28672
28673
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 28668

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



28647
28648
28649
28650
28651
28652
28653
28654
28655
28656
28657
28658
28659
28660
28661
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 28647

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



28663
28664
28665
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 28663

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