alexa 0.0.7 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -11,11 +11,14 @@
11
11
  :links_in_count, :keywords, :related_links, :speed_median_load_time, :speed_percentile,
12
12
  :rank_by_country, :rank_by_city, :usage_statistics
13
13
 
14
+ You can set configuration in block like this:
15
+ Alexa.config do |c|
16
+ c.access_key_id = "key"
17
+ c.secret_access_key = "secret"
18
+ end
19
+
14
20
  == Installation
15
- <b>deprecated!</b>
16
- gem install morgoth-alexa
17
- <b>actual</b>
18
- gem install alexa --source http://gemcutter.org
21
+ gem install alexa
19
22
 
20
23
  == Contributors
21
24
  * rmoriz[http://github.com/rmoriz]
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.7
1
+ 0.1.0
data/alexa.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{alexa}
8
- s.version = "0.0.7"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Wojciech Wnętrzak"]
12
- s.date = %q{2009-11-03}
12
+ s.date = %q{2010-01-28}
13
13
  s.description = %q{Alexa Web Information Service library (AWIS)}
14
14
  s.email = %q{w.wnetrzak@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -25,12 +25,14 @@ Gem::Specification.new do |s|
25
25
  "VERSION",
26
26
  "alexa.gemspec",
27
27
  "lib/alexa.rb",
28
+ "lib/alexa/config.rb",
28
29
  "lib/alexa/url_info.rb",
29
- "test/alexa_test.rb",
30
+ "test/config_test.rb",
30
31
  "test/fixtures/empty.xml",
31
32
  "test/fixtures/polsl.xml",
32
33
  "test/fixtures/polsl_small.xml",
33
- "test/test_helper.rb"
34
+ "test/test_helper.rb",
35
+ "test/url_info_test.rb"
34
36
  ]
35
37
  s.homepage = %q{http://github.com/morgoth/alexa}
36
38
  s.rdoc_options = ["--charset=UTF-8"]
@@ -38,7 +40,8 @@ Gem::Specification.new do |s|
38
40
  s.rubygems_version = %q{1.3.5}
39
41
  s.summary = %q{Alexa Web Information Service library}
40
42
  s.test_files = [
41
- "test/alexa_test.rb",
43
+ "test/url_info_test.rb",
44
+ "test/config_test.rb",
42
45
  "test/test_helper.rb"
43
46
  ]
44
47
 
data/lib/alexa.rb CHANGED
@@ -8,6 +8,7 @@ require "net/https"
8
8
  require "xmlsimple"
9
9
  require "time"
10
10
 
11
+ require 'alexa/config'
11
12
  require 'alexa/url_info'
12
13
 
13
14
  module Alexa
@@ -0,0 +1,15 @@
1
+ require 'singleton'
2
+
3
+ module Alexa
4
+ class Config
5
+ include Singleton
6
+ attr_accessor :access_key_id, :secret_access_key
7
+ end
8
+
9
+ def self.config
10
+ if block_given?
11
+ yield Config.instance
12
+ end
13
+ Config.instance
14
+ end
15
+ end
@@ -1,23 +1,25 @@
1
1
  module Alexa
2
2
  class UrlInfo
3
3
  RESPONSE_GROUP = "Rank,ContactInfo,AdultContent,Speed,Language,Keywords,OwnedDomains,LinksInCount,SiteData,RelatedLinks,RankByCountry,RankByCity,UsageStats"
4
- attr_accessor :access_key_id, :secret_access_key, :host, :response_group, :xml_response,
4
+ attr_accessor :host, :response_group, :xml_response,
5
5
  :rank, :data_url, :site_title, :site_description, :language_locale, :language_encoding,
6
6
  :links_in_count, :keywords, :related_links, :speed_median_load_time, :speed_percentile,
7
7
  :rank_by_country, :rank_by_city, :usage_statistics
8
8
 
9
- def initialize(options = {} )
10
- @access_key_id = options[:access_key_id] or raise ArgumentError.new("you must specify access_key_id")
11
- @secret_access_key = options[:secret_access_key] or raise ArgumentError.new("you must specify secret_access_key")
9
+ def initialize(options = {})
10
+ Alexa.config.access_key_id = options[:access_key_id] || Alexa.config.access_key_id
11
+ Alexa.config.secret_access_key = options[:secret_access_key] || Alexa.config.secret_access_key
12
+ raise ArgumentError.new("you must specify access_key_id") if Alexa.config.access_key_id.nil?
13
+ raise ArgumentError.new("you must specify secret_access_key") if Alexa.config.secret_access_key.nil?
12
14
  @host = options[:host] or raise ArgumentError.new("you must specify host")
13
15
  @response_group = options[:response_group] || RESPONSE_GROUP
14
16
  end
15
17
 
16
18
  def connect
17
19
  action = "UrlInfo"
18
- timestamp = ( Time::now ).utc.strftime("%Y-%m-%dT%H:%M:%S.000Z")
19
- signature = generate_signature(secret_access_key, action, timestamp)
20
- url = generate_url(action, access_key_id, signature, timestamp, response_group, host)
20
+ timestamp = Time::now.utc.strftime("%Y-%m-%dT%H:%M:%S.000Z")
21
+ signature = generate_signature(Alexa.config.secret_access_key, action, timestamp)
22
+ url = generate_url(action, Alexa.config.access_key_id, signature, timestamp, response_group, host)
21
23
  response = Net::HTTP.start(url.host) do |http|
22
24
  http.get url.request_uri
23
25
  end
@@ -74,7 +76,7 @@ module Alexa
74
76
  end
75
77
  end
76
78
 
77
- def generate_signature(secret_acces_key, action, timestamp)
79
+ def generate_signature(secret_access_key, action, timestamp)
78
80
  Base64.encode64( OpenSSL::HMAC.digest( OpenSSL::Digest::Digest.new( "sha1" ), secret_access_key, action + timestamp)).strip
79
81
  end
80
82
 
@@ -0,0 +1,38 @@
1
+ require 'test_helper'
2
+
3
+ class ConfigTest < Test::Unit::TestCase
4
+ context "Config test" do
5
+ setup do
6
+ # Config is singleton
7
+ Alexa.config.access_key_id = nil
8
+ Alexa.config.secret_access_key = nil
9
+ end
10
+
11
+ should "raise argumment error if access key id is not present" do
12
+ assert_raise ArgumentError do
13
+ Alexa::UrlInfo.new(
14
+ :secret_access_key => "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDF",
15
+ :host => "some.host"
16
+ )
17
+ end
18
+ end
19
+
20
+ should "raise argumment error if secret access key is not present" do
21
+ assert_raise ArgumentError do
22
+ Alexa::UrlInfo.new(
23
+ :access_key_id => "12345678901234567890",
24
+ :host => "some.host"
25
+ )
26
+ end
27
+ end
28
+
29
+ should "raise argumment error if host is not present" do
30
+ assert_raise ArgumentError do
31
+ Alexa::UrlInfo.new(
32
+ :access_key_id => "12345678901234567890",
33
+ :secret_access_key => "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDF"
34
+ )
35
+ end
36
+ end
37
+ end
38
+ end
data/test/test_helper.rb CHANGED
@@ -6,9 +6,6 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
6
  $LOAD_PATH.unshift(File.dirname(__FILE__))
7
7
  require 'alexa'
8
8
 
9
- class Test::Unit::TestCase
10
- end
11
-
12
9
  def fixture_file(filename)
13
10
  return '' if filename == ''
14
11
  file_path = File.expand_path(File.dirname(__FILE__) + '/fixtures/' + filename)
@@ -1,6 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
- class AlexaTest < Test::Unit::TestCase
3
+ class UrlInfoTest < Test::Unit::TestCase
4
4
  context "Alexa::UrlInfo" do
5
5
  setup do
6
6
  @alexa = Alexa::UrlInfo.new(
@@ -11,14 +11,14 @@ class AlexaTest < Test::Unit::TestCase
11
11
  end
12
12
 
13
13
  should "Generate signature" do
14
- signature = @alexa.send :generate_signature, @alexa.secret_access_key, "UrlInfo", '2009-07-03T07:22:24.000Z'
14
+ signature = @alexa.send :generate_signature, Alexa.config.secret_access_key, "UrlInfo", '2009-07-03T07:22:24.000Z'
15
15
  assert_equal "I1mPdBy+flhhzqqUaamNq9gq190=", signature
16
16
  end
17
17
 
18
18
  should "Generate url" do
19
19
  url = @alexa.send(:generate_url,
20
20
  "UrlInfo",
21
- @alexa.access_key_id,
21
+ Alexa.config.access_key_id,
22
22
  "I1mPdBy+flhhzqqUaamNq9gq190=",
23
23
  '2009-07-03T07:22:24.000Z',
24
24
  "Rank,ContactInfo,AdultContent,Speed,Language,Keywords,OwnedDomains,LinksInCount,SiteData,RelatedLinks",
@@ -192,27 +192,4 @@ class AlexaTest < Test::Unit::TestCase
192
192
  end
193
193
  end
194
194
  end
195
-
196
- should "Raise argumment error if keys or host are not present" do
197
- assert_raise ArgumentError do
198
- Alexa::UrlInfo.new(
199
- :secret_access_key => "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDF",
200
- :host => "some.host"
201
- )
202
- end
203
-
204
- assert_raise ArgumentError do
205
- Alexa::UrlInfo.new(
206
- :access_key_id => "12345678901234567890",
207
- :host => "some.host"
208
- )
209
- end
210
-
211
- assert_raise ArgumentError do
212
- Alexa::UrlInfo.new(
213
- :access_key_id => "12345678901234567890",
214
- :secret_access_key => "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDF"
215
- )
216
- end
217
- end
218
195
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alexa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Wojciech Wn\xC4\x99trzak"
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-03 00:00:00 +01:00
12
+ date: 2010-01-28 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -40,12 +40,14 @@ files:
40
40
  - VERSION
41
41
  - alexa.gemspec
42
42
  - lib/alexa.rb
43
+ - lib/alexa/config.rb
43
44
  - lib/alexa/url_info.rb
44
- - test/alexa_test.rb
45
+ - test/config_test.rb
45
46
  - test/fixtures/empty.xml
46
47
  - test/fixtures/polsl.xml
47
48
  - test/fixtures/polsl_small.xml
48
49
  - test/test_helper.rb
50
+ - test/url_info_test.rb
49
51
  has_rdoc: true
50
52
  homepage: http://github.com/morgoth/alexa
51
53
  licenses: []
@@ -75,5 +77,6 @@ signing_key:
75
77
  specification_version: 3
76
78
  summary: Alexa Web Information Service library
77
79
  test_files:
78
- - test/alexa_test.rb
80
+ - test/url_info_test.rb
81
+ - test/config_test.rb
79
82
  - test/test_helper.rb