alexa 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,45 +1,63 @@
1
- module Alexa::API
2
- class TrafficHistory
3
- include Alexa::Utils
4
-
5
- attr_reader :url, :range, :start, :response_body
6
-
7
- def initialize(credentials)
8
- @credentials = credentials
9
- end
10
-
11
- def fetch(arguments = {})
12
- @url = arguments[:url] || raise(ArgumentError.new("You must specify url"))
13
- @range = arguments.fetch(:range, 31)
14
- @start = arguments.fetch(:start) { Time.now - (3600 * 24 * range.to_i) }
15
- @response_body = Alexa::Connection.new(@credentials).get(params)
16
- self
17
- end
18
-
19
- # Attributes
20
-
21
- def data
22
- @data ||= safe_retrieve(parsed_body, "TrafficHistoryResponse", "Response", "TrafficHistoryResult", "Alexa", "TrafficHistory", "HistoricalData", "Data")
23
- end
24
-
25
- private
26
-
27
- def params
28
- {
29
- "Action" => "TrafficHistory",
30
- "ResponseGroup" => "History",
31
- "Range" => range,
32
- "Start" => start_param,
33
- "Url" => url
34
- }
35
- end
36
-
37
- def start_param
38
- start.respond_to?(:strftime) ? start.strftime("%Y%m%d") : start
39
- end
40
-
41
- def parsed_body
42
- @parsed_body ||= MultiXml.parse(response_body)
1
+ require "alexa/api/base"
2
+
3
+ module Alexa
4
+ module API
5
+ class TrafficHistory < Base
6
+ def fetch(arguments = {})
7
+ raise ArgumentError, "You must specify url" unless arguments.has_key?(:url)
8
+ @arguments = arguments
9
+
10
+ @arguments[:range] = arguments.fetch(:range, 31)
11
+ @arguments[:start] = arguments.fetch(:start) { Time.now - (3600 * 24 * @arguments[:range].to_i) }
12
+
13
+ @response_body = Alexa::Connection.new(@credentials).get(params)
14
+ self
15
+ end
16
+
17
+ # Response attributes
18
+
19
+ def site
20
+ @site ||= safe_retrieve(parsed_body, "TrafficHistoryResponse", "Response", "TrafficHistoryResult", "Alexa", "TrafficHistory", "Site")
21
+ end
22
+
23
+ def start
24
+ @start ||= safe_retrieve(parsed_body, "TrafficHistoryResponse", "Response", "TrafficHistoryResult", "Alexa", "TrafficHistory", "Start")
25
+ end
26
+
27
+ def range
28
+ return @range if defined?(@range)
29
+ if range = safe_retrieve(parsed_body, "TrafficHistoryResponse", "Response", "TrafficHistoryResult", "Alexa", "TrafficHistory", "Range")
30
+ @range = range.to_i
31
+ end
32
+ end
33
+
34
+ def data
35
+ @data ||= safe_retrieve(parsed_body, "TrafficHistoryResponse", "Response", "TrafficHistoryResult", "Alexa", "TrafficHistory", "HistoricalData", "Data")
36
+ end
37
+
38
+ def status_code
39
+ @status_code ||= safe_retrieve(parsed_body, "TrafficHistoryResponse", "Response", "ResponseStatus", "StatusCode")
40
+ end
41
+
42
+ def request_id
43
+ @request_id ||= safe_retrieve(parsed_body, "TrafficHistoryResponse", "Response", "OperationRequest", "RequestId")
44
+ end
45
+
46
+ private
47
+
48
+ def params
49
+ {
50
+ "Action" => "TrafficHistory",
51
+ "ResponseGroup" => "History",
52
+ "Range" => arguments[:range],
53
+ "Start" => start_param,
54
+ "Url" => arguments[:url]
55
+ }
56
+ end
57
+
58
+ def start_param
59
+ arguments[:start].respond_to?(:strftime) ? arguments[:start].strftime("%Y%m%d") : arguments[:start]
60
+ end
43
61
  end
44
62
  end
45
63
  end
@@ -1,108 +1,111 @@
1
- module Alexa::API
2
- class UrlInfo
3
- include Alexa::Utils
1
+ require "alexa/api/base"
4
2
 
5
- DEFAULT_RESPONSE_GROUP = ["adult_content", "contact_info", "keywords", "language", "links_in_count", "owned_domains", "rank", "rank_by_city", "rank_by_country", "related_links", "site_data", "speed", "usage_stats"]
3
+ module Alexa
4
+ module API
5
+ class UrlInfo < Base
6
+ DEFAULT_RESPONSE_GROUP = ["adult_content", "contact_info", "keywords", "language", "links_in_count", "owned_domains", "rank", "rank_by_city", "rank_by_country", "related_links", "site_data", "speed", "usage_stats"]
6
7
 
