aitch 1.2.1 → 1.2.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
  SHA256:
3
- metadata.gz: ac6be3c16c842bbd1555aed0de859b6e62bf59f511cce28a6c441efd980e5751
4
- data.tar.gz: 2ebc4b41d63c9dd41756bb7fff03b52a6cbca2f56ed310183bc158a822bbdbd9
3
+ metadata.gz: c49091163504e6a6587170951f8e4545b4474b1912c85c5bcfeb102763a214b7
4
+ data.tar.gz: 7fac91b61ee9ad82e8f880f25758b9f6a433ad8ed3bcc5134b261e64f5da61fc
5
5
  SHA512:
6
- metadata.gz: 602da6e2d0aaf3812e7f19b84b48b47eaeac59086345bceb76c601eba64f369ae0833c32980f66b0c026338bb098ad72563d1a4d2fdf8384fe50792856d281a1
7
- data.tar.gz: b35ef25d1e59cd76923e626fdd6d60a735977a35d6560a32fe3313a8421eeccbce9524163775651da6fc017fcd47f7ef20c7ec8a40460d4a627e2d75d334fae8
6
+ metadata.gz: 859bdbfb5f70d99646c628f16a2cea718385299ca1b48d32e0ecfb8c9598ad27b8cf3363498b50eaddf8585901212faa1f588dc9dc29ad1982891f2a3c26b1fa
7
+ data.tar.gz: a7045d8d2a6c9d172b2b90180cd4d2afb13a5ceac088b74b9cb7ccc63fa9050920df69310e0eef1ae745b44d0edd4c45f6779c9a6fe420c3951878be4ff3fbf1
data/.rubocop.yml CHANGED
@@ -16,3 +16,9 @@ Naming/AccessorMethodName:
16
16
 
17
17
  Style/DocumentDynamicEvalDefinition:
18
18
  Enabled: false
19
+
20
+ Gemspec/DevelopmentDependencies:
21
+ Enabled: false
22
+
23
+ Minitest/MultipleAssertions:
24
+ Enabled: false
data/CHANGELOG.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Changelog
2
2
 
3
+ - **1.2.2**
4
+ - Transform header key before assigning values.
3
5
  - **1.2.1**
4
6
  - Avoid calling `to_h` when params are array-like.
5
7
  - **1.2.0**
@@ -2,6 +2,8 @@
2
2
 
3
3
  module Aitch
4
4
  class Location
5
+ MATCHER = %r{\A/}.freeze
6
+
5
7
  attr_reader :redirect_stack, :current_url
6
8
 
7
9
  def initialize(redirect_stack, current_url)
@@ -10,7 +12,7 @@ module Aitch
10
12
  end
11
13
 
12
14
  def location
13
- return current_url unless current_url.match?(%r{\A/}) # rubocop:disable Performance/StartWith
15
+ return current_url unless current_url.match?(MATCHER)
14
16
 
15
17
  uri = find_uri_with_host
16
18
  url = ["#{uri.scheme}://#{uri.hostname}"]
data/lib/aitch/request.rb CHANGED
@@ -4,6 +4,14 @@ module Aitch
4
4
  class Request
5
5
  attr_accessor :request_method, :url, :data, :headers, :options, :redirects
6
6
 
7
+ CONTENT_TYPE = "Content-Type"
8
+ USER_AGENT = "User-Agent"
9
+ ACCEPT_ENCODING = "Accept-Encoding"
10
+ GZIP_DEFLATE = "gzip,deflate"
11
+ HTTPS = "https"
12
+ HEADER_SEPARATOR_RE = /[-_]/.freeze
13
+ JSON_RE = /\bjson\b/.freeze
14
+
7
15
  alias params= data=
8
16
  alias body= data=
9
17
 
@@ -30,12 +38,12 @@ module Aitch
30
38
  end
31
39
 
32
40
  def content_type=(content_type)
33
- headers["Content-Type"] = content_type
41
+ headers[CONTENT_TYPE] = content_type
34
42
  end
35
43
 
36
44
  def content_type
