sucker 0.8.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +4 -10
- data/lib/sucker.rb +10 -2
- data/lib/sucker/active_support/core_ext/object/blank.rb +77 -0
- data/lib/sucker/active_support/xml_mini/nokogiri.rb +77 -0
- data/lib/sucker/request.rb +2 -2
- data/lib/sucker/stub.rb +1 -3
- metadata +13 -31
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Sucker
|
2
2
|
======
|
3
3
|
|
4
|
-
Sucker is a paper-thin Ruby wrapper to the [Amazon Product Advertising API](https://affiliate-program.amazon.co.uk/gp/advertising/api/detail/main.html). It runs on cURL and Nokogiri and supports
|
4
|
+
Sucker is a paper-thin Ruby wrapper to the [Amazon Product Advertising API](https://affiliate-program.amazon.co.uk/gp/advertising/api/detail/main.html). It runs on cURL and Nokogiri and supports __the entire API__.
|
5
5
|
|
6
6
|
![Sucker](http://upload.wikimedia.org/wikipedia/commons/thumb/f/f8/FEMA_-_32011_-_FEMA_Joint_Field_Office_%28JFO%29_preparation_in_Ohio.jpg/540px-FEMA_-_32011_-_FEMA_Joint_Field_Office_%28JFO%29_preparation_in_Ohio.jpg)
|
7
7
|
|
@@ -15,7 +15,7 @@ Set up a worker.
|
|
15
15
|
:key => "API KEY",
|
16
16
|
:secret => "API SECRET")
|
17
17
|
|
18
|
-
Optionally, fiddle with curl. Say you want to
|
18
|
+
Optionally, fiddle with curl. Say you want to query Amazon through a different network interface:
|
19
19
|
|
20
20
|
worker.curl { |c| c.interface = "eth1" }
|
21
21
|
|
@@ -75,18 +75,12 @@ In your spec, you can now stub the worker:
|
|
75
75
|
|
76
76
|
The first time you run the spec, Sucker will perform the actual request. Following requests will use a cached response.
|
77
77
|
|
78
|
-
Active Support
|
79
|
-
--------------
|
80
|
-
Sucker::Response relies on the Nokogiri implementation of the XmlMini parser in Active Support (~> 3.0).
|
81
|
-
|
82
|
-
In a non-Rails environment, that merely means requiring twenty-something lines of code from Active Support.
|
83
|
-
|
84
78
|
Compatibility
|
85
79
|
-------------
|
86
80
|
|
87
|
-
Specs pass against Ruby 1.8.7
|
81
|
+
Specs pass against Ruby 1.8.7 and 1.9.2.
|
88
82
|
|
89
83
|
Todo
|
90
84
|
----
|
91
85
|
|
92
|
-
*
|
86
|
+
* Rip out the Stub class and use VCR instead once Webmock gets a cURL adapter.
|
data/lib/sucker.rb
CHANGED
@@ -1,14 +1,22 @@
|
|
1
|
-
require "active_support/xml_mini/nokogiri"
|
2
1
|
require "cgi"
|
3
2
|
require "curb"
|
4
3
|
require "digest/md5"
|
4
|
+
require "nokogiri"
|
5
5
|
require "sucker/request"
|
6
6
|
require "sucker/response"
|
7
|
+
require "uri"
|
8
|
+
|
9
|
+
if Gem.available?("activesupport", ">= 2.3.2")
|
10
|
+
require "active_support/xml_mini/nokogiri"
|
11
|
+
else
|
12
|
+
require "sucker/active_support/core_ext/object/blank"
|
13
|
+
require "sucker/active_support/xml_mini/nokogiri"
|
14
|
+
end
|
7
15
|
|
8
16
|
# = Sucker
|
9
17
|
# Sucker is a paper-thin Ruby wrapper to the Amazon Product Advertising API.
|
10
18
|
module Sucker
|
11
|
-
AMAZON_API_VERSION = "2010-06-01"
|
19
|
+
AMAZON_API_VERSION = "2010-06-01"
|
12
20
|
|
13
21
|
# Instantiates a new Sucker::Request
|
14
22
|
def self.new(args={})
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# http://github.com/rails/rails/blob/v3.0.0/activesupport/core_ext/object/blank.rb
|
2
|
+
class Object
|
3
|
+
# An object is blank if it's false, empty, or a whitespace string.
|
4
|
+
# For example, "", " ", +nil+, [], and {} are blank.
|
5
|
+
#
|
6
|
+
# This simplifies:
|
7
|
+
#
|
8
|
+
# if !address.nil? && !address.empty?
|
9
|
+
#
|
10
|
+
# ...to:
|
11
|
+
#
|
12
|
+
# if !address.blank?
|
13
|
+
def blank?
|
14
|
+
respond_to?(:empty?) ? empty? : !self
|
15
|
+
end
|
16
|
+
|
17
|
+
# An object is present if it's not blank.
|
18
|
+
def present?
|
19
|
+
!blank?
|
20
|
+
end
|
21
|
+
|
22
|
+
# Returns object if it's #present? otherwise returns nil.
|
23
|
+
# object.presence is equivalent to object.present? ? object : nil.
|
24
|
+
#
|
25
|
+
# This is handy for any representation of objects where blank is the same
|
26
|
+
# as not present at all. For example, this simplifies a common check for
|
27
|
+
# HTTP POST/query parameters:
|
28
|
+
#
|
29
|
+
# state = params[:state] if params[:state].present?
|
30
|
+
# country = params[:country] if params[:country].present?
|
31
|
+
# region = state || country || 'US'
|
32
|
+
#
|
33
|
+
# ...becomes:
|
34
|
+
#
|
35
|
+
# region = params[:state].presence || params[:country].presence || 'US'
|
36
|
+
def presence
|
37
|
+
self if present?
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class NilClass #:nodoc:
|
42
|
+
def blank?
|
43
|
+
true
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
class FalseClass #:nodoc:
|
48
|
+
def blank?
|
49
|
+
true
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class TrueClass #:nodoc:
|
54
|
+
def blank?
|
55
|
+
false
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class Array #:nodoc:
|
60
|
+
alias_method :blank?, :empty?
|
61
|
+
end
|
62
|
+
|
63
|
+
class Hash #:nodoc:
|
64
|
+
alias_method :blank?, :empty?
|
65
|
+
end
|
66
|
+
|
67
|
+
class String #:nodoc:
|
68
|
+
def blank?
|
69
|
+
self !~ /\S/
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
class Numeric #:nodoc:
|
74
|
+
def blank?
|
75
|
+
false
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# http://github.com/rails/rails/blob/v3.0.0/activesupport/lib/active_support/xml_mini/nokogiri.rb
|
2
|
+
module Sucker
|
3
|
+
module ActiveSupport
|
4
|
+
module XmlMini_Nokogiri #:nodoc:
|
5
|
+
extend self
|
6
|
+
|
7
|
+
# Parse an XML Document string or IO into a simple hash using libxml / nokogiri.
|
8
|
+
# data::
|
9
|
+
# XML Document string or IO to parse
|
10
|
+
def parse(data)
|
11
|
+
if !data.respond_to?(:read)
|
12
|
+
data = StringIO.new(data || '')
|
13
|
+
end
|
14
|
+
|
15
|
+
char = data.getc
|
16
|
+
if char.nil?
|
17
|
+
{}
|
18
|
+
else
|
19
|
+
data.ungetc(char)
|
20
|
+
doc = Nokogiri::XML(data)
|
21
|
+
raise doc.errors.first if doc.errors.length > 0
|
22
|
+
doc.to_hash
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
module Conversions #:nodoc:
|
27
|
+
module Document #:nodoc:
|
28
|
+
def to_hash
|
29
|
+
root.to_hash
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
module Node #:nodoc:
|
34
|
+
CONTENT_ROOT = '__content__'.freeze
|
35
|
+
|
36
|
+
# Convert XML document to hash
|
37
|
+
#
|
38
|
+
# hash::
|
39
|
+
# Hash to merge the converted element into.
|
40
|
+
def to_hash(hash={})
|
41
|
+
node_hash = {}
|
42
|
+
|
43
|
+
# Insert node hash into parent hash correctly.
|
44
|
+
case hash[name]
|
45
|
+
when Array then hash[name] << node_hash
|
46
|
+
when Hash then hash[name] = [hash[name], node_hash]
|
47
|
+
when nil then hash[name] = node_hash
|
48
|
+
end
|
49
|
+
|
50
|
+
# Handle child elements
|
51
|
+
children.each do |c|
|
52
|
+
if c.element?
|
53
|
+
c.to_hash(node_hash)
|
54
|
+
elsif c.text? || c.cdata?
|
55
|
+
node_hash[CONTENT_ROOT] ||= ''
|
56
|
+
node_hash[CONTENT_ROOT] << c.content
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# Remove content node if it is blank and there are child tags
|
61
|
+
if node_hash.length > 1 && node_hash[CONTENT_ROOT].blank?
|
62
|
+
node_hash.delete(CONTENT_ROOT)
|
63
|
+
end
|
64
|
+
|
65
|
+
# Handle attributes
|
66
|
+
attribute_nodes.each { |a| node_hash[a.node_name] = a.value }
|
67
|
+
|
68
|
+
hash
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
Nokogiri::XML::Document.send(:include, Conversions::Document)
|
74
|
+
Nokogiri::XML::Node.send(:include, Conversions::Node)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
data/lib/sucker/request.rb
CHANGED
@@ -8,8 +8,8 @@ module Sucker
|
|
8
8
|
:de => 'ecs.amazonaws.de',
|
9
9
|
:ca => 'ecs.amazonaws.ca',
|
10
10
|
:fr => 'ecs.amazonaws.fr',
|
11
|
-
:jp => 'ecs.amazonaws.jp' }
|
12
|
-
PATH = "/onca/xml"
|
11
|
+
:jp => 'ecs.amazonaws.jp' }
|
12
|
+
PATH = "/onca/xml"
|
13
13
|
|
14
14
|
# The Amazon locale to query
|
15
15
|
attr_accessor :locale
|
data/lib/sucker/stub.rb
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
module Sucker
|
2
2
|
|
3
|
-
# Stubs Sucker::Response to run specs offline.
|
4
|
-
# a general-purpose stubber for Curb but last time I checked, there was
|
5
|
-
# none.
|
3
|
+
# Stubs Sucker::Response to run specs offline.
|
6
4
|
class MockResponse < Response
|
7
5
|
def initialize(mock_response_body)
|
8
6
|
self.body = mock_response_body
|
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sucker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 63
|
5
4
|
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 0
|
8
|
-
-
|
7
|
+
- 9
|
9
8
|
- 0
|
10
|
-
version: 0.
|
9
|
+
version: 0.9.0
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- Hakan Ensari
|
@@ -16,57 +15,39 @@ autorequire:
|
|
16
15
|
bindir: bin
|
17
16
|
cert_chain: []
|
18
17
|
|
19
|
-
date: 2010-09-
|
18
|
+
date: 2010-09-19 00:00:00 +01:00
|
20
19
|
default_executable:
|
21
20
|
dependencies:
|
22
21
|
- !ruby/object:Gem::Dependency
|
23
|
-
type: :runtime
|
24
|
-
prerelease: false
|
25
|
-
name: activesupport
|
26
|
-
version_requirements: &id001 !ruby/object:Gem::Requirement
|
27
|
-
none: false
|
28
|
-
requirements:
|
29
|
-
- - ~>
|
30
|
-
- !ruby/object:Gem::Version
|
31
|
-
hash: 7
|
32
|
-
segments:
|
33
|
-
- 3
|
34
|
-
- 0
|
35
|
-
- 0
|
36
|
-
version: 3.0.0
|
37
|
-
requirement: *id001
|
38
|
-
- !ruby/object:Gem::Dependency
|
39
|
-
type: :runtime
|
40
|
-
prerelease: false
|
41
22
|
name: nokogiri
|
42
|
-
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
43
24
|
none: false
|
44
25
|
requirements:
|
45
26
|
- - ~>
|
46
27
|
- !ruby/object:Gem::Version
|
47
|
-
hash: 7
|
48
28
|
segments:
|
49
29
|
- 1
|
50
30
|
- 4
|
51
31
|
- 0
|
52
32
|
version: 1.4.0
|
53
|
-
requirement: *id002
|
54
|
-
- !ruby/object:Gem::Dependency
|
55
33
|
type: :runtime
|
56
34
|
prerelease: false
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
57
37
|
name: curb
|
58
|
-
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
59
39
|
none: false
|
60
40
|
requirements:
|
61
41
|
- - ~>
|
62
42
|
- !ruby/object:Gem::Version
|
63
|
-
hash: 3
|
64
43
|
segments:
|
65
44
|
- 0
|
66
45
|
- 7
|
67
46
|
- 0
|
68
47
|
version: 0.7.0
|
69
|
-
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: *id002
|
70
51
|
description: A paper-thin Ruby wrapper to the Amazon Product Advertising API
|
71
52
|
email: code@papercavalier.com
|
72
53
|
executables: []
|
@@ -79,6 +60,8 @@ extra_rdoc_files:
|
|
79
60
|
files:
|
80
61
|
- LICENSE
|
81
62
|
- lib/sucker.rb
|
63
|
+
- lib/sucker/active_support/core_ext/object/blank.rb
|
64
|
+
- lib/sucker/active_support/xml_mini/nokogiri.rb
|
82
65
|
- lib/sucker/request.rb
|
83
66
|
- lib/sucker/response.rb
|
84
67
|
- lib/sucker/stub.rb
|
@@ -111,7 +94,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
111
94
|
requirements:
|
112
95
|
- - ">="
|
113
96
|
- !ruby/object:Gem::Version
|
114
|
-
hash:
|
97
|
+
hash: -4366708092572469247
|
115
98
|
segments:
|
116
99
|
- 0
|
117
100
|
version: "0"
|
@@ -120,7 +103,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
120
103
|
requirements:
|
121
104
|
- - ">="
|
122
105
|
- !ruby/object:Gem::Version
|
123
|
-
hash: 3
|
124
106
|
segments:
|
125
107
|
- 0
|
126
108
|
version: "0"
|