simple-http 0.3.3 → 0.3.4

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: b513811215ed8543923236fd40336bd3989771cc85f7da2a2d6ecfff869c7bd4
4
- data.tar.gz: 0a0718c191e4102a8a06162e92d295dcd519aaeb22fbb4997ee45a8b3a4e7a26
3
+ metadata.gz: af4c48df122f1e2d75e2646e7175e8356b3d29ed29090b7b2d50e5b045ac4c67
4
+ data.tar.gz: 377058a3745909a67b4cf51307fcc87743bf5e054e681541e72b668473a07933
5
5
  SHA512:
6
- metadata.gz: 2b304ffa55e45fdcc8c5c68b52d379dae57d38d07404c817a04de80409c0e161c75c6c94dff0cdbad81506ffee3e61e0569b33d1e20fd8fba77880e420694650
7
- data.tar.gz: 429e91c1b979b1750cf4896b2ebf2a13cf072bb4e0418268573615273dd1a5785ef5eb6f9927851d55531eb41703d748d57fe9a8c59533c750e463147d486848
6
+ metadata.gz: 3bb65d9e2a2fe14e197fcab613fe587523fb932cd1f45b2648d2a160defe25762490223d66a338d342095abb83fafc3c564a6d3bd525c0dc5033728dad33dde8
7
+ data.tar.gz: 910e7c3e08cd98361067e79082496b972cb24e0b3bc64365807d058119696e23758a3ac2e166ebdbe1dd678c8a0c4b10dcfaeeaf1c5b105cb7613fe8dd6765da
@@ -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
  #
@@ -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
- end
39
+ end
40
40
 
41
41
  Kernel.raise error_klass.new(response)
42
42
  end
@@ -1,24 +1,22 @@
1
1
  module Simple::HTTP::Helpers
2
- module BuildURL
3
- def build_url(base, *args)
4
- option_args, string_args = args.partition { |arg| arg.is_a?(Hash) }
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
- url = File.join([base] + string_args)
6
+ url = File.join([base] + string_args)
8
7
 
9
- query = build_query(options)
10
- url += url.index("?") ? "&#{query}" : "?#{query}" if query
8
+ query = build_query(options)
9
+ url += url.index("?") ? "&#{query}" : "?#{query}" if query
11
10
 
12
- url
13
- end
11
+ url
12
+ end
14
13
 
15
- private
14
+ private
16
15
 
17
- def build_query(params)
18
- params = params.reject { |_k, v| v.blank? }
19
- return nil if params.blank?
16
+ def build_query(params)
17
+ params = params.reject { |_k, v| v.blank? }
18
+ return nil if params.blank?
20
19
 
21
- params.map { |k, value| "#{k}=#{escape(value.to_s)}" }.join("&")
22
- end
20
+ params.map { |k, value| "#{k}=#{CGI.escape(value.to_s)}" }.join("&")
23
21
  end
24
22
  end
@@ -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.new entry }
74
+ parsed_content.map { |entry| convert_into(entry, into: into) }
75
75
  else
76
- into.new parsed_content
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 = case media_type
86
- when "application/json"
87
- body.empty? ? nil : JSON.parse(body)
88
- else
89
- body
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.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 00:00:00.000000000 Z
11
+ date: 2019-12-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: expectation