37
- headers["Content-Type"] ||
38
- options.fetch(:default_headers, {})["Content-Type"]
45
+ headers[CONTENT_TYPE] ||
46
+ options.fetch(:default_headers, {})[CONTENT_TYPE]
39
47
  end
40
48
 
41
49
  def request
@@ -79,7 +87,7 @@ module Aitch
79
87
  body_data = data
80
88
  body_data = data.to_h if data.respond_to?(:to_h) && !data.is_a?(Array)
81
89
 
82
- if content_type.to_s.match?(/\bjson\b/)
90
+ if content_type.to_s.match?(JSON_RE)
83
91
  body_data = ResponseParser::JSONParser.engine.dump(body_data)
84
92
  end
85
93
 
@@ -95,7 +103,8 @@ module Aitch
95
103
 
96
104
  all_headers.each do |name, value|
97
105
  value = value.respond_to?(:call) ? value.call : value
98
- request[name.to_s] = value.to_s
106
+ name = name.to_s.split(HEADER_SEPARATOR_RE).map(&:capitalize).join("-")
107
+ request[name] = value.to_s
99
108
  end
100
109
  end
101
110
 
@@ -106,7 +115,7 @@ module Aitch
106
115
  end
107
116
 
108
117
  private def set_https(client)
109
- client.use_ssl = uri.scheme == "https"
118
+ client.use_ssl = uri.scheme == HTTPS
110
119
  client.verify_mode = OpenSSL::SSL::VERIFY_PEER
111
120
  end
112
121
 
@@ -120,11 +129,11 @@ module Aitch
120
129
  end
121
130
 
122
131
  private def set_user_agent(request)
123
- request["User-Agent"] = options[:user_agent]
132
+ request[USER_AGENT] = options[:user_agent]
124
133
  end
125
134
 
126
135
  private def set_gzip(request)
127
- request["Accept-Encoding"] = "gzip,deflate"
136
+ request[ACCEPT_ENCODING] = GZIP_DEFLATE
128
137
  end
129
138
 
130
139
  def timeout_exception
@@ -4,11 +4,20 @@ module Aitch
4
4
  class Response
5
5
  extend Forwardable
6
6
 
7
+ JSON_STR = "json"
8
+ XML_STR = "xml"
9
+ HTML_STR = "html"
10
+ EMPTY_STR = ""
11
+ DOUBLE_COLON = "::"
12
+ SPACE_STR = " "
13
+ ERROR_SUFFIX = "_error"
14
+ X_RE = /^x-/.freeze
15
+
7
16
  def_delegators :@http_response, :content_type
8
17
  attr_accessor :redirected_from, :url
9
18
 
10
19
  def self.description_for_code(code)
11
- [code, DESCRIPTION[code]].compact.join(" ")
20
+ [code, DESCRIPTION[code]].compact.join(SPACE_STR)
12
21
  end
13
22
 
14
23
  def initialize(options, http_response)
@@ -19,8 +28,8 @@ module Aitch
19
28
 
20
29
  ERRORS.each do |status_code, exception|
21
30
  method_name = Utils
22
- .underscore(exception.name.split("::").last)
23
- .gsub("_error", "")
31
+ .underscore(exception.name.split(DOUBLE_COLON).last)
32
+ .gsub(ERROR_SUFFIX, EMPTY_STR)
24
33
 
25
34
  define_method "#{method_name}?" do
26
35
  code == status_code
@@ -53,15 +62,15 @@ module Aitch
53
62
  end
54
63
 
55
64
  def json?
56
- content_type.include?("json")
65
+ content_type.include?(JSON_STR)
57
66
  end
58
67
 
59
68
  def xml?
60
- content_type.include?("xml")
69
+ content_type.include?(XML_STR)
61
70
  end
62
71
 
63
72
  def html?
64
- content_type.include?("html")
73
+ content_type.include?(HTML_STR)
65
74
  end
66
75
 
67
76
  def data
@@ -71,7 +80,7 @@ module Aitch
71
80
  def headers
72
81
  @headers ||= {}.tap do |headers|
73
82
  @http_response.each_header do |name, value|
