Class: Twingly::Analytics::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/twingly-analytics/query.rb

Constant Summary collapse

BASE_URL =
'https://api.twingly.com'
ANALYTICS_PATH =
'/analytics/Analytics.ashx'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Query

Returns a new instance of Query.



11
12
13
# File 'lib/twingly-analytics/query.rb', line 11

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



6
7
8
# File 'lib/twingly-analytics/query.rb', line 6

def client
  @client
end

#end_timeObject

Returns the value of attribute end_time.



6
7
8
# File 'lib/twingly-analytics/query.rb', line 6

def end_time
  @end_time
end

#languageObject

Returns the value of attribute language.



6
7
8
# File 'lib/twingly-analytics/query.rb', line 6

def language
  @language
end

#patternObject

Returns the value of attribute pattern.



6
7
8
# File 'lib/twingly-analytics/query.rb', line 6

def pattern
  @pattern
end

#start_timeObject

Returns the value of attribute start_time.



6
7
8
# File 'lib/twingly-analytics/query.rb', line 6

def start_time
  @start_time
end

Instance Method Details

#executeObject



19
20
21
# File 'lib/twingly-analytics/query.rb', line 19

def execute
  Parser.new.parse(get_response.body)
end

#request_parametersObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/twingly-analytics/query.rb', line 27

def request_parameters
  fail("Missing pattern") if pattern.to_s.empty?

  {
    :key => client.api_key,
    :searchpattern => pattern,
    :documentlang => language,
    :ts => ts,
    :tsTo => ts_to,
    :xmloutputversion => 2
  }
end

#urlObject



15
16
17
# File 'lib/twingly-analytics/query.rb', line 15

def url
  "#{BASE_URL}#{ANALYTICS_PATH}?#{url_parameters}"
end

#url_parametersObject



23
24
25
# File 'lib/twingly-analytics/query.rb', line 23

def url_parameters
  Faraday::Utils.build_query(request_parameters)
end