aitch 0.1.2 → 0.1.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 53e317f071ded556b9eff2bfbf8b222cbfc6735a
4
- data.tar.gz: e24ee0d7e6f28c49b49b22de458f663a7776ff3e
3
+ metadata.gz: 784b17de50a0aa8ff0b9232c153aa67a30f0c713
4
+ data.tar.gz: 96c2f060bb52b68d26a6040f19315ef7578a8a50
5
5
  SHA512:
6
- metadata.gz: 2e19f66a240033ab32c969190dfa4994cfd6da5a6132bbd3fcc9331b4ff12f8d78193474d8be432bb206d9b59c3270657afcdc2d99ea98b93668ea3a6a2aa983
7
- data.tar.gz: 626726af8014cbb4391c7be9da92c469cfc5519cd9f7e842ba874320e20375cfe7fcc4bd35a9e995f028856386a6c52d70218fb8d89a054aa1176fa507bb6643
6
+ metadata.gz: 3e264c4a316fb85601f6cf112ee1aab7d8f17140ed019d1096bf0045f1b3d7052b66a282f5365f9e9b474bfd928a23b1ca99c35e13faa5ea1478282fe7a071b3
7
+ data.tar.gz: 0b70c3a814cb20dab65aaa4293506f596441618d95b4dc3c76a3e2d9a28a6f5f825bac8829191dc60bbb4316d7ce86851747249c7cb25519cf1b9b7ab2df4e0a
data/README.md CHANGED
@@ -10,7 +10,7 @@ A simple HTTP client.
10
10
  Features:
11
11
 
12
12
  * Supports Gzip|Deflate response
13
- * Automatically parses JSON and XML responses
13
+ * Automatically parses JSON, HTML and XML responses
14
14
  * Automatically follows redirect
15
15
 
16
16
  ## Installation
@@ -60,6 +60,9 @@ Aitch.configure do |config|
60
60
 
61
61
  # Set the XML parser.
62
62
  config.xml_parser = Aitch::XMLParser
63
+
64
+ # Set the HTML parser.
65
+ config.html_parser = Aitch::HTMLParser
63
66
  end
64
67
  ```
65
68
 
@@ -94,7 +97,21 @@ response.redirect? # status 3xx
94
97
  response.error? # status 4xx or 5xx
95
98
  response.error # response error
96
99
  response.body # returned body
97
- response.data # JSON or XML payload
100
+ response.data # HTML, JSON or XML payload
101
+ ```
102
+
103
+ #### Parsing XML and HTML with Nokogiri
104
+
105
+ If your response is a XML or a HTML content type, we'll automatically convert the response into a Nokogiri object.
106
+
107
+ ```ruby
108
+ response = Aitch.get("http://simplesideias.com.br")
109
+
110
+ response.data.class
111
+ #=> Nokogiri::HTML::Document
112
+
113
+ response.data.css("h1").size
114
+ #=> 69
98
115
  ```
99
116
 
100
117
  ### Following redirects
@@ -18,6 +18,8 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.add_dependency "nokogiri"
22
+
21
23
  spec.add_development_dependency "bundler"
22
24
  spec.add_development_dependency "rake"
23
25
  spec.add_development_dependency "rspec"
@@ -14,6 +14,7 @@ require "aitch/response/errors"
14
14
  require "aitch/response"
15
15
  require "aitch/response/body"
16
16
  require "aitch/xml_parser"
17
+ require "aitch/html_parser"
17
18
  require "aitch/version"
18
19
 
19
20
  module Aitch
@@ -27,6 +27,9 @@ module Aitch
27
27
  # Set the XML parser.
28
28
  attr_accessor :xml_parser
29
29
 
30
+ # Set the HTML parser.
31
+ attr_accessor :html_parser
32
+
30
33
  def initialize
31
34
  @timeout = 10
32
35
  @redirect_limit = 5
@@ -35,6 +38,7 @@ module Aitch
35
38
  @default_headers = {}
36
39
  @json_parser = JSON
37
40
  @xml_parser = XMLParser
41
+ @html_parser = HTMLParser
38
42
  end
39
43
  end
40
44
  end
@@ -0,0 +1,7 @@
1
+ module Aitch
2
+ module HTMLParser
3
+ def self.load(source)
4
+ Nokogiri::HTML(source.to_s)
5
+ end
6
+ end
7
+ end
@@ -58,6 +58,8 @@ module Aitch
58
58
  @config.json_parser.load(body)
59
59
  elsif xml?
60
60
  @config.xml_parser.load(body)
61
+ elsif html?
62
+ @config.html_parser.load(body)
61
63
  else
62
64
  body
63
65
  end
@@ -1,3 +1,3 @@
1
1
  module Aitch
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -0,0 +1,11 @@
1
+ require "spec_helper"
2
+
3
+ describe Aitch::HTMLParser do
4
+ it "instantiates Nokogiri" do
5
+ Nokogiri
6
+ .should_receive(:HTML)
7
+ .with("HTML")
8
+
9
+ Aitch::HTMLParser.load("HTML")
10
+ end
11
+ end
@@ -158,7 +158,7 @@ describe Aitch::Response do
158
158
  FakeWeb.register_uri(:get, "http://example.org/", body: "Hello", content_type: "text/html")
159
159
  response = Aitch.get("http://example.org/")
160
160
 
161
- expect(response.data).to eql("Hello")
161
+ expect(response.data).to be_a(Nokogiri::HTML::Document)
162
162
  end
163
163
  end
164
164
 
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aitch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-10 00:00:00.000000000 Z
11
+ date: 2013-05-12 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -127,6 +141,7 @@ files:
127
141
  - lib/aitch.rb
128
142
  - lib/aitch/configuration.rb
129
143
  - lib/aitch/errors.rb
144
+ - lib/aitch/html_parser.rb
130
145
  - lib/aitch/namespace.rb
131
146
  - lib/aitch/redirect.rb
132
147
  - lib/aitch/request.rb
@@ -139,6 +154,7 @@ files:
139
154
  - lib/aitch/xml_parser.rb
140
155
  - spec/aitch/aitch_spec.rb
141
156
  - spec/aitch/configuration_spec.rb
157
+ - spec/aitch/html_parser_spec.rb
142
158
  - spec/aitch/namespace_spec.rb
143
159
  - spec/aitch/request_spec.rb
144
160
  - spec/aitch/response_spec.rb
@@ -173,6 +189,7 @@ summary: A simple HTTP client
173
189
  test_files:
174
190
  - spec/aitch/aitch_spec.rb
175
191
  - spec/aitch/configuration_spec.rb
192
+ - spec/aitch/html_parser_spec.rb
176
193
  - spec/aitch/namespace_spec.rb
177
194
  - spec/aitch/request_spec.rb
178
195
  - spec/aitch/response_spec.rb