74
- headers[name.gsub(/^x-/, "")] = value
83
+ headers[name.gsub(X_RE, EMPTY_STR)] = value
75
84
  end
76
85
  end
77
86
  end
data/lib/aitch/utils.rb CHANGED
@@ -3,8 +3,11 @@
3
3
  module Aitch
4
4
  module Utils
5
5
  extend self
6
+
7
+ MATCHER = /(?<=.)(URI|[A-Z])/.freeze
8
+
6
9
  def underscore(string)
7
- string = string.gsub(/(?<=.)(URI|[A-Z])/) do |char|
10
+ string = string.gsub(MATCHER) do |char|
8
11
  "_#{char}"
9
12
  end
10
13
 
data/lib/aitch/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Aitch
4
- VERSION = "1.2.1"
4
+ VERSION = "1.2.2"
5
5
  end
@@ -4,25 +4,25 @@ require "test_helper"
4
4
 
5
5
  class AitchTest < Minitest::Test
6
6
  test "defines public API" do
7
- assert Aitch.respond_to?(:get)
8
- assert Aitch.respond_to?(:get!)
9
- assert Aitch.respond_to?(:post)
10
- assert Aitch.respond_to?(:post!)
11
- assert Aitch.respond_to?(:put)
12
- assert Aitch.respond_to?(:put!)
13
- assert Aitch.respond_to?(:patch)
14
- assert Aitch.respond_to?(:patch!)
15
- assert Aitch.respond_to?(:delete)
16
- assert Aitch.respond_to?(:delete!)
17
- assert Aitch.respond_to?(:head)
18
- assert Aitch.respond_to?(:head!)
19
- assert Aitch.respond_to?(:options)
20
- assert Aitch.respond_to?(:options!)
21
- assert Aitch.respond_to?(:trace)
22
- assert Aitch.respond_to?(:trace!)
23
- assert Aitch.respond_to?(:execute)
24
- assert Aitch.respond_to?(:execute!)
25
- assert Aitch.respond_to?(:config)
26
- assert Aitch.respond_to?(:configuration)
7
+ assert_respond_to Aitch, :get
8
+ assert_respond_to Aitch, :get!
9
+ assert_respond_to Aitch, :post
10
+ assert_respond_to Aitch, :post!
11
+ assert_respond_to Aitch, :put
12
+ assert_respond_to Aitch, :put!
13
+ assert_respond_to Aitch, :patch
14
+ assert_respond_to Aitch, :patch!
15
+ assert_respond_to Aitch, :delete
16
+ assert_respond_to Aitch, :delete!
17
+ assert_respond_to Aitch, :head
18
+ assert_respond_to Aitch, :head!
19
+ assert_respond_to Aitch, :options
20
+ assert_respond_to Aitch, :options!
21
+ assert_respond_to Aitch, :trace
22
+ assert_respond_to Aitch, :trace!
23
+ assert_respond_to Aitch, :execute
24
+ assert_respond_to Aitch, :execute!
25
+ assert_respond_to Aitch, :config
26
+ assert_respond_to Aitch, :configuration
27
27
  end
28
28
  end
@@ -9,6 +9,7 @@ class ConfigurationTest < Minitest::Test
9
9
 
10
10
  test "sets default user agent" do
11
11
  user_agent = "Aitch/#{Aitch::VERSION} (http://rubygems.org/gems/aitch)"
12
+
12
13
  assert_equal user_agent, Aitch::Configuration.new.user_agent
13
14
  end
14
15
 
@@ -17,7 +18,7 @@ class ConfigurationTest < Minitest::Test
17
18
  end
18
19
 
19
20
  test "sets default headers" do
20
- assert_equal({}, Aitch::Configuration.new.default_headers)
21
+ assert_empty(Aitch::Configuration.new.default_headers)
21
22
  end
22
23
 
23
24
  test "configures aitch" do
@@ -7,31 +7,37 @@ class DslTest < Minitest::Test
7
7
 
8
8
  test "sets url" do
9
9
  dsl.url "URL"
10
+
10
11
  assert_equal "URL", dsl.url
11
12
  end
12
13
 
13
14
  test "sets options" do
