Class: Alexa::TrafficHistory

Inherits:
Object
  • Object
show all
Defined in:
lib/alexa/traffic_history.rb,
lib/alexa/traffic_history/historical_data.rb

Defined Under Namespace

Classes: ArgumentException, HistoricalData

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ TrafficHistory

Returns a new instance of TrafficHistory.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/alexa/traffic_history.rb', line 12

def initialize(data = {})
  @options = {
    'Action'           => 'TrafficHistory',
    'AWSAccessKeyId'   => Alexa.config.access_key_id,
    'Timestamp'        => ( Time::now ).utc.strftime("%Y-%m-%dT%H:%M:%S.000Z"),
    'ResponseGroup'    => 'History',
    'SignatureVersion' => 2,
    'SignatureMethod'  => 'HmacSHA256',
    'Version'          => '2005-07-11',
    'Range'            => nil,
    'StartDate'        => nil,
    'Url'              => nil,
  }

  if data['historical_data']
    @historical_data = data['historical_data']
  end

  if data['range']
    @range = data['range']
  end

  if data['site']
    @site = data['site']
  end

  if data['start']
    @start = data['start']
  end

  if Alexa.config.proxy
    RestClient.proxy = Alexa.config.proxy
  end
end

Instance Attribute Details

#historical_dataObject (readonly)

Returns the value of attribute historical_data.



10
11
12
# File 'lib/alexa/traffic_history.rb', line 10

def historical_data
  @historical_data
end

#rangeObject (readonly)

Returns the value of attribute range.



10
11
12
# File 'lib/alexa/traffic_history.rb', line 10

def range
  @range
end

#siteObject (readonly)

Returns the value of attribute site.



10
11
12
# File 'lib/alexa/traffic_history.rb', line 10

def site
  @site
end

#startObject (readonly)

Returns the value of attribute start.



10
11
12
# File 'lib/alexa/traffic_history.rb', line 10

def start
  @start
end

Class Method Details

.from_mongo(value) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/alexa/traffic_history.rb', line 86

def self.from_mongo(value)
  return nil   if value.nil?
  return value if value.is_a?(self)

  self.new('range'           => value['range'],
           'site'            => value['site'],
           'start'           => value['start'],
           'historical_data' => HistoricalData.from_mongo(value['historical_data']))
end

.to_mongo(value) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/alexa/traffic_history.rb', line 74

def self.to_mongo(value)
  return nil   if value.nil?
  return value if value.is_a? Hash

  return {
    'range'           => value.range.to_s,
    'site'            => value.site.to_s,
    'start'           => value.start.to_s,
    'historical_data' => value.historical_data.to_mongo,
  }
end

Instance Method Details

#fetch(options = {}) ⇒ Object

Raises:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/alexa/traffic_history.rb', line 56

def fetch(options = {})
  @options.merge!(options)
  @options.delete_if { |k,v| v.nil? }

  raise ArgumentException.new('Url field is empty!') unless @options['Url']

  response = RestClient.get(url)
  data = Nokogiri::XML(response.to_s)
  data.remove_namespaces!

  @range = data.xpath('//TrafficHistory/Range').first.text.strip
  @site  = data.xpath('//TrafficHistory/Site').first.text.strip
  @start = data.xpath('//TrafficHistory/Start').first.text.strip
  @historical_data = HistoricalData.new(data.xpath('//HistoricalData').first)

  self
end

#signatureObject



47
48
49
# File 'lib/alexa/traffic_history.rb', line 47

def signature
  Alexa.sign(@options)
end

#to_mongoObject



96
97
98
# File 'lib/alexa/traffic_history.rb', line 96

def to_mongo
  self.class.to_mongo(self)
end

#urlObject



51
52
53
54
# File 'lib/alexa/traffic_history.rb', line 51

def url
  qs = Alexa.query_string(@options)
  'http://' + ::Alexa::SERVICE_HOST + '/?' + qs + '&Signature=' + signature
end