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.
- data/README.md +70 -33
- data/lib/alexa/api/base.rb +20 -0
- data/lib/alexa/api/category_browse.rb +48 -45
- data/lib/alexa/api/category_listings.rb +60 -50
- data/lib/alexa/api/sites_linking_in.rb +41 -38
- data/lib/alexa/api/traffic_history.rb +60 -42
- data/lib/alexa/api/url_info.rb +81 -78
- data/lib/alexa/version.rb +1 -1
- data/test/api/category_browse_test.rb +14 -0
- data/test/api/category_listings_test.rb +18 -0
- data/test/api/sites_linking_in_test.rb +14 -0
- data/test/api/traffic_history_test.rb +38 -4
- data/test/api/url_info_test.rb +15 -1
- data/test/fixtures/traffic_history/{github_range_2.txt → alexa_error.txt} +1 -1
- data/test/fixtures/traffic_history/github.txt +325 -0
- metadata +7 -6
- data/test/fixtures/traffic_history/alexa_example.txt +0 -37
@@ -1,45 +1,63 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
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
|
-
|
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
|
data/lib/alexa/api/url_info.rb
CHANGED
@@ -1,108 +1,111 @@
|
|
1
|
-
|
2
|
-
class UrlInfo
|
3
|
-
include Alexa::Utils
|
1
|
+
require "alexa/api/base"
|
4
2
|
|
5
|
-
|
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
|
-
|
8
|
+
def fetch(arguments = {})
|
9
|
+
raise ArgumentError, "You must specify url" unless arguments.has_key?(:url)
|
10
|
+
@arguments = arguments
|
8
11
|
|
9
|
-
|
10
|
-
@credentials = credentials
|
11
|
-
end
|
12
|
+
@arguments[:response_group] = Array(arguments.fetch(:response_group, DEFAULT_RESPONSE_GROUP))
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
-
|
18
|
+
# Response attributes
|
21
19
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
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
|
-
|
30
|
-
|
31
|
-
|
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
|
-
|
34
|
-
|
35
|
-
|
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
|
-
|
38
|
-
|
39
|
-
|
35
|
+
def data_url
|
36
|
+
@data_url ||= safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "TrafficData", "DataUrl", "__content__")
|
37
|
+
end
|
40
38
|
|
41
|
-
|
42
|
-
|
43
|
-
|
39
|
+
def usage_statistics
|
40
|
+
@usage_statistics ||= safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "TrafficData", "UsageStatistics", "UsageStatistic")
|
41
|
+
end
|
44
42
|
|
45
|
-
|
46
|
-
|
47
|
-
|
43
|
+
def site_title
|
44
|
+
@site_title ||= safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "ContentData", "SiteData", "Title")
|
45
|
+
end
|
48
46
|
|
49
|
-
|
50
|
-
|
51
|
-
|
47
|
+
def site_description
|
48
|
+
@site_description ||= safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "ContentData", "SiteData", "Description")
|
49
|
+
end
|
52
50
|
|
53
|
-
|
54
|
-
|
55
|
-
|
51
|
+
def language_locale
|
52
|
+
@language_locale ||= safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "ContentData", "Language", "Locale")
|
53
|
+
end
|
56
54
|
|
57
|
-
|
58
|
-
|
59
|
-
|
55
|
+
def language_encoding
|
56
|
+
@language_encoding ||= safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "ContentData", "Language", "Encoding")
|
57
|
+
end
|
60
58
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
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
|
-
|
69
|
-
|
70
|
-
|
66
|
+
def keywords
|
67
|
+
@keywords ||= safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "ContentData", "Keywords", "Keyword")
|
68
|
+
end
|
71
69
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
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
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
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
|
-
|
87
|
-
|
88
|
-
|
84
|
+
def related_links
|
85
|
+
@related_links ||= safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "UrlInfoResult", "Alexa", "Related", "RelatedLinks", "RelatedLink")
|
86
|
+
end
|
89
87
|
|
90
|
-
|
88
|
+
def status_code
|
89
|
+
@status_code ||= safe_retrieve(parsed_body, "UrlInfoResponse", "Response", "ResponseStatus", "StatusCode")
|
90
|
+
end
|
91
91
|
|
92
|
-
|
93
|
-
|
94
|
-
|
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
|
-
|
101
|
-
|
102
|
-
|
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
|
-
|
105
|
-
|
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
|
data/lib/alexa/version.rb
CHANGED
@@ -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/
|
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 => "
|
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
|
-
|
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
|