14
15
  dsl.options "OPTIONS"
16
+
15
17
  assert_equal "OPTIONS", dsl.options
16
18
  end
17
19
 
18
20
  test "sets headers" do
19
21
  dsl.headers "HEADERS"
22
+
20
23
  assert_equal "HEADERS", dsl.headers
21
24
  end
22
25
 
23
26
  test "sets data" do
24
27
  dsl.data "DATA"
28
+
25
29
  assert_equal "DATA", dsl.data
26
30
  end
27
31
 
28
32
  test "sets data through params" do
29
33
  dsl.params "PARAMS"
34
+
30
35
  assert_equal "PARAMS", dsl.data
31
36
  end
32
37
 
33
38
  test "sets data through body" do
34
39
  dsl.body "BODY"
40
+
35
41
  assert_equal "BODY", dsl.data
36
42
  end
37
43
 
@@ -41,6 +41,6 @@ class ExecuteBangTest < Minitest::Test
41
41
  response = stub(error?: true, error: "ERROR")
42
42
  Aitch::Request.any_instance.stubs(:perform).returns(response)
43
43
 
44
- assert_raises("ERROR") { Aitch.get!("URL") }
44
+ assert_raises(StandardError, "ERROR") { Aitch.get!("URL") }
45
45
  end
46
46
  end
@@ -7,7 +7,7 @@ class ClientHttpsTest < Minitest::Test
7
7
  let(:client) { request.client }
8
8
 
9
9
  test "sets https" do
10
- assert client.use_ssl?
10
+ assert_predicate client, :use_ssl?
11
11
  end
12
12
 
13
13
  test "sets verification mode" do
@@ -16,6 +16,7 @@ class ClientHttpsTest < Minitest::Test
16
16
 
17
17
  test "sets timeout" do
18
18
  request.options[:timeout] = 20
19
+
19
20
  assert_equal 20, client.read_timeout
20
21
  end
21
22
  end
@@ -14,7 +14,7 @@ class FollowRedirectTest < Minitest::Test
14
14
 
15
15
  response = Aitch.get("http://example.org/")
16
16
 
17
- refute response.redirect?
17
+ refute_predicate response, :redirect?
18
18
  assert_equal "Hello", response.body
19
19
  assert_equal ["http://example.org/", "http://example.com/"], response.redirected_from
20
20
  assert_equal "http://www.example.com/", response.url
@@ -23,6 +23,7 @@ class RequestClassTest < Minitest::Test
23
23
  ].each do |method|
24
24
  test "instantiates #{method.upcase} method" do
25
25
  request = build_request(request_method: method).request
26
+
26
27
  assert_equal "Net::HTTP::#{method.capitalize}", request.class.name
27
28
  end
28
29
  end
@@ -5,6 +5,7 @@ require "test_helper"
5
5
  class RequestTest < Minitest::Test
6
6
  test "sets content type" do
7
7
  request = build_request(content_type: "application/json")
8
+
8
9
  assert_equal "application/json", request.content_type
9
10
  end
10
11
 
@@ -35,26 +36,31 @@ class RequestTest < Minitest::Test
35
36
 
36
37
  test "requests gzip encoding" do
37
38
  request = build_request.request
39
+
38
40
  assert_equal "gzip,deflate", request["Accept-Encoding"]
39
41
  end
40
42
 
41
43
  test "sets path" do
42
44
  request = build_request(url: "http://example.org/some/path").request
45
+
43
46
  assert_equal "/some/path", request.path
44
47
  end
45
48
 
46
49
  test "sets request body from hash" do
47
50
  request = build_request(request_method: "post", data: {a: 1}).request
51
+
48
52
  assert_equal "a=1", request.body
49
53
  end
50
54
 
51
55
  test "sets request body from string" do
52
56
  request = build_request(request_method: "post", data: "some body").request
57
+
53
58
  assert_equal "some body", request.body
54
59
  end
55
60
 
56
61
  test "sets request body from params key" do
57
62
  request = build_request(request_method: "post", params: "some body").request
63
+
58
64
  assert_equal "some body", request.body
59
65
  end
60
66
 
