aitch 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c38a1b4c89a137aad0faec66bd2c53f00d59871c
4
- data.tar.gz: 6d6054265f125130692693f502f43e856830a7e8
3
+ metadata.gz: 53e317f071ded556b9eff2bfbf8b222cbfc6735a
4
+ data.tar.gz: e24ee0d7e6f28c49b49b22de458f663a7776ff3e
5
5
  SHA512:
6
- metadata.gz: 233cb27de492f6e8f03cceaf0f8d5ddf65aaffdaee7aaf963db1865aecbed347024360727ec5e72e3064bd046b53bc196e7f50cdce435a5cc6ae8bd38d3ca62b
7
- data.tar.gz: 9c02590e9d20323273e4584ae86f0ac0694ef316a2e35be6e2c7516402afb78991110f9aa228cbd2a35207c3c8dd510df57da2e4d6720906b55286401a7a2e26
6
+ metadata.gz: 2e19f66a240033ab32c969190dfa4994cfd6da5a6132bbd3fcc9331b4ff12f8d78193474d8be432bb206d9b59c3270657afcdc2d99ea98b93668ea3a6a2aa983
7
+ data.tar.gz: 626726af8014cbb4391c7be9da92c469cfc5519cd9f7e842ba874320e20375cfe7fcc4bd35a9e995f028856386a6c52d70218fb8d89a054aa1176fa507bb6643
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/README.md CHANGED
@@ -3,6 +3,7 @@
3
3
  [![Build Status](https://travis-ci.org/fnando/aitch.png)](https://travis-ci.org/fnando/aitch)
4
4
  [![CodeClimate](https://codeclimate.com/github/fnando/aitch.png)](https://codeclimate.com/github/fnando/aitch/)
5
5
  [![RubyGems](https://badge.fury.io/rb/aitch.png)](https://rubygems.org/gems/aitch)
6
+ [![Coveralls](https://coveralls.io/repos/fnando/aitch/badge.png?branch=master)](https://coveralls.io/r/fnando/aitch)
6
7
 
7
8
  A simple HTTP client.
8
9
 
@@ -3,13 +3,15 @@ require "forwardable"
3
3
  require "json"
4
4
  require "zlib"
5
5
 
6
+ require "aitch/utils"
7
+ require "aitch/uri"
6
8
  require "aitch/namespace"
7
9
  require "aitch/configuration"
8
10
  require "aitch/errors"
9
11
  require "aitch/request"
10
12
  require "aitch/redirect"
11
- require "aitch/response"
12
13
  require "aitch/response/errors"
14
+ require "aitch/response"
13
15
  require "aitch/response/body"
14
16
  require "aitch/xml_parser"
15
17
  require "aitch/version"
@@ -33,8 +33,8 @@ module Aitch
33
33
  end
34
34
 
35
35
  def request
36
- @request ||= http_method_class.new(File.join("/", uri.path)).tap do |request|
37
- set_body(request)
36
+ @request ||= http_method_class.new(uri.request_uri).tap do |request|
37
+ set_body(request) if request.request_body_permitted?
38
38
  set_user_agent(request)
39
39
  set_gzip(request)
40
40
  set_headers(request)
@@ -51,9 +51,7 @@ module Aitch
51
51
  end
52
52
 
53
53
  def uri
54
- @uri ||= URI.parse(url)
55
- rescue URI::InvalidURIError
56
- raise InvalidURIError
54
+ @uri ||= URI.new(url, data, http_method_class::REQUEST_HAS_BODY)
57
55
  end
58
56
 
59
57
  def http_method_class
@@ -9,6 +9,14 @@ module Aitch
9
9
  @http_response = http_response
10
10
  end
11
11
 
12
+ ERRORS.each do |status_code, exception|
13
+ method_name = Utils.underscore(exception.name.split("::").last).gsub("_error", "")
14
+
15
+ define_method "#{method_name}?" do
16
+ code == status_code
17
+ end
18
+ end
19
+
12
20
  def code
13
21
  @http_response.code.to_i
14
22
  end
@@ -0,0 +1,43 @@
1
+ module Aitch
2
+ class URI
3
+ extend Forwardable
4
+
5
+ def_delegators :@uri, :host, :port, :scheme
6
+
7
+ def initialize(url, data = {}, request_has_body = false)
8
+ @url = url
9
+ @data = data
10
+ @request_has_body = request_has_body
11
+
12
+ begin
13
+ @uri = ::URI.parse(url)
14
+ rescue ::URI::InvalidURIError => error
15
+ raise InvalidURIError, error
16
+ end
17
+ end
18
+
19
+ def request_has_body?
20
+ @request_has_body
21
+ end
22
+
23
+ def path
24
+ File.join("/", @uri.path)
25
+ end
26
+
27
+ def request_uri
28
+ [path, query, fragment].compact.join("")
29
+ end
30
+
31
+ def fragment
32
+ "##{@uri.fragment}" if @uri.fragment
33
+ end
34
+
35
+ def query
36
+ query = [@uri.query]
37
+ query << ::URI.encode_www_form(@data.to_a) if !request_has_body? && @data.respond_to?(:to_a)
38
+ query = query.compact.reject(&:empty?).join("&")
39
+
40
+ "?#{query}" unless query == ""
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,11 @@
1
+ module Aitch
2
+ module Utils extend self
3
+ def underscore(string)
4
+ string = string.gsub(/(?<=.)(URI|[A-Z])/) do |char|
5
+ "_#{char}"
6
+ end
7
+
8
+ string.downcase
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module Aitch
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -4,7 +4,7 @@ describe Aitch::Request do
4
4
  def build_request(options = {})
5
5
  Aitch::Request.new({
6
6
  request_method: "get",
7
- url: "URL",
7
+ url: "http://example.org",
8
8
  config: Aitch::Configuration.new
9
9
  }.merge(options))
10
10
  end
@@ -50,28 +50,35 @@ describe Aitch::Request do
50
50
  end
51
51
 
52
52
  it "sets request body from hash" do
53
- request = build_request(data: {a: 1}).request
53
+ request = build_request(request_method: "post", data: {a: 1}).request
54
54
  expect(request.body).to eql("a=1")
55
55
  end
56
56
 
57
57
  it "sets request body from string" do
58
- request = build_request(data: "some body").request
58
+ request = build_request(request_method: "post", data: "some body").request
59
59
  expect(request.body).to eql("some body")
60
60
  end
61
61
 
62
62
  it "sets request body from to_h protocol" do
63
63
  data = stub(to_h: {a: 1})
64
- request = build_request(data: data).request
64
+ request = build_request(request_method: "post", data: data).request
65
65
  expect(request.body).to eql("a=1")
66
66
  end
67
67
 
68
68
  it "sets request body from to_s protocol" do
69
69
  data = stub(to_s: "some body")
70
- request = build_request(data: data).request
70
+ request = build_request(request_method: "post", data: data).request
71
71
 
72
72
  expect(request.body).to eql("some body")
73
73
  end
74
74
 
75
+ it "sets query string from hash data" do
76
+ FakeWeb.register_uri :get, "http://example.org/?a=1&b=2", body: "hello"
77
+ requester = build_request(data: {a: 1, b: 2})
78
+
79
+ expect(requester.perform.body).to eql("hello")
80
+ end
81
+
75
82
  it "sets default headers" do
76
83
  requester = build_request
77
84
  requester.config.default_headers = {"HEADER" => "VALUE"}
@@ -162,4 +169,13 @@ describe Aitch::Request do
162
169
  }.to raise_error(Aitch::TooManyRedirectsError)
163
170
  end
164
171
  end
172
+
173
+ describe "GET requests" do
174
+ it "sets data as query string" do
175
+ FakeWeb.register_uri(:get, %r[.+], body: "")
176
+ Aitch.get("http://example.org/", a: 1, b: 2)
177
+
178
+ expect(FakeWeb.last_request.path).to eql("/?a=1&b=2")
179
+ end
180
+ end
165
181
  end
@@ -177,4 +177,15 @@ describe Aitch::Response do
177
177
  expect(response.data).to be_a(Nokogiri::XML::Document)
178
178
  end
179
179
  end
180
+
181
+ Aitch::Response::ERRORS.each do |code, exception|
182
+ name = Aitch::Utils.underscore(exception.name.split("::").last).gsub("_error", "")
183
+
184
+ it "detects response as #{name}" do
185
+ config = mock
186
+ http_response = stub(code: code)
187
+ response = Aitch::Response.new(config, http_response)
188
+ expect(response.public_send("#{name}?")).to be_true
189
+ end
190
+ end
180
191
  end
@@ -0,0 +1,36 @@
1
+ require "spec_helper"
2
+
3
+ describe Aitch::URI do
4
+ it "returns default path" do
5
+ expect(Aitch::URI.new("http://example.org").path).to eql("/")
6
+ end
7
+
8
+ it "returns defined path" do
9
+ expect(Aitch::URI.new("http://example.org/some/path").path).to eql("/some/path")
10
+ end
11
+
12
+ it "returns fragment" do
13
+ expect(Aitch::URI.new("http://example.org/#top").fragment).to eql("#top")
14
+ end
15
+
16
+ it "returns query string" do
17
+ expect(Aitch::URI.new("http://example.org/?a=1&b=2").query).to eql("?a=1&b=2")
18
+ end
19
+
20
+ it "converts data into query string" do
21
+ expect(Aitch::URI.new("http://example.org", a: 1, b: 2).query).to eql("?a=1&b=2")
22
+ end
23
+
24
+ it "merges data into query string" do
25
+ expect(Aitch::URI.new("http://example.org/?a=1&b=2", c: 3).query).to eql("?a=1&b=2&c=3")
26
+ end
27
+
28
+ it "ignores data when request has body" do
29
+ expect(Aitch::URI.new("http://example.org/", {c: 3}, true).query).to eql(nil)
30
+ end
31
+
32
+ it "returns request uri" do
33
+ uri = Aitch::URI.new("http://example.org/some/path?a=1&b=2#hello", c: 3)
34
+ expect(uri.request_uri).to eql("/some/path?a=1&b=2&c=3#hello")
35
+ end
36
+ end
@@ -0,0 +1,13 @@
1
+ require "spec_helper"
2
+
3
+ describe Aitch::Utils do
4
+ describe ".underscore" do
5
+ it "replaces capital letters by underscores" do
6
+ expect(Aitch::Utils.underscore("SomeConstantName")).to eql("some_constant_name")
7
+ end
8
+
9
+ it "considers URI acronym" do
10
+ expect(Aitch::Utils.underscore("RequestURITooLong")).to eql("request_uri_too_long")
11
+ end
12
+ end
13
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aitch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
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-04-30 00:00:00.000000000 Z
11
+ date: 2013-05-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -115,6 +115,7 @@ executables: []
115
115
  extensions: []
116
116
  extra_rdoc_files: []
117
117
  files:
118
+ - .coveralls.yml
118
119
  - .gitignore
119
120
  - .rspec
120
121
  - .travis.yml
@@ -132,6 +133,8 @@ files:
132
133
  - lib/aitch/response.rb
133
134
  - lib/aitch/response/body.rb
134
135
  - lib/aitch/response/errors.rb
136
+ - lib/aitch/uri.rb
137
+ - lib/aitch/utils.rb
135
138
  - lib/aitch/version.rb
136
139
  - lib/aitch/xml_parser.rb
137
140
  - spec/aitch/aitch_spec.rb
@@ -139,6 +142,8 @@ files:
139
142
  - spec/aitch/namespace_spec.rb
140
143
  - spec/aitch/request_spec.rb
141
144
  - spec/aitch/response_spec.rb
145
+ - spec/aitch/uri_spec.rb
146
+ - spec/aitch/utils_spec.rb
142
147
  - spec/aitch/xml_parser_spec.rb
143
148
  - spec/spec_helper.rb
144
149
  homepage: http://rubygems.org/gems/aitch
@@ -161,7 +166,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
161
166
  version: '0'
162
167
  requirements: []
163
168
  rubyforge_project:
164
- rubygems_version: 2.0.0
169
+ rubygems_version: 2.0.3
165
170
  signing_key:
166
171
  specification_version: 4
167
172
  summary: A simple HTTP client
@@ -171,5 +176,7 @@ test_files:
171
176
  - spec/aitch/namespace_spec.rb
172
177
  - spec/aitch/request_spec.rb
173
178
  - spec/aitch/response_spec.rb
179
+ - spec/aitch/uri_spec.rb
180
+ - spec/aitch/utils_spec.rb
174
181
  - spec/aitch/xml_parser_spec.rb
175
182
  - spec/spec_helper.rb