module FacturapiRuby::HttpClient

Constants

BASE_URL

Public Class Methods

delete(options) click to toggle source
# File lib/facturapi_ruby/http_client.rb, line 55
def delete(options)
  response = request(Net::HTTP::Delete, options)

  json_response(response)
end
get(options) click to toggle source
# File lib/facturapi_ruby/http_client.rb, line 33
def get(options)
  response = request(Net::HTTP::Get, options)

  json_response(response)
end
get_file(options) click to toggle source
# File lib/facturapi_ruby/http_client.rb, line 19
def get_file(options)
  response = request(Net::HTTP::Get, options)

  file = Tempfile.open(['my', options[:file_ext]])
  file.binmode
  file.write(response.body)

  if response.code.start_with? '20'
    file
  else
    raise FacturapiRubyError.new(JSON.parse(response.body))
  end
end
post(options) click to toggle source
# File lib/facturapi_ruby/http_client.rb, line 39
def post(options)
  response = request(Net::HTTP::Post, options) do |request|
    request.body = options[:api_options].to_json
  end
  
  json_response(response)
end
put(options) click to toggle source
# File lib/facturapi_ruby/http_client.rb, line 47
def put(options)
  response = request(Net::HTTP::Put, options) do |request|
    request.body = options[:api_options].to_json
  end

  json_response(response)
end

Private Class Methods

json_response(response) click to toggle source
# File lib/facturapi_ruby/http_client.rb, line 63
def json_response(response)
  if response.code.start_with? '20'
    JSON.parse(response.body)
  else
    raise FacturapiRubyError.new(JSON.parse(response.body))
  end
end
request(request_method, options) { |request| ... } click to toggle source
# File lib/facturapi_ruby/http_client.rb, line 71
def request(request_method, options)
  uri = URI(BASE_URL + options[:endpoint])

  http         = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  request      = request_method.new(uri.request_uri, 'Content-Type' => 'application/json')
  # request.body = options[:api_options].to_json

  yield(request) if block_given?

  request.basic_auth(FacturapiRuby.configuration.api_key, '')

  http.set_debug_output($stdout) if FacturapiRuby.configuration.debug_output

  http.request(request)
end