@@ -67,6 +73,7 @@ class RequestTest < Minitest::Test
67
73
  ).request
68
74
 
69
75
  expected = {a: 1}.to_json
76
+
70
77
  assert_equal expected, request.body
71
78
  end
72
79
 
@@ -78,12 +85,14 @@ class RequestTest < Minitest::Test
78
85
  ).request
79
86
 
80
87
  expected = {a: 1}.to_json
88
+
81
89
  assert_equal expected, request.body
82
90
  end
83
91
 
84
92
  test "sets request body from to_h protocol" do
85
93
  data = stub(to_h: {a: 1})
86
94
  request = build_request(request_method: "post", data: data).request
95
+
87
96
  assert_equal "a=1", request.body
88
97
  end
89
98
 
@@ -123,11 +132,19 @@ class RequestTest < Minitest::Test
123
132
 
124
133
  test "sets custom headers" do
125
134
  request = build_request(headers: {"HEADER" => "VALUE"}).request
135
+
126
136
  assert_equal "VALUE", request["HEADER"]
127
137
  end
128
138
 
139
+ test "sets headers from underscored headers" do
140
+ request = build_request(headers: {content_type: "text/plain"}).request
141
+
142
+ assert_equal "text/plain", request["Content-Type"]
143
+ end
144
+
129
145
  test "executes headers with callable protocol" do
130
146
  request = build_request(headers: {"HEADER" => -> { "VALUE" }}).request
147
+
131
148
  assert_equal "VALUE", request["HEADER"]
132
149
  end
133
150
 
@@ -7,7 +7,7 @@ class HtmlResponseTest < Minitest::Test
7
7
  register_uri(:get, "http://example.org/", body: "", content_type: "text/html")
8
8
  response = Aitch.get("http://example.org/")
9
9
 
10
- assert response.html?
10
+ assert_predicate response, :html?
11
11
  end
12
12
 
13
13
  test "returns html" do
@@ -7,7 +7,7 @@ class JsonResponseTest < Minitest::Test
7
7
  register_uri(:get, "http://example.org/", body: "[]", content_type: "application/json")
8
8
  response = Aitch.get("http://example.org/")
9
9
 
10
- assert response.json?
10
+ assert_predicate response, :json?
11
11
  end
12
12
 
13
13
  test "returns json" do
@@ -6,7 +6,7 @@ class Status3xxTest < Minitest::Test
6
6
  setup { Aitch.configuration.follow_redirect = false }
7
7
 
8
8
  test "sets default redirected from" do
9
- assert_equal [], Aitch::Response.new({}, stub("response")).redirected_from
9
+ assert_empty Aitch::Response.new({}, stub("response")).redirected_from
10
10
  end
11
11
 
12
12
  test "uses provided redirected from" do
@@ -16,6 +16,7 @@ class Status3xxTest < Minitest::Test
16
16
  test "has body" do
17
17
  register_uri(:get, "http://example.org/", body: "Hello", status: 301)
18
18
  response = Aitch.get("http://example.org/")
19
+
19
20
  assert_equal "Hello", response.body
20
21
  end
21
22
 
@@ -23,20 +24,21 @@ class Status3xxTest < Minitest::Test
23
24
  register_uri(:get, "http://example.org/", status: 301)
24
25
  response = Aitch.get("http://example.org/")
25
26
 
26
- assert response.success?
27
- assert response.ok?
27
+ assert_predicate response, :success?
28
+ assert_predicate response, :ok?
28
29
  end
29
30
 
30
31
  test "detects as redirect" do
31
32
  register_uri(:get, "http://example.org/", status: 301)
32
33
  response = Aitch.get("http://example.org/")
33
34
 
34
- assert response.redirect?
35
+ assert_predicate response, :redirect?
35
36
  end
36
37
 
37
38
  test "returns location" do
38
39
  register_uri(:get, "http://example.org/", status: 301, location: "https://example.com/")
39
40
  response = Aitch.get("http://example.org/")
41
+
40
42
  assert_equal "https://example.com/", response.location
41
43
  end
42
44
 