7
- attr_reader :url, :response_group, :response_body
8
+ def fetch(arguments = {})
9
+ raise ArgumentError, "You must specify url" unless arguments.has_key?(:url)
10
+ @arguments = arguments
8
11
 
9
- def initialize(credentials)
10
- @credentials = credentials
11
- end
12
+ @arguments[:response_group] = Array(arguments.fetch(:response_group, DEFAULT_RESPONSE_GROUP))
12
13
 
13
- def fetch(arguments = {})
14
- @url = arguments[:url] || raise(ArgumentError.new("You must specify url"))
15
- @response_group = Array(arguments.fetch(:response_group, DEFAULT_RESPONSE_GROUP))
16
- @response_body = Alexa::Connection.new(@credentials).get(params)
17
- self
18
- end
14
+ @response_body = Alexa::Connection.new(@credentials).get(params)
15
+ self
16
+ end
19
17
 
20
- # attributes
18
+ # Response attributes
21
19
 
22
- def rank
23
- return @rank if defined?(@rank)
24
- if rank = safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "TrafficData", "Rank")
25
- @rank = rank.to_i
20
+ def rank
21
+ return @rank if defined?(@rank)
22
+ if rank = safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "TrafficData", "Rank")
23
+ @rank = rank.to_i
24
+ end
26
25
  end
27
- end
28
26
 
29
- def rank_by_country
30
- @rank_by_country ||= safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "TrafficData", "RankByCountry", "Country")
31
- end
27
+ def rank_by_country
28
+ @rank_by_country ||= safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "TrafficData", "RankByCountry", "Country")
29
+ end
32
30
 
33
- def rank_by_city
34
- @rank_by_city ||= safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "TrafficData", "RankByCity", "City")
35
- end
31
+ def rank_by_city
32
+ @rank_by_city ||= safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "TrafficData", "RankByCity", "City")
33
+ end
36
34
 
37
- def data_url
38
- @data_url ||= safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "TrafficData", "DataUrl", "__content__")
39
- end
35
+ def data_url
36
+ @data_url ||= safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "TrafficData", "DataUrl", "__content__")
37
+ end
40
38
 
41
- def usage_statistics
42
- @usage_statistics ||= safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "TrafficData", "UsageStatistics", "UsageStatistic")
43
- end
39
+ def usage_statistics
40
+ @usage_statistics ||= safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "TrafficData", "UsageStatistics", "UsageStatistic")
41
+ end
44
42
 
45
- def site_title
46
- @site_title ||= safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "ContentData", "SiteData", "Title")
47
- end
43
+ def site_title
44
+ @site_title ||= safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "ContentData", "SiteData", "Title")
45
+ end
48
46
 
49
- def site_description
50
- @site_description ||= safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "ContentData", "SiteData", "Description")
51
- end
47
+ def site_description
48
+ @site_description ||= safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "ContentData", "SiteData", "Description")
49
+ end
52
50
 
53
- def language_locale
54
- @language_locale ||= safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "ContentData", "Language", "Locale")
55
- end
51
+ def language_locale
52
+ @language_locale ||= safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "ContentData", "Language", "Locale")
53
+ end
56
54
 
57
- def language_encoding
58
- @language_encoding ||= safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "ContentData", "Language", "Encoding")
59
- end
55
+ def language_encoding
56
+ @language_encoding ||= safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "ContentData", "Language", "Encoding")
57
+ end
60
58
 
61
- def links_in_count
62
- return @links_in_count if defined?(@links_in_count)
63
- if links_in_count = safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "ContentData", "LinksInCount")
64
- @links_in_count = links_in_count.to_i
59
+ def links_in_count
60
+ return @links_in_count if defined?(@links_in_count)
61
+ if links_in_count = safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "ContentData", "LinksInCount")
62
+ @links_in_count = links_in_count.to_i
63
+ end
65
64
  end
66
- end
67
65
 
68
- def keywords
69
- @keywords ||= safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "ContentData", "Keywords", "Keyword")
70
- end
66
+ def keywords
67
+ @keywords ||= safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "ContentData", "Keywords", "Keyword")
68
+ end
71
69
 
