sucker 1.2.0 → 1.3.0.pre

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. data/LICENSE +1 -1
  2. data/README.md +45 -20
  3. data/lib/sucker/parameters.rb +11 -7
  4. data/lib/sucker/parameters.rbc +723 -0
  5. data/lib/sucker/request.rb +52 -154
  6. data/lib/sucker/request.rbc +2719 -0
  7. data/lib/sucker/response.rb +14 -36
  8. data/lib/sucker/response.rbc +1307 -0
  9. data/lib/sucker/version.rb +3 -1
  10. data/lib/sucker/version.rbc +130 -0
  11. data/lib/sucker.rb +4 -2
  12. data/lib/sucker.rbc +286 -0
  13. data/spec/fixtures/cassette_library/0394751221.yml +26 -0
  14. data/spec/fixtures/cassette_library/0415246334.yml +26 -0
  15. data/spec/fixtures/cassette_library/0679753354.yml +26 -0
  16. data/spec/fixtures/cassette_library/0816614024-0007218095.yml +26 -0
  17. data/spec/fixtures/cassette_library/0816614024-0143105825.yml +26 -0
  18. data/spec/fixtures/cassette_library/0816614024.yml +26 -0
  19. data/spec/fixtures/cassette_library/482224816x.yml +26 -0
  20. data/spec/fixtures/cassette_library/816614024.yml +151 -0
  21. data/spec/fixtures/cassette_library/a2h6nh4sqyfz4m.yml +26 -0
  22. data/spec/fixtures/cassette_library/a2jyso6w6kep83.yml +26 -0
  23. data/spec/fixtures/cassette_library/author-lacan-or-deleuze-and-not-fiction.yml +26 -0
  24. data/spec/fixtures/cassette_library/b000aspues.yml +26 -0
  25. data/spec/fixtures/cassette_library/deleuze-binding-kindle.yml +26 -0
  26. data/spec/fixtures/cassette_library/deleuze.yml +26 -0
  27. data/spec/fixtures/cassette_library/george-orwell.yml +26 -0
  28. data/spec/fixtures/cassette_library/spec/sucker/request.yml +4 -4
  29. data/spec/fixtures/cassette_library/spec/sucker/response.yml +3 -3
  30. data/spec/spec_helper.rbc +230 -0
  31. data/spec/sucker/parameters_spec.rbc +1476 -0
  32. data/spec/sucker/request_spec.rb +34 -217
  33. data/spec/sucker/request_spec.rbc +2473 -0
  34. data/spec/sucker/response_spec.rb +46 -95
  35. data/spec/sucker/response_spec.rbc +2854 -0
  36. data/spec/sucker_spec.rbc +287 -0
  37. data/spec/support/amazon_credentials.rbc +154 -0
  38. data/spec/support/asins.rbc +335 -0
  39. data/spec/support/vcr.rbc +356 -0
  40. metadata +72 -96
  41. data/CHANGELOG.md +0 -47
@@ -1,8 +1,10 @@
1
- require "active_support/xml_mini/nokogiri"
1
+ # encoding: utf-8
2
+
3
+ require 'active_support/xml_mini/nokogiri'
2
4
 
3
5
  module Sucker #:nodoc:
4
6
 
5
- # A Nokogiri-driven wrapper around the cURL response
7
+ # A Nokogiri-based wrapper around the response
6
8
  class Response
7
9
 
8
10
  # The response body
@@ -11,36 +13,20 @@ module Sucker #:nodoc:
11
13
  # The HTTP status code of the response
12
14
  attr_accessor :code
13
15
 
14
- # Transaction time in seconds for request
15
- attr_accessor :time
16
-
17
- # The request URI
18
- attr_accessor :uri
19
-
20
- def initialize(curl)
21
- self.body = curl.body_str
22
- self.code = curl.response_code
23
- self.time = curl.total_time
24
- self.uri = curl.url
25
- end
26
-
27
- # A shorthand that yields each match to a block
28
- #
29
- # worker.get.each("Item") { |item| process(item) }
30
- #
31
- def each(path)
32
- find(path).each { |e| yield e }
16
+ def initialize(response)
17
+ self.body = response.body
18
+ self.code = response.code
33
19
  end
34
20
 
35
21
  # Returns an array of errors in the reponse
36
22
  def errors
37
- find("Error")
23
+ find('Error')
38
24
  end
39
25
 
40
26
  # Queries an xpath and returns an array of matching nodes
41
27
  #
42
28
  # response = worker.get
43
- # response.find("Item").each { |item| ... }
29
+ # items = response.find('Item')
44
30
  #
45
31
  def find(path)
46
32
  xml.xpath("//xmlns:#{path}").map { |e| strip_content(e.to_hash[path]) }
@@ -51,15 +37,7 @@ module Sucker #:nodoc:
51
37
  errors.count > 0
52
38
  end
53
39
 
54
- # A shorthand that yields matches to a block and collects returned values
55
- #
56
- # descriptions = worker.get.map("Item") { |item| build_description(item) }
57
- #
58
- def map(path)
59
- find(path).map { |e| yield e }
60
- end
61
-
62
- # Parses the response into a simple hash
40
+ # Parses response into a simple hash
63
41
  #
64
42
  # response = worker.get
65
43
  # response.to_hash
@@ -75,7 +53,7 @@ module Sucker #:nodoc:
75
53
  # => true
76
54
  #
77
55
  def valid?
78
- code == 200
56
+ code == '200'
79
57
  end
80
58
 
81
59
  # The XML document
@@ -88,14 +66,14 @@ module Sucker #:nodoc:
88
66
 
89
67
  private
90
68
 
91
- # Let's massage that hash
69
+ # Massage hash
92
70
  def strip_content(node)
93
71
  case node
94
72
  when Array
95
73
  node.map { |child| strip_content(child) }
96
74
  when Hash
97
- if node.keys.size == 1 && node["__content__"]
98
- node["__content__"]
75
+ if node.keys.size == 1 && node['__content__']
76
+ node['__content__']
99
77
  else
100
78
  node.inject({}) do |attributes, key_value|
101
79
  attributes.merge({ key_value.first => strip_content(key_value.last) })