class VeezoLocationApi::Client

Constants

BASE_URI

Public Class Methods

new(tenant_id) click to toggle source
# File lib/veezo_location_api/client.rb, line 13
def initialize(tenant_id)
  @tenant_id = tenant_id
end

Public Instance Methods

last_location_by_day(occurence: nil) click to toggle source
# File lib/veezo_location_api/client.rb, line 29
def last_location_by_day(occurence: nil)
  if occurence.nil?
    occurence = Time.now.utc.strftime("%Y-%m-%d")
  end

  uri = URI(resolve_uri("/locations/last-by-day?occurence=#{occurence}"))
  get = generate_get(uri)

  request(uri, get)
end
locations_by_day(occurence: nil) click to toggle source
# File lib/veezo_location_api/client.rb, line 17
def locations_by_day(occurence: nil)
  if occurence.nil?
    occurence = Time.now.utc.strftime("%Y-%m-%d")
  end

  uri = URI(resolve_uri("/locations/all-by-day?occurence=#{occurence}"))

  get = generate_get(uri)

  request(uri, get)
end

Private Instance Methods

generate_get(uri, body = nil) click to toggle source
# File lib/veezo_location_api/client.rb, line 46
def generate_get(uri, body = nil)
  get = Net::HTTP::Get.new(uri, 'Content-Type' => 'application/json')
  get['Accept'] = 'application/json'
  get.body = body.to_json if body.is_a?(Hash)
  get
end
request(uri, request) click to toggle source
# File lib/veezo_location_api/client.rb, line 53
def request(uri, request)
  http_client = Net::HTTP.new(uri.host, uri.port).tap do |client|
    client.use_ssl = (uri.scheme == "https")
    client.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  response = http_client.request(request)
  response(response)
rescue => error
  raise(VeezoLocationApiClientError, error)
end
resolve_uri(path) click to toggle source
# File lib/veezo_location_api/client.rb, line 42
def resolve_uri(path)
  "#{BASE_URI}/api/v1/#{@tenant_id}#{path}"
end
response(response) click to toggle source
# File lib/veezo_location_api/client.rb, line 65
def response(response)
  code = response.code.to_i
  success = (code >= 200 || code < 300)

  if success
    temp_data = JSON.parse(response.body)
    data = temp_data['data'] if temp_data['data'].any?
  end

  Response.new(
    success: success,
    status_code: code,
    data: data
  )
end