Class: Mashery::RestClient::Query

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

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Query



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

def initialize(options)
  @options  = options
end

Instance Method Details

#check_dates!Object



38
39
40
41
42
# File 'lib/mashery/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!Object

Raises:



32
33
34
35
36
# File 'lib/mashery/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

#configObject



58
59
60
# File 'lib/mashery/rest_client/query.rb', line 58

def config
  Mashery.config
end

#end_dateObject



28
29
30
# File 'lib/mashery/rest_client/query.rb', line 28

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

#formatObject



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

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

#limitObject



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

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

#paramsObject



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mashery/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_paramsObject



62
63
64
# File 'lib/mashery/rest_client/query.rb', line 62

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

#resourceObject



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

def resource
  @options[:resource]
end

#rest_pathObject



66
67
68
# File 'lib/mashery/rest_client/query.rb', line 66

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

#service_idObject



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

def service_id
  @options[:service_id]
end

#start_dateObject



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

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

#urlObject



70
71
72
73
74
# File 'lib/mashery/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