simple-http 0.3.3 → 0.3.4
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 +4 -4
- data/lib/simple/http.rb +3 -0
- data/lib/simple/http/errors.rb +1 -1
- data/lib/simple/http/helpers.rb +13 -15
- data/lib/simple/http/response.rb +29 -7
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: af4c48df122f1e2d75e2646e7175e8356b3d29ed29090b7b2d50e5b045ac4c67
|
4
|
+
data.tar.gz: 377058a3745909a67b4cf51307fcc87743bf5e054e681541e72b668473a07933
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3bb65d9e2a2fe14e197fcab613fe587523fb932cd1f45b2648d2a160defe25762490223d66a338d342095abb83fafc3c564a6d3bd525c0dc5033728dad33dde8
|
7
|
+
data.tar.gz: 910e7c3e08cd98361067e79082496b972cb24e0b3bc64365807d058119696e23758a3ac2e166ebdbe1dd678c8a0c4b10dcfaeeaf1c5b105cb7613fe8dd6765da
|
data/lib/simple/http.rb
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
# License:: Distributes under the terms of the Modified BSD License, see LICENSE.BSD for details.
|
4
4
|
|
5
5
|
# rubocop:disable Metrics/ClassLength
|
6
|
+
# rubocop:disable Metrics/AbcSize
|
6
7
|
|
7
8
|
require "json"
|
8
9
|
require "expectation"
|
@@ -20,6 +21,8 @@ require_relative "http/request"
|
|
20
21
|
require_relative "http/response"
|
21
22
|
require_relative "http/checked_response"
|
22
23
|
|
24
|
+
Simple::HTTP.extend Simple::HTTP::Helpers
|
25
|
+
|
23
26
|
#
|
24
27
|
# A very simple, Net::HTTP-based HTTP client.
|
25
28
|
#
|
data/lib/simple/http/errors.rb
CHANGED
@@ -36,7 +36,7 @@ class Simple::HTTP::StatusError < Simple::HTTP::Error
|
|
36
36
|
when 400..499 then ::Simple::HTTP::Status4XXError
|
37
37
|
when 500..599 then ::Simple::HTTP::Status5XXError
|
38
38
|
else ::Simple::HTTP::StatusError
|
39
|
-
|
39
|
+
end
|
40
40
|
|
41
41
|
Kernel.raise error_klass.new(response)
|
42
42
|
end
|
data/lib/simple/http/helpers.rb
CHANGED
@@ -1,24 +1,22 @@
|
|
1
1
|
module Simple::HTTP::Helpers
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
options = option_args.inject({}) { |hsh, option| hsh.update option }
|
2
|
+
def build_url(base, *args)
|
3
|
+
option_args, string_args = args.partition { |arg| arg.is_a?(Hash) }
|
4
|
+
options = option_args.inject({}) { |hsh, option| hsh.update option }
|
6
5
|
|
7
|
-
|
6
|
+
url = File.join([base] + string_args)
|
8
7
|
|
9
|
-
|
10
|
-
|
8
|
+
query = build_query(options)
|
9
|
+
url += url.index("?") ? "&#{query}" : "?#{query}" if query
|
11
10
|
|
12
|
-
|
13
|
-
|
11
|
+
url
|
12
|
+
end
|
14
13
|
|
15
|
-
|
14
|
+
private
|
16
15
|
|
17
|
-
|
18
|
-
|
19
|
-
|
16
|
+
def build_query(params)
|
17
|
+
params = params.reject { |_k, v| v.blank? }
|
18
|
+
return nil if params.blank?
|
20
19
|
|
21
|
-
|
22
|
-
end
|
20
|
+
params.map { |k, value| "#{k}=#{CGI.escape(value.to_s)}" }.join("&")
|
23
21
|
end
|
24
22
|
end
|
data/lib/simple/http/response.rb
CHANGED
@@ -71,22 +71,44 @@ class Simple::HTTP::Response
|
|
71
71
|
return parsed_content if into.nil?
|
72
72
|
|
73
73
|
if parsed_content.is_a?(Array)
|
74
|
-
parsed_content.map { |entry| into
|
74
|
+
parsed_content.map { |entry| convert_into(entry, into: into) }
|
75
75
|
else
|
76
|
-
into
|
76
|
+
convert_into(parsed_content, into: into)
|
77
77
|
end
|
78
78
|
end
|
79
79
|
|
80
80
|
private
|
81
81
|
|
82
|
+
def convert_into(rec, into:)
|
83
|
+
case into
|
84
|
+
when :struct
|
85
|
+
to_struct(rec)
|
86
|
+
else
|
87
|
+
into.new rec
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def to_struct(hsh)
|
92
|
+
keys = hsh.keys
|
93
|
+
values = hsh.values_at(*keys)
|
94
|
+
|
95
|
+
@to_structs ||= {}
|
96
|
+
struct = (@to_structs[keys] ||= Struct.new(*keys.map(&:to_sym)))
|
97
|
+
struct.new(*values)
|
98
|
+
end
|
99
|
+
|
82
100
|
def parsed_content
|
83
101
|
return @parsed_content if defined? @parsed_content
|
84
102
|
|
85
|
-
@parsed_content =
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
103
|
+
@parsed_content = parse_content
|
104
|
+
end
|
105
|
+
|
106
|
+
def parse_content
|
107
|
+
case media_type
|
108
|
+
when "application/json"
|
109
|
+
body.empty? ? nil : JSON.parse(body)
|
110
|
+
else
|
111
|
+
body
|
90
112
|
end
|
91
113
|
end
|
92
114
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple-http
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- radiospiel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-12-
|
11
|
+
date: 2019-12-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: expectation
|