morgoth-alexa 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
data/alexa.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{alexa}
5
- s.version = "0.0.1"
5
+ s.version = "0.0.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Wojciech Wnętrzak"]
@@ -24,6 +24,7 @@ Gem::Specification.new do |s|
24
24
  "lib/alexa/url_info.rb",
25
25
  "test/alexa_test.rb",
26
26
  "test/fixtures/polsl.xml",
27
+ "test/fixtures/polsl_small.xml",
27
28
  "test/test_helper.rb"
28
29
  ]
29
30
  s.homepage = %q{http://github.com/morgoth/alexa}
@@ -7,9 +7,9 @@ module Alexa
7
7
  :rank_by_country, :rank_by_city, :usage_statistics
8
8
 
9
9
  def initialize(options = {} )
10
- @access_key_id = options[:access_key_id]
11
- @secret_access_key = options[:secret_access_key]
12
- @host = options[:host]
10
+ @access_key_id = options[:access_key_id] or raise ArgumentError.new("you must specify acces_key_id")
11
+ @secret_access_key = options[:secret_access_key] or raise ArgumentError.new("you must specify secret_acces_key")
12
+ @host = options[:host] or raise ArgumentError.new("you must specify host")
13
13
  @response_group = options[:response_group] || RESPONSE_GROUP
14
14
  end
15
15
 
@@ -23,20 +23,24 @@ module Alexa
23
23
 
24
24
  def parse_xml(xml)
25
25
  xml = XmlSimple.xml_in(xml.force_encoding(Encoding::UTF_8), 'ForceArray' => false)
26
- @rank = xml['Response']['UrlInfoResult']['Alexa']['TrafficData']['Rank'].to_i
27
- @data_url = xml['Response']['UrlInfoResult']['Alexa']['TrafficData']['DataUrl']['content']
28
- @site_title = xml['Response']['UrlInfoResult']['Alexa']['ContentData']['SiteData']['Title']
29
- @site_description = xml['Response']['UrlInfoResult']['Alexa']['ContentData']['SiteData']['Description']
30
- @language_locale = xml['Response']['UrlInfoResult']['Alexa']['ContentData']['Language']['Locale']
31
- @language_encoding = xml['Response']['UrlInfoResult']['Alexa']['ContentData']['Language']['Encoding']
32
- @links_in_count = xml['Response']['UrlInfoResult']['Alexa']['ContentData']['LinksInCount'].to_i
33
- @keywords = xml['Response']['UrlInfoResult']['Alexa']['ContentData']['Keywords']['Keyword']
34
- @related_links = xml['Response']['UrlInfoResult']['Alexa']['Related']['RelatedLinks']['RelatedLink']
35
- @speed_median_load_time = xml['Response']['UrlInfoResult']['Alexa']['ContentData']['Speed']['MedianLoadTime'].to_i
36
- @speed_percentile = xml['Response']['UrlInfoResult']['Alexa']['ContentData']['Speed']['Percentile'].to_i
37
- @rank_by_country = xml['Response']['UrlInfoResult']['Alexa']['TrafficData']['RankByCountry']['Country']
38
- @rank_by_city = xml['Response']['UrlInfoResult']['Alexa']['TrafficData']['RankByCity']['City']
39
- @usage_statistics = xml['Response']['UrlInfoResult']['Alexa']['TrafficData']['UsageStatistics']["UsageStatistic"]
26
+ group = response_group.split(',')
27
+ alexa = xml['Response']['UrlInfoResult']['Alexa']
28
+ @rank = alexa['TrafficData']['Rank'].to_i if group.include?('Rank')
29
+ @data_url = alexa['TrafficData']['DataUrl']['content'] if group.include?('Rank')
30
+ @rank_by_country = alexa['TrafficData']['RankByCountry']['Country'] if group.include?('RankByCountry')
31
+ @rank_by_city = alexa['TrafficData']['RankByCity']['City'] if group.include?('RankByCity')
32
+ @usage_statistics = alexa['TrafficData']['UsageStatistics']["UsageStatistic"]if group.include?('UsageStats')
33
+
34
+ @site_title = alexa['ContentData']['SiteData']['Title'] if group.include?('SiteData')
35
+ @site_description = alexa['ContentData']['SiteData']['Description'] if group.include?('SiteData')
36
+ @language_locale = alexa['ContentData']['Language']['Locale'] if group.include?('Language')
37
+ @language_encoding = alexa['ContentData']['Language']['Encoding'] if group.include?('Language')
38
+ @links_in_count = alexa['ContentData']['LinksInCount'].to_i if group.include?('LinksInCount')
39
+ @keywords = alexa['ContentData']['Keywords']['Keyword'] if group.include?('Keywords')
40
+ @speed_median_load_time = alexa['ContentData']['Speed']['MedianLoadTime'].to_i if group.include?('Speed')
41
+ @speed_percentile = alexa['ContentData']['Speed']['Percentile'].to_i if group.include?('Speed')
42
+
43
+ @related_links = alexa['Related']['RelatedLinks']['RelatedLink'] if group.include?('RelatedLinks')
40
44
  end
41
45
 
42
46
  private
data/test/alexa_test.rb CHANGED
@@ -29,10 +29,41 @@ class AlexaTest < Test::Unit::TestCase
29
29
  assert_equal "awis.amazonaws.com", url.host
30
30
  end
31
31
 
32
- context "should parse xml and" do
32
+ context "should parse xml return by options LinksInCount,SiteData and" do
33
33
  setup do
34
- @xml = fixture_file('polsl.xml')
35
- @alexa.parse_xml(@xml)
34
+ @alexa.response_group = "Rank,LinksInCount,SiteData"
35
+ xml = fixture_file('polsl_small.xml')
36
+ @alexa.parse_xml(xml)
37
+ end
38
+
39
+ should "return rank" do
40
+ assert_equal 86020, @alexa.rank
41
+ end
42
+
43
+ should "return data url" do
44
+ assert_equal "polsl.pl/", @alexa.data_url
45
+ end
46
+
47
+ should "return site title" do
48
+ assert_equal "Silesian University of Technology", @alexa.site_title
49
+ end
50
+
51
+ should "return site description" do
52
+ assert_equal "About the university, studies, faculties and departments, photo gallery.", @alexa.site_description
53
+ end
54
+
55
+ should "not crush" do
56
+ assert_nothing_raised do
57
+ @alexa.language_locale
58
+ end
59
+ assert_nil @alexa.language_locale
60
+ end
61
+ end
62
+
63
+ context "should parse xml with all options and" do
64
+ setup do
65
+ xml = fixture_file('polsl.xml')
66
+ @alexa.parse_xml(xml)
36
67
  end
37
68
 
38
69
  should "return rank" do
@@ -93,4 +124,27 @@ class AlexaTest < Test::Unit::TestCase
93
124
 
94
125
  end
95
126
  end
127
+
128
+ should "Raise argumment error if keys or host are not present" do
129
+ assert_raise ArgumentError, do
130
+ Alexa::UrlInfo.new(
131
+ :secret_access_key => "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDF",
132
+ :host => "some.host"
133
+ )
134
+ end
135
+
136
+ assert_raise ArgumentError, do
137
+ Alexa::UrlInfo.new(
138
+ :access_key_id => "12345678901234567890",
139
+ :host => "some.host"
140
+ )
141
+ end
142
+
143
+ assert_raise ArgumentError, do
144
+ Alexa::UrlInfo.new(
145
+ :access_key_id => "12345678901234567890",
146
+ :secret_access_key => "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDF",
147
+ )
148
+ end
149
+ end
96
150
  end
@@ -0,0 +1,16 @@
1
+ <?xml version="1.0"?>
2
+ <aws:UrlInfoResponse xmlns:aws="http://alexa.amazonaws.com/doc/2005-10-05/"><aws:Response xmlns:aws="http://awis.amazonaws.com/doc/2005-07-11"><aws:OperationRequest><aws:RequestId>ce456f7b-b273-482e-8533-9442718b8002</aws:RequestId></aws:OperationRequest><aws:UrlInfoResult><aws:Alexa>
3
+
4
+ <aws:ContentData>
5
+ <aws:DataUrl type="canonical">polsl.pl/</aws:DataUrl>
6
+ <aws:SiteData>
7
+ <aws:Title>Silesian University of Technology</aws:Title>
8
+ <aws:Description>About the university, studies, faculties and departments, photo gallery.</aws:Description>
9
+ </aws:SiteData>
10
+ <aws:LinksInCount>281</aws:LinksInCount>
11
+ </aws:ContentData>
12
+ <aws:TrafficData>
13
+ <aws:DataUrl type="canonical">polsl.pl/</aws:DataUrl>
14
+ <aws:Rank>86020</aws:Rank>
15
+ </aws:TrafficData>
16
+ </aws:Alexa></aws:UrlInfoResult><aws:ResponseStatus xmlns:aws="http://alexa.amazonaws.com/doc/2005-10-05/"><aws:StatusCode>Success</aws:StatusCode></aws:ResponseStatus></aws:Response></aws:UrlInfoResponse>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: morgoth-alexa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Wojciech Wn\xC4\x99trzak"
@@ -43,6 +43,7 @@ files:
43
43
  - lib/alexa/url_info.rb
44
44
  - test/alexa_test.rb
45
45
  - test/fixtures/polsl.xml
46
+ - test/fixtures/polsl_small.xml
46
47
  - test/test_helper.rb
47
48
  has_rdoc: false
48
49
  homepage: http://github.com/morgoth/alexa