class Masheri::RestClient::Query

Public Class Methods

new(options) click to toggle source
# File lib/masheri/rest_client/query.rb, line 4
def initialize(options)
  @options  = options
end

Public Instance Methods

check_dates!() click to toggle source
# File lib/masheri/rest_client/query.rb, line 38
def check_dates!
  if @options[:end_date] - @options[:start_date] > 7.days
    raise InvalidDateRange.new(@options[:start_date], @options[:end_date])
  end
end
check_params!() click to toggle source
# File lib/masheri/rest_client/query.rb, line 32
def check_params!
  raise QueryParamMissing.new("resource")   if @options[:resource].blank?
  raise QueryParamMissing.new("end_date")   if @options[:end_date].blank?
  raise QueryParamMissing.new("start_date") if @options[:start_date].blank?
end
config() click to toggle source
# File lib/masheri/rest_client/query.rb, line 58
def config
  Masheri.config
end
end_date() click to toggle source
# File lib/masheri/rest_client/query.rb, line 28
def end_date
  @options[:end_date].strftime("%Y-%m-%dT00:00:00Z")
end
format() click to toggle source
# File lib/masheri/rest_client/query.rb, line 16
def format
  @options[:format] || "csv"
end
limit() click to toggle source
# File lib/masheri/rest_client/query.rb, line 20
def limit
  @options[:limit] || 1000
end
params() click to toggle source
# File lib/masheri/rest_client/query.rb, line 44
def params
  check_params!
  check_dates!

  params = {
    start_date: start_date,
    end_date:   end_date,
    format:     format,
    limit:      limit,
    apikey:     config.key,
    sig:        config.signature
  }
end
query_params() click to toggle source
# File lib/masheri/rest_client/query.rb, line 62
def query_params
  URI.encode_www_form(params).gsub("%3A", ":")
end
resource() click to toggle source
# File lib/masheri/rest_client/query.rb, line 12
def resource
  @options[:resource]
end
rest_path() click to toggle source
# File lib/masheri/rest_client/query.rb, line 66
def rest_path
  "/v2/rest/#{config.site_id}/reports/calls/#{resource}/service/#{service_id}"
end
service_id() click to toggle source
# File lib/masheri/rest_client/query.rb, line 8
def service_id
  @options[:service_id]
end
start_date() click to toggle source
# File lib/masheri/rest_client/query.rb, line 24
def start_date
  @options[:start_date].strftime("%Y-%m-%dT00:00:00Z")
end
url() click to toggle source
# File lib/masheri/rest_client/query.rb, line 70
def url
  uri        = URI::HTTP.build(host: config.host, path: rest_path, query: query_params)
  uri.scheme = "https"
  uri.to_s
end