@@ -7,7 +7,7 @@ class Status4xxTest < Minitest::Test
7
7
  register_uri(:get, "http://example.org/", status: 404)
8
8
  response = Aitch.get("http://example.org/")
9
9
 
10
- assert response.error?
10
+ assert_predicate response, :error?
11
11
  end
12
12
 
13
13
  test "sets error" do
@@ -7,7 +7,7 @@ class Status5xxTest < Minitest::Test
7
7
  register_uri(:get, "http://example.org/", status: 500)
8
8
  response = Aitch.get("http://example.org/")
9
9
 
10
- assert response.error?
10
+ assert_predicate response, :error?
11
11
  end
12
12
 
13
13
  test "sets error" do
@@ -7,7 +7,7 @@ class XmlResponseTest < Minitest::Test
7
7
  register_uri(:get, "http://example.org/", body: "[]", content_type: "application/xml")
8
8
  response = Aitch.get("http://example.org/")
9
9
 
10
- assert response.xml?
10
+ assert_predicate response, :xml?
11
11
  end
12
12
 
13
13
  test "returns xml" do
@@ -6,12 +6,14 @@ class ResponseTest < Minitest::Test
6
6
  test "has body" do
7
7
  register_uri(:get, "http://example.org/", body: "Hello")
8
8
  response = Aitch.get("http://example.org/")
9
+
9
10
  assert_equal "Hello", response.body
10
11
  end
11
12
 
12
13
  test "sets current url" do
13
14
  register_uri(:get, "http://example.org/", body: "Hello")
14
15
  response = Aitch.get("http://example.org/")
16
+
15
17
  assert_equal "http://example.org/", response.url
16
18
  end
17
19
 
@@ -39,19 +41,22 @@ class ResponseTest < Minitest::Test
39
41
  test "returns status code" do
40
42
  register_uri(:get, "http://example.org/", body: "")
41
43
  response = Aitch.get("http://example.org/")
44
+
42
45
  assert_equal 200, response.code
43
46
  end
44
47
 
45
48
  test "returns content type" do
46
49
  register_uri(:get, "http://example.org/", content_type: "text/html")
47
50
  response = Aitch.get("http://example.org/")
51
+
48
52
  assert_equal "text/html", response.content_type
49
53
  end
50
54
 
51
55
  test "detects as successful response" do
52
56
  register_uri(:get, "http://example.org/", content_type: "text/html")
53
57
  response = Aitch.get("http://example.org/")
54
- assert response.success?
58
+
59
+ assert_predicate response, :success?
55
60
  end
56
61
 
57
62
  test "returns headers" do
@@ -74,7 +79,7 @@ class ResponseTest < Minitest::Test
74
79
  response = Aitch.get("http://example.org/")
75
80
 
76
81
  assert_equal "0.003", response.runtime
77
- assert response.respond_to?(:runtime)
82
+ assert_respond_to response, :runtime
78
83
  end
79
84
 
80
85
  test "raises when have no method" do
@@ -33,6 +33,7 @@ class UriTest < Minitest::Test
33
33
 
34
34
  test "returns request uri" do
35
35
  uri = Aitch::URI.new("http://example.org/some/path?a=1&b=2#hello", c: 3)
36
+
36
37
  assert_equal "/some/path?a=1&b=2&c=3#hello", uri.request_uri
37
38
  end
38
39
  end
@@ -5,6 +5,7 @@ require "test_helper"
5
5
  class SymbolizeKeysTest < Minitest::Test
6
6
  test "converts keys to symbols" do
7
7
  expected = {a: 1}
8
+
8
9
  assert_equal expected, Aitch::Utils.symbolize_keys("a" => 1)
9
10
  end
10
11
  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: 1.2.1
4
+ version: 1.2.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: 2022-11-13 00:00:00.000000000 Z
11
+ date: 2023-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -255,7 +255,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
255
255
  - !ruby/object:Gem::Version
256
256
  version: '0'
257
257
  requirements: []
258
- rubygems_version: 3.3.7
258
+ rubygems_version: 3.4.3
259
259
  signing_key:
260
260
  specification_version: 4
261
261
  summary: A simple HTTP client