module Masheri class RestClient class Query def initialize(options) @options = options end def service_id @options[:service_id] end def resource @options[:resource] end def format @options[:format] || "csv" end def limit @options[:limit] || 1000 end def start_date @options[:start_date].strftime("%Y-%m-%dT00:00:00Z") end def end_date @options[:end_date].strftime("%Y-%m-%dT00:00:00Z") end 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 def check_dates! if @options[:end_date] - @options[:start_date] > 7.days raise InvalidDateRange.new(@options[:start_date], @options[:end_date]) end end 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 def config Masheri.config end def query_params URI.encode_www_form(params).gsub("%3A", ":") end def rest_path "/v2/rest/#{config.site_id}/reports/calls/#{resource}/service/#{service_id}" end def url uri = URI::HTTP.build(host: config.host, path: rest_path, query: query_params) # TODO: Waiting on support from Mashery # uri.scheme = "https" uri.to_s end end end end