class Utilities::ApiClient

Constants

BASE_URL

Public Class Methods

new(api_user, api_key) click to toggle source
# File lib/utilities/api_client.rb, line 9
def initialize(api_user, api_key)
  @headers = {
    'X-API-USER': api_user,
    'X-API-KEY': api_key,
    'content-type': 'text/json'
  }
  @uri = nil
  @req = nil
  @res = nil
end

Public Instance Methods

call(method, path, params) click to toggle source
# File lib/utilities/api_client.rb, line 20
def call(method, path, params)
  @uri = URI.parse("#{BASE_URL}#{path}")
  case method
  when :get
    get(params)
  when :post
    post(params)
  else
    {code: 405, error: "Method not allowed"}
  end
end

Private Instance Methods

call!() click to toggle source
# File lib/utilities/api_client.rb, line 45
def call!
  @headers.each do |k, v|
    @req.add_field "#{k}", "#{v}" 
  end

  @res = HTTP.start(@uri.hostname, @uri.port, use_ssl: true) {|http|
    http.request(@req)
  }

  result = JSON.parse(@res.body)
  result[:http_code] = @res.code

  result
end
get(params) click to toggle source
# File lib/utilities/api_client.rb, line 33
def get(params)
  @uri.query = URI.encode_www_form(params)
  @req = HTTP::Get.new(@uri)
  call!
end
post(params) click to toggle source
# File lib/utilities/api_client.rb, line 39
def post(params)
  @req = HTTP::Post.new(@uri)
  @req.set_form(params)
  call!
end