72
- def speed_median_load_time
73
- return @speed_median_load_time if defined?(@speed_median_load_time)
74
- if speed_median_load_time = safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "ContentData", "Speed", "MedianLoadTime")
75
- @speed_median_load_time = speed_median_load_time.to_i
70
+ def speed_median_load_time
71
+ return @speed_median_load_time if defined?(@speed_median_load_time)
72
+ if speed_median_load_time = safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "ContentData", "Speed", "MedianLoadTime")
73
+ @speed_median_load_time = speed_median_load_time.to_i
74
+ end
76
75
  end
77
- end
78
76
 
79
- def speed_percentile
80
- return @speed_percentile if defined?(@speed_percentile)
81
- if speed_percentile = safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "ContentData", "Speed", "Percentile")
82
- @speed_percentile = speed_percentile.to_i
77
+ def speed_percentile
78
+ return @speed_percentile if defined?(@speed_percentile)
79
+ if speed_percentile = safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "ContentData", "Speed", "Percentile")
80
+ @speed_percentile = speed_percentile.to_i
81
+ end
83
82
  end
84
- end
85
83
 
86
- def related_links
87
- @related_links ||= safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "Related", "RelatedLinks", "RelatedLink")
88
- end
84
+ def related_links
85
+ @related_links ||= safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "Related", "RelatedLinks", "RelatedLink")
86
+ end
89
87
 
90
- private
88
+ def status_code
89
+ @status_code ||= safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "ResponseStatus", "StatusCode")
90
+ end
91
91
 
92
- def params
93
- {
94
- "Action" => "UrlInfo",
95
- "ResponseGroup" => response_group_param,
96
- "Url" => url
97
- }
98
- end
92
+ def request_id
93
+ @request_id ||= safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "OperationRequest", "RequestId")
94
+ end
99
95
 
100
- def response_group_param
101
- response_group.sort.map { |group| camelize(group) }.join(",")
102
- end
96
+ private
97
+
98
+ def params
99
+ {
100
+ "Action" => "UrlInfo",
101
+ "ResponseGroup" => response_group_param,
102
+ "Url" => arguments[:url]
103
+ }
104
+ end
103
105
 
104
- def parsed_body
105
- @parsed_body ||= MultiXml.parse(response_body)
106
+ def response_group_param
107
+ arguments[:response_group].sort.map { |group| camelize(group) }.join(",")
108
+ end
106
109
  end
107
110
  end
108
111
  end
@@ -1,3 +1,3 @@
1
1
  module Alexa
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.1"
3
3
  end
@@ -1,6 +1,12 @@
1
1
  require "helper"
2
2
 
3
3
  describe Alexa::API::CategoryBrowse do
4
+ it "raises argument error when path not present" do
5
+ assert_raises Alexa::ArgumentError, /path/ do
6
+ Alexa::API::CategoryBrowse.new(:access_key_id => "fake", :secret_access_key => "fake").fetch
7
+ end
8
+ end
9
+
4
10
  describe "parsing xml" do
5
11
  before do
