amazon-awis 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ pkg/*.gem
@@ -0,0 +1,3 @@
1
+ 0.1.0 2009-10-16
2
+ ----------------
3
+ Initial Release
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2009 Hasham Malik
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
data/README ADDED
@@ -0,0 +1,58 @@
1
+ == amazon-awis
2
+
3
+ Ruby Amazon Alexa web service REST API using Hpricot with configurable
4
+ default options and method call options. Uses Response and
5
+ Element wrapper classes for easy access to REST XML output.
6
+
7
+ Version: 0.1.0
8
+
9
+ == INSTALLATION
10
+
11
+ $ gem install amazon-awis
12
+
13
+ == EXAMPLE
14
+
15
+ require 'amazon/awis'
16
+
17
+ # set the default options; options will be camelized and converted to REST request parameters.
18
+ Amazon::Awis.options = {:aws_access_key_id => [your access key]}
19
+
20
+ # to generate signed requests include your secret key:
21
+ Amazon::Awis.options = {:aws_access_key_id => [your developer token], :aws_secret_key => [your secret access key]}
22
+
23
+ # alternatively,
24
+ Amazon::Awis.configure do |options|
25
+ options[:aws_access_key_id] = [your access key]
26
+ options[:aws_secret_key] = [you secret key]
27
+ options[:responsegroup] = 'Rank'
28
+ end
29
+
30
+ # options provided on method call will merge with the default options
31
+ res = Amazon::Awis.get_info('yahoo.com')
32
+
33
+ # some common response object methods
34
+ res.is_success? # return true if request was successful
35
+
36
+ #dynamic methods
37
+ res.rank # return the rank of domain name that was passed
38
+ res.dataurl # returns the url which was queried
39
+
40
+
41
+ Refer to Amazon ECS documentation for more information on Amazon REST request parameters and XML output:
42
+ http://docs.amazonwebservices.com/AlexaWebInfoService/2005-07-11/
43
+
44
+ == SOURCE CODES
45
+
46
+ * http://github.com/hasham2/amazon-awis/tree/master
47
+
48
+ == LINKS
49
+
50
+ * http://github.com/hasham2/amazon-awis
51
+ * http://hasham2.blogspot.com
52
+
53
+
54
+ == LICENSE
55
+
56
+ (The MIT License)
57
+
58
+ Copyright (c) 2009 Hasham Malik
@@ -0,0 +1,28 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+ require 'rake/gempackagetask'
5
+ require 'rake/packagetask'
6
+
7
+ begin
8
+ require 'jeweler'
9
+ Jeweler::Tasks.new do |gemspec|
10
+ gemspec.name = "amazon-awis"
11
+ gemspec.summary = "Ruby Amazon Alexa web information service REST API"
12
+ gemspec.description = "Ruby Amazon Alexa web information service REST API"
13
+ gemspec.email = "hasham2@gmail.com"
14
+ gemspec.homepage = "http://hasham2.blogspot.com"
15
+ gemspec.authors = ["Hasham Malik"]
16
+ gemspec.add_dependency('hpricot', '>= 0.4')
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler not available. Install it with: sudo gem install jeweler"
21
+ end
22
+
23
+ desc "Run the unit tests in test"
24
+ Rake::TestTask.new(:test) do |t|
25
+ t.libs << "test"
26
+ t.pattern = 'test/**/*_test.rb'
27
+ t.verbose = true
28
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,53 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{amazon-awis}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Hasham Malik"]
12
+ s.date = %q{2009-10-19}
13
+ s.description = %q{Ruby Amazon Alexa web information service REST API}
14
+ s.email = %q{hasham2@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "CHANGELOG",
21
+ "MIT-LICENSE",
22
+ "README",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "amazon-awis.gemspec",
26
+ "lib/amazon/awis.rb",
27
+ "test/amazon/awis_test.rb",
28
+ "test/test_helper.rb"
29
+ ]
30
+ s.homepage = %q{http://hasham2.blogspot.com}
31
+ s.rdoc_options = ["--charset=UTF-8"]
32
+ s.require_paths = ["lib"]
33
+ s.rubygems_version = %q{1.3.5}
34
+ s.summary = %q{Ruby Amazon Alexa web information service REST API}
35
+ s.test_files = [
36
+ "test/amazon/awis_test.rb",
37
+ "test/test_helper.rb"
38
+ ]
39
+
40
+ if s.respond_to? :specification_version then
41
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
42
+ s.specification_version = 3
43
+
44
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
45
+ s.add_runtime_dependency(%q<hpricot>, [">= 0.4"])
46
+ else
47
+ s.add_dependency(%q<hpricot>, [">= 0.4"])
48
+ end
49
+ else
50
+ s.add_dependency(%q<hpricot>, [">= 0.4"])
51
+ end
52
+ end
53
+
@@ -0,0 +1,262 @@
1
+ #--
2
+ # Copyright (c) 2009 Hasham Malik
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18
+ # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19
+ # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20
+ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21
+ # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ require "cgi"
25
+ require "base64"
26
+ require "openssl"
27
+ require "digest/sha1"
28
+ require "uri"
29
+ require "net/https"
30
+ require "time"
31
+ require "hpricot"
32
+
33
+ module Amazon
34
+
35
+ class RequestError < StandardError; end
36
+
37
+ class Awis
38
+
39
+ @@options = {
40
+ :action => "UrlInfo",
41
+ :responsegroup => "Rank"
42
+ }
43
+
44
+ @@debug = false
45
+
46
+ # Default service options
47
+ def self.options
48
+ @@options
49
+ end
50
+
51
+ # Set default service options
52
+ def self.options=(opts)
53
+ @@options = opts
54
+ end
55
+
56
+ # Get debug flag.
57
+ def self.debug
58
+ @@debug
59
+ end
60
+
61
+ # Set debug flag to true or false.
62
+ def self.debug=(dbg)
63
+ @@debug = dbg
64
+ end
65
+
66
+ def self.configure(&proc)
67
+ raise ArgumentError, "Block is required." unless block_given?
68
+ yield @@options
69
+ end
70
+
71
+ def self.get_info(domain)
72
+ url = self.prepare_url(domain)
73
+ log "Request URL: #{url}"
74
+ res = Net::HTTP.get_response(url)
75
+ unless res.kind_of? Net::HTTPSuccess
76
+ raise Amazon::RequestError, "HTTP Response: #{res.code} #{res.message}"
77
+ end
78
+ log "Response text: #{res.body}"
79
+ Response.new(res.body)
80
+ end
81
+
82
+
83
+ # Response object returned after a REST call to Amazon service.
84
+ class Response
85
+ # XML input is in string format
86
+ def initialize(xml)
87
+ @doc = Hpricot(xml)
88
+ end
89
+
90
+ # Return Hpricot object.
91
+ def doc
92
+ @doc
93
+ end
94
+
95
+ # Return true if response has an error.
96
+ def has_error?
97
+ !(error.nil? || error.empty?)
98
+ end
99
+
100
+ # Return error message.
101
+ def error
102
+ Element.get(@doc, "error/message")
103
+ end
104
+
105
+ # Return error code
106
+ def error_code
107
+ Element.get(@doc, "error/code")
108
+ end
109
+
110
+ # Return error message.
111
+ def is_success?
112
+ (@doc/"aws:statuscode").innerHTML == "Success"
113
+ end
114
+
115
+ #returns inner html of any tag in awis response i.e resp.rank => 3
116
+ def method_missing(methodId)
117
+ txt = (@doc/"aws:#{methodId.id2name}").innerHTML
118
+ if txt.empty?
119
+ raise NoMethodError
120
+ else
121
+ txt
122
+ end
123
+ end
124
+
125
+ end
126
+
127
+ protected
128
+
129
+ def self.log(s)
130
+ return unless self.debug
131
+ if defined? RAILS_DEFAULT_LOGGER
132
+ RAILS_DEFAULT_LOGGER.error(s)
133
+ elsif defined? LOGGER
134
+ LOGGER.error(s)
135
+ else
136
+ puts s
137
+ end
138
+ end
139
+
140
+ private
141
+
142
+ def self.prepare_url(domain)
143
+
144
+ timestamp = ( Time::now ).utc.strftime("%Y-%m-%dT%H:%M:%S.000Z")
145
+ secret_key = secret_key = self.options[:aws_secret_key]
146
+ action = self.options[:action]
147
+ signature = Base64.encode64( OpenSSL::HMAC.digest( OpenSSL::Digest::Digest.new( "sha1" ), secret_key, action + timestamp)).strip
148
+ url = URI.parse("http://awis.amazonaws.com/?" +
149
+ {
150
+ "Action" => action,
151
+ "AWSAccessKeyId" => self.options[:aws_access_key_id],
152
+ "Signature" => signature,
153
+ "Timestamp" => timestamp,
154
+ "ResponseGroup" => self.options[:responsegroup],
155
+ "Url" => domain
156
+ }.to_a.collect{|item| item.first + "=" + CGI::escape(item.last) }.join("&")
157
+ )
158
+ return url
159
+
160
+ end
161
+
162
+ end
163
+
164
+ # Internal wrapper class to provide convenient method to access Hpricot element value.
165
+ class Element
166
+ # Pass Hpricot::Elements object
167
+ def initialize(element)
168
+ @element = element
169
+ end
170
+
171
+ # Returns Hpricot::Elments object
172
+ def elem
173
+ @element
174
+ end
175
+
176
+ # Find Hpricot::Elements matching the given path. Example: element/"author".
177
+ def /(path)
178
+ elements = @element/path
179
+ return nil if elements.size == 0
180
+ elements
181
+ end
182
+
183
+ # Find Hpricot::Elements matching the given path, and convert to Amazon::Element.
184
+ # Returns an array Amazon::Elements if more than Hpricot::Elements size is greater than 1.
185
+ def search_and_convert(path)
186
+ elements = self./(path)
187
+ return unless elements
188
+ elements = elements.map{|element| Element.new(element)}
189
+ return elements.first if elements.size == 1
190
+ elements
191
+ end
192
+
193
+ # Get the text value of the given path, leave empty to retrieve current element value.
194
+ def get(path='')
195
+ Element.get(@element, path)
196
+ end
197
+
198
+ # Get the unescaped HTML text of the given path.
199
+ def get_unescaped(path='')
200
+ Element.get_unescaped(@element, path)
201
+ end
202
+
203
+ # Get the array values of the given path.
204
+ def get_array(path='')
205
+ Element.get_array(@element, path)
206
+ end
207
+
208
+ # Get the children element text values in hash format with the element names as the hash keys.
209
+ def get_hash(path='')
210
+ Element.get_hash(@element, path)
211
+ end
212
+
213
+ # Similar to #get, except an element object must be passed-in.
214
+ def self.get(element, path='')
215
+ return unless element
216
+ result = element.at(path)
217
+ result = result.inner_html if result
218
+ result
219
+ end
220
+
221
+ # Similar to #get_unescaped, except an element object must be passed-in.
222
+ def self.get_unescaped(element, path='')
223
+ result = get(element, path)
224
+ CGI::unescapeHTML(result) if result
225
+ end
226
+
227
+ # Similar to #get_array, except an element object must be passed-in.
228
+ def self.get_array(element, path='')
229
+ return unless element
230
+
231
+ result = element/path
232
+ if (result.is_a? Hpricot::Elements) || (result.is_a? Array)
233
+ parsed_result = []
234
+ result.each {|item|
235
+ parsed_result << Element.get(item)
236
+ }
237
+ parsed_result
238
+ else
239
+ [Element.get(result)]
240
+ end
241
+ end
242
+
243
+ # Similar to #get_hash, except an element object must be passed-in.
244
+ def self.get_hash(element, path='')
245
+ return unless element
246
+
247
+ result = element.at(path)
248
+ if result
249
+ hash = {}
250
+ result = result.children
251
+ result.each do |item|
252
+ hash[item.name.to_sym] = item.inner_html
253
+ end
254
+ hash
255
+ end
256
+ end
257
+
258
+ def to_s
259
+ elem.to_s if elem
260
+ end
261
+ end
262
+ end
@@ -0,0 +1,49 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class Amazon::AwisTest < Test::Unit::TestCase
4
+
5
+ AWS_ACCESS_KEY_ID = ''
6
+ AWS_SECRET_KEY = ''
7
+
8
+ DOMAIN_STRING = 'yahoo.com'
9
+
10
+ raise "Please specify set your AWS_ACCESS_KEY_ID" if AWS_ACCESS_KEY_ID.empty?
11
+ raise "Please specify set your AWS_SECRET_KEY" if AWS_SECRET_KEY.empty?
12
+
13
+ Amazon::Awis.configure do |options|
14
+ options[:aws_access_key_id] = AWS_ACCESS_KEY_ID
15
+ options[:aws_secret_key] = AWS_SECRET_KEY
16
+ end
17
+
18
+ Amazon::Awis.debug = true
19
+
20
+ ## Test get_info
21
+ def test_get_info
22
+ Amazon::Awis.configure do |options|
23
+ options[:responsegroup] = 'Rank'
24
+ end
25
+ resp = Amazon::Awis.get_info(DOMAIN_STRING)
26
+ assert(resp.is_success?)
27
+ assert(resp.dataurl == 'yahoo.com/')
28
+ assert(resp.rank == 3.to_s)
29
+ end
30
+
31
+ ## Test get_info with SiteData response group
32
+ def test_site_data_response
33
+ Amazon::Awis.configure do |options|
34
+ options[:responsegroup] = 'SiteData'
35
+ end
36
+ resp = Amazon::Awis.get_info(DOMAIN_STRING)
37
+ assert(resp.title == 'Yahoo!')
38
+ assert(resp.onlinesince == '18-Jan-1995')
39
+ end
40
+
41
+ ## Test get_info with ContactInfo response group
42
+ def test_contact_info_response
43
+ Amazon::Awis.configure do |options|
44
+ options[:responsegroup] = 'ContactInfo'
45
+ end
46
+ resp = Amazon::Awis.get_info(DOMAIN_STRING)
47
+ assert(resp.symbol == 'YHOO')
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/amazon/awis'
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: amazon-awis
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Hasham Malik
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-19 00:00:00 +05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hpricot
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0.4"
24
+ version:
25
+ description: Ruby Amazon Alexa web information service REST API
26
+ email: hasham2@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README
33
+ files:
34
+ - .gitignore
35
+ - CHANGELOG
36
+ - MIT-LICENSE
37
+ - README
38
+ - Rakefile
39
+ - VERSION
40
+ - amazon-awis.gemspec
41
+ - lib/amazon/awis.rb
42
+ - test/amazon/awis_test.rb
43
+ - test/test_helper.rb
44
+ has_rdoc: true
45
+ homepage: http://hasham2.blogspot.com
46
+ licenses: []
47
+
48
+ post_install_message:
49
+ rdoc_options:
50
+ - --charset=UTF-8
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ requirements: []
66
+
67
+ rubyforge_project:
68
+ rubygems_version: 1.3.5
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: Ruby Amazon Alexa web information service REST API
72
+ test_files:
73
+ - test/amazon/awis_test.rb
74
+ - test/test_helper.rb