Class: Masheri::RestClient::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/masheri/rest_client/query.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Query

Returns a new instance of Query.



4
5
6
# File 'lib/masheri/rest_client/query.rb', line 4

def initialize(options)
  @options  = options
end

Instance Method Details

#check_dates!Object



29
30
31
32
33
# File 'lib/masheri/rest_client/query.rb', line 29

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!Object

Raises:



24
25
26
27
# File 'lib/masheri/rest_client/query.rb', line 24

def check_params!
  raise QueryParamMissing.new("end_date")   if @options[:end_date].blank?
  raise QueryParamMissing.new("start_date") if @options[:start_date].blank?
end

#configObject



49
50
51
# File 'lib/masheri/rest_client/query.rb', line 49

def config
  Masheri.config
end

#end_dateObject



20
21
22
# File 'lib/masheri/rest_client/query.rb', line 20

def end_date
  @options[:end_date].strftime("%Y-%m-%dT00:00:00Z")
end

#formatObject



8
9
10
# File 'lib/masheri/rest_client/query.rb', line 8

def format
  @options[:format] || "csv"
end

#limitObject



12
13
14
# File 'lib/masheri/rest_client/query.rb', line 12

def limit
  @options[:limit] || 1000
end

#paramsObject



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/masheri/rest_client/query.rb', line 35

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

#pathObject



65
66
67
# File 'lib/masheri/rest_client/query.rb', line 65

def path
  "/v2/rest/#{config.site_id}/reports/calls/#{resource}/service/#{service_id}"
end

#query_paramsObject



53
54
55
# File 'lib/masheri/rest_client/query.rb', line 53

def query_params
  URI.encode_www_form(params).gsub("%3A", ":")
end

#resourceObject



61
62
63
# File 'lib/masheri/rest_client/query.rb', line 61

def resource
  @options[:resource]
end

#service_idObject



57
58
59
# File 'lib/masheri/rest_client/query.rb', line 57

def service_id
  @options[:service_id]
end

#start_dateObject



16
17
18
# File 'lib/masheri/rest_client/query.rb', line 16

def start_date
  @options[:start_date].strftime("%Y-%m-%dT00:00:00Z")
end

#urlObject



69
70
71
# File 'lib/masheri/rest_client/query.rb', line 69

def url
  URI::HTTP.build(host: config.host, path: path, query: query_params).to_s
end