abebooks4r 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
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.
@@ -0,0 +1,49 @@
1
+ = abebooks4r
2
+
3
+ abebooks4r provides high level ruby interface for AbeBooks REST service API.
4
+
5
+ == Installation
6
+
7
+ $ gem sources -a http://gemcutter.org
8
+ $ sudo gem install abebooks4r
9
+
10
+ Abebooks4r requires hpricot gem
11
+
12
+ $ sudo gem install hpricot
13
+
14
+ == Usage
15
+
16
+ require 'abebooks4r'
17
+
18
+ Abebooks4r::Abe.configure do |options|
19
+ options[:clientkey] = ABE_ACCESS_KEY
20
+ end
21
+
22
+ res = Abebooks4r::Abe.search(:author => 'Brad Ediger', :title => 'Advanced Rails')
23
+
24
+ unless res.has_error?
25
+ res.books.each do |book|
26
+ puts book.get('title')
27
+ puts book.get('author')
28
+ end
29
+ end
30
+
31
+ == Links
32
+
33
+ http://www.abebooks.de/docs/AffiliateProgram/WebServices/
34
+ http://hasham2.blogspot.com/
35
+
36
+ == Note on Patches/Pull Requests
37
+
38
+ * Fork the project.
39
+ * Make your feature addition or bug fix.
40
+ * Add tests for it. This is important so I don't break it in a
41
+ future version unintentionally.
42
+ * Commit, do not mess with rakefile, version, or history.
43
+ (if you want to have your own version, that is fine but
44
+ bump version in a commit by itself I can ignore when I pull)
45
+ * Send me a pull request. Bonus points for topic branches.
46
+
47
+ == Copyright
48
+
49
+ Copyright (c) 2009 Hasham Malik. See LICENSE for details.
@@ -0,0 +1,52 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "abebooks4r"
8
+ gem.summary = %Q{abebooks4r provides high level ruby interface for AbeBooks REST service API}
9
+ gem.description = %Q{Abebooks is a service to buy and sell new and used books online. abebooks4r provides high level ruby interface for AbeBooks REST service API}
10
+ gem.email = "hasham2@gmail.com"
11
+ gem.homepage = "http://github.com/hasham2/abebooks4r"
12
+ gem.authors = ["Hasham Malik"]
13
+ gem.add_dependency('hpricot', '>= 0.4')
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/test_*.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
47
+
48
+ rdoc.rdoc_dir = 'rdoc'
49
+ rdoc.title = "abebooks4r #{version}"
50
+ rdoc.rdoc_files.include('README*')
51
+ rdoc.rdoc_files.include('lib/**/*.rb')
52
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,54 @@
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{abebooks4r}
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-26}
13
+ s.description = %q{Abebooks is a service to buy and sell new and used books online. abebooks4r provides high level ruby interface for AbeBooks REST service API}
14
+ s.email = %q{hasham2@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "abebooks4r.gemspec",
27
+ "lib/abebooks4r.rb",
28
+ "test/helper.rb",
29
+ "test/test_abebooks4r.rb"
30
+ ]
31
+ s.homepage = %q{http://github.com/hasham2/abebooks4r}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.5}
35
+ s.summary = %q{abebooks4r provides high level ruby interface for AbeBooks REST service API}
36
+ s.test_files = [
37
+ "test/helper.rb",
38
+ "test/test_abebooks4r.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
46
+ s.add_runtime_dependency(%q<hpricot>, [">= 0.4"])
47
+ else
48
+ s.add_dependency(%q<hpricot>, [">= 0.4"])
49
+ end
50
+ else
51
+ s.add_dependency(%q<hpricot>, [">= 0.4"])
52
+ end
53
+ end
54
+
@@ -0,0 +1,244 @@
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 "uri"
27
+ require "net/https"
28
+ require "hpricot"
29
+
30
+ module Abebooks4r
31
+
32
+ class RequestError < StandardError; end
33
+
34
+ class Abe
35
+
36
+ @@options = {:clientkey => ''}
37
+ @@debug = false
38
+
39
+ # Default service options
40
+ def self.options
41
+ @@options
42
+ end
43
+
44
+ # Set default service options
45
+ def self.options=(opts)
46
+ @@options = opts
47
+ end
48
+
49
+ # Get debug flag.
50
+ def self.debug
51
+ @@debug
52
+ end
53
+
54
+ # Set debug flag to true or false.
55
+ def self.debug=(dbg)
56
+ @@debug = dbg
57
+ end
58
+
59
+ def self.configure(&proc)
60
+ raise ArgumentError, "Block is required." unless block_given?
61
+ yield @@options
62
+ end
63
+
64
+ #search abe books web service for the book
65
+ def self.search(params = {})
66
+ url = self.prepare_url(params)
67
+ log "Request URL: #{url}"
68
+ res = Net::HTTP.get_response(url)
69
+ unless res.kind_of? Net::HTTPSuccess
70
+ raise Abebooks4r::RequestError, "HTTP Response: #{res.code} #{res.message}"
71
+ end
72
+ log "Response text: #{res.body}"
73
+ Response.new(res.body)
74
+ end
75
+
76
+
77
+ # Response object returned after a REST call to Amazon service.
78
+ class Response
79
+ # XML input is in string format
80
+ def initialize(xml)
81
+ @doc = Hpricot(xml)
82
+ end
83
+
84
+ # Return Hpricot object.
85
+ def doc
86
+ @doc
87
+ end
88
+
89
+ # Return true if response has an error.
90
+ def has_error?
91
+ !(error.nil? || error.empty?)
92
+ end
93
+
94
+ # Return error message.
95
+ def error
96
+ Element.get(@doc, "searchresults/messages")
97
+ end
98
+
99
+ # Return error code
100
+ def error_message
101
+ Element.get(@doc, "searchresults/messages")
102
+ end
103
+
104
+ # Return an array of Abebooks4r::Element item objects.
105
+ def books
106
+ unless @books
107
+ @books = (@doc/"book").collect {|item| Element.new(item)}
108
+ end
109
+ @books
110
+ end
111
+
112
+ # Return total results.
113
+ def total_results
114
+ unless @total_results
115
+ @total_results = (@doc/"resultcount").inner_html.to_i
116
+ end
117
+ @total_results
118
+ end
119
+ end
120
+
121
+ protected
122
+
123
+ def self.log(s)
124
+ return unless self.debug
125
+ if defined? RAILS_DEFAULT_LOGGER
126
+ RAILS_DEFAULT_LOGGER.error(s)
127
+ elsif defined? LOGGER
128
+ LOGGER.error(s)
129
+ else
130
+ puts s
131
+ end
132
+ end
133
+
134
+ private
135
+
136
+ def self.prepare_url(params)
137
+ params = params.merge(self.options)
138
+ url = URI.parse("http://search2.abebooks.com/search?" +
139
+ params.to_a.collect{|item| item.first.id2name + "=" + CGI::escape(item.last) }.join("&")
140
+ )
141
+ return url
142
+ end
143
+ end
144
+
145
+ # Internal wrapper class to provide convenient method to access Hpricot element value.
146
+ class Element
147
+
148
+ # Pass Hpricot::Elements object
149
+ def initialize(element)
150
+ @element = element
151
+ end
152
+
153
+ # Returns Hpricot::Elments object
154
+ def elem
155
+ @element
156
+ end
157
+
158
+ # Find Hpricot::Elements matching the given path. Example: element/"author".
159
+ def /(path)
160
+ elements = @element/path
161
+ return nil if elements.size == 0
162
+ elements
163
+ end
164
+
165
+ # Find Hpricot::Elements matching the given path, and convert to Abebooks4r::Element.
166
+ # Returns an array Abebooks4r::Elements if more than Hpricot::Elements size is greater than 1.
167
+ def search_and_convert(path)
168
+ elements = self./(path)
169
+ return unless elements
170
+ elements = elements.map{|element| Element.new(element)}
171
+ return elements.first if elements.size == 1
172
+ elements
173
+ end
174
+
175
+ # Get the text value of the given path, leave empty to retrieve current element value.
176
+ def get(path='')
177
+ Element.get(@element, path)
178
+ end
179
+
180
+ # Get the unescaped HTML text of the given path.
181
+ def get_unescaped(path='')
182
+ Element.get_unescaped(@element, path)
183
+ end
184
+
185
+ # Get the array values of the given path.
186
+ def get_array(path='')
187
+ Element.get_array(@element, path)
188
+ end
189
+
190
+ # Get the children element text values in hash format with the element names as the hash keys.
191
+ def get_hash(path='')
192
+ Element.get_hash(@element, path)
193
+ end
194
+
195
+ # Similar to #get, except an element object must be passed-in.
196
+ def self.get(element, path='')
197
+ return unless element
198
+ result = element.at(path)
199
+ result = result.inner_html if result
200
+ result
201
+ end
202
+
203
+ # Similar to #get_unescaped, except an element object must be passed-in.
204
+ def self.get_unescaped(element, path='')
205
+ result = get(element, path)
206
+ CGI::unescapeHTML(result) if result
207
+ end
208
+
209
+ # Similar to #get_array, except an element object must be passed-in.
210
+ def self.get_array(element, path='')
211
+ return unless element
212
+
213
+ result = element/path
214
+ if (result.is_a? Hpricot::Elements) || (result.is_a? Array)
215
+ parsed_result = []
216
+ result.each {|item|
217
+ parsed_result << Element.get(item)
218
+ }
219
+ parsed_result
220
+ else
221
+ [Element.get(result)]
222
+ end
223
+ end
224
+
225
+ # Similar to #get_hash, except an element object must be passed-in.
226
+ def self.get_hash(element, path='')
227
+ return unless element
228
+
229
+ result = element.at(path)
230
+ if result
231
+ hash = {}
232
+ result = result.children
233
+ result.each do |item|
234
+ hash[item.name.to_sym] = item.inner_html
235
+ end
236
+ hash
237
+ end
238
+ end
239
+
240
+ def to_s
241
+ elem.to_s if elem
242
+ end
243
+ end
244
+ end
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+
4
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ require 'abebooks4r'
7
+
8
+ class Test::Unit::TestCase
9
+ end
@@ -0,0 +1,35 @@
1
+ require 'helper'
2
+
3
+ class TestAbebooks4r < Test::Unit::TestCase
4
+
5
+ ABE_ACCESS_KEY = ''
6
+
7
+ raise "Please specify set your ABE_ACCESS_KEY" if ABE_ACCESS_KEY.empty?
8
+
9
+
10
+ Abebooks4r::Abe.configure do |options|
11
+ options[:clientkey] = ABE_ACCESS_KEY
12
+ end
13
+
14
+ #set a switch for debugging
15
+ Abebooks4r::Abe.debug = true
16
+
17
+ #test the result count from service
18
+ def test_abebooks_total_result_count
19
+ resp = Abebooks4r::Abe.search(:author => 'Brad Ediger', :title => 'Advanced Rails')
20
+ assert(resp.total_results == 41) #this varies time to time
21
+ end
22
+
23
+ #test the composition of book obect returned
24
+ def test_abebooks_book_object
25
+ resp = Abebooks4r::Abe.search(:author => 'Brad Ediger', :title => 'Advanced Rails')
26
+ assert(resp.books.first.get('title') == 'Advanced Rails')
27
+ assert(resp.books.first.get('author') == 'Ediger, Brad')
28
+ end
29
+
30
+ #test error detection on service response
31
+ def test_abebooks_error
32
+ resp = Abebooks4r::Abe.search(:authr => 'Brad Ediger', :title => 'Advanced Rails')
33
+ assert(resp.has_error? == true)
34
+ end
35
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: abebooks4r
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-26 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: Abebooks is a service to buy and sell new and used books online. abebooks4r provides high level ruby interface for AbeBooks REST service API
26
+ email: hasham2@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - VERSION
41
+ - abebooks4r.gemspec
42
+ - lib/abebooks4r.rb
43
+ - test/helper.rb
44
+ - test/test_abebooks4r.rb
45
+ has_rdoc: true
46
+ homepage: http://github.com/hasham2/abebooks4r
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options:
51
+ - --charset=UTF-8
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.3.5
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: abebooks4r provides high level ruby interface for AbeBooks REST service API
73
+ test_files:
74
+ - test/helper.rb
75
+ - test/test_abebooks4r.rb