6
12
  stub_request(:get, %r{http://awis.amazonaws.com}).to_return(fixture("category_browse/card_games.txt"))
@@ -23,5 +29,13 @@ describe Alexa::API::CategoryBrowse do
23
29
  it "returns letter bars" do
24
30
  assert_equal 36, @category_browse.letter_bars.size
25
31
  end
32
+
33
+ it "has success status code" do
34
+ assert_equal "Success", @category_browse.status_code
35
+ end
36
+
37
+ it "has request id" do
38
+ assert_equal "c8bec6fe-84f3-9a8e-7444-47f86c64d74b", @category_browse.request_id
39
+ end
26
40
  end
27
41
  end
@@ -1,6 +1,12 @@
1
1
  require "helper"
2
2
 
3
3
  describe Alexa::API::CategoryListings do
4
+ it "raises argument error when path not present" do
5
+ assert_raises Alexa::ArgumentError, /path/ do
6
+ Alexa::API::CategoryListings.new(:access_key_id => "fake", :secret_access_key => "fake").fetch
7
+ end
8
+ end
9
+
4
10
  describe "parsing xml" do
5
11
  before do
6
12
  stub_request(:get, %r{http://awis.amazonaws.com}).to_return(fixture("category_listings/card_games.txt"))
@@ -12,8 +18,20 @@ describe Alexa::API::CategoryListings do
12
18
  assert_equal 1051, @category_listings.recursive_count
13
19
  end
14
20
 
21
+ it "returns count" do
22
+ assert_equal 1, @category_listings.count
23
+ end
24
+
15
25
  it "returns listings" do
16
26
  assert_equal 20, @category_listings.listings.size
17
27
  end
28
+
29
+ it "has success status code" do
30
+ assert_equal "Success", @category_listings.status_code
31
+ end
32
+
33
+ it "has request id" do
34
+ assert_equal "a069b6cd-309f-df52-88be-1bd88ab45b7e", @category_listings.request_id
35
+ end
18
36
  end
19
37
  end
@@ -1,6 +1,12 @@
1
1
  require "helper"
2
2
 
3
3
  describe Alexa::API::SitesLinkingIn do
4
+ it "raises argument error when url not present" do
5
+ assert_raises Alexa::ArgumentError, /url/ do
6
+ Alexa::API::SitesLinkingIn.new(:access_key_id => "fake", :secret_access_key => "fake").fetch
7
+ end
8
+ end
9
+
4
10
  describe "parsing xml" do
5
11
  before do
6
12
  stub_request(:get, %r{http://awis.amazonaws.com}).to_return(fixture("sites_linking_in/github_count_3.txt"))
@@ -19,5 +25,13 @@ describe Alexa::API::SitesLinkingIn do
19
25
  it "has Url attribute on single site" do
20
26
  assert_equal "code.google.com:80/a/eclipselabs.org/p/m2eclipse-android-integration", @sites_linking_in.sites.first["Url"]
21
27
  end
28
+
29
+ it "has success status code" do
30
+ assert_equal "Success", @sites_linking_in.status_code
31
+ end
32
+
33
+ it "has request id" do
34
+ assert_equal "abb553a3-035f-8d12-f353-40532a087b52", @sites_linking_in.request_id
35
+ end
22
36
  end
23
37
  end
@@ -1,24 +1,58 @@
1
1
  require "helper"
2
2
 
3
3
  describe Alexa::API::TrafficHistory do
4
+ it "raises argument error when url not present" do
5
+ assert_raises Alexa::ArgumentError, /url/ do
6
+ Alexa::API::TrafficHistory.new(:access_key_id => "fake", :secret_access_key => "fake").fetch
7
+ end
8
+ end
9
+
4
10
  it "defaults start to be in range to current time" do
5
11
  stub_request(:get, %r{http://awis.amazonaws.com}).to_return(:body => "ok")
6
12
  @traffic_history = Alexa::API::TrafficHistory.new(:access_key_id => "fake", :secret_access_key => "fake")
7
13
  @traffic_history.fetch(:url => "github.com", :range => 14)
8
14
 
9
15
  # 14 days from now
10
- assert_in_delta (Time.now - 3600 * 24 * 14).to_i, @traffic_history.start.to_i, 0.001
16
+ assert_in_delta (Time.now - 3600 * 24 * 14).to_i, @traffic_history.arguments[:start].to_i, 0.001
11
17
  end
12
18
 
13
19
  describe "parsing xml" do
14
20
  before do
15
- stub_request(:get, %r{http://awis.amazonaws.com}).to_return(fixture("traffic_history/alexa_example.txt"))
21
+ stub_request(:get, %r{http://awis.amazonaws.com}).to_return(fixture("traffic_history/github.txt"))
16
22
  @traffic_history = Alexa::API::TrafficHistory.new(:access_key_id => "fake", :secret_access_key => "fake")
17
- @traffic_history.fetch(:url => "amazon.com", :start => "20050101", :range => 31)
23
+ @traffic_history.fetch(:url => "github.com")
24
+ end
25
+
26
+ it "returns site" do
27
+ assert_equal "github.com", @traffic_history.site
28
+ end
29
+
30
+ it "returns range" do
31
+ assert_equal 28, @traffic_history.range
32
+ end
33
+
34
+ it "returns start" do
35
+ assert_equal "2012-07-11", @traffic_history.start
18
36
  end
19
37
 
20
38
  it "returns data" do
21
- # assert_equal 1, @traffic_history.data.size
39
+ assert_equal 28, @traffic_history.data.size
40
+ end
41
+
42
+ it "has success status code" do
43
+ assert_equal "Success", @traffic_history.status_code
44
+ end
45
+
46
+ it "has request id" do
47
+ assert_equal "617614fc-46a3-a105-a7ab-80a17f117bcf", @traffic_history.request_id
22
48
  end
23
49
  end
50
+
51
+ it "has error status code" do
52
+ stub_request(:get, %r{http://awis.amazonaws.com}).to_return(fixture("traffic_history/alexa_error.txt"))
53
+ traffic_history = Alexa::API::TrafficHistory.new(:access_key_id => "fake", :secret_access_key => "fake")
54
+ traffic_history.fetch(:url => "amazon.com")
55
+
56
+ assert_equal "AlexaError", traffic_history.status_code
57
+ end
24
58
  end