piko-clean-pkg 0.0.1

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.
Files changed (64) hide show
  1. checksums.yaml +7 -0
  2. data/httparty-0.24.2/CONTRIBUTING.md +23 -0
  3. data/httparty-0.24.2/Changelog.md +624 -0
  4. data/httparty-0.24.2/Gemfile +27 -0
  5. data/httparty-0.24.2/Guardfile +17 -0
  6. data/httparty-0.24.2/MIT-LICENSE +20 -0
  7. data/httparty-0.24.2/README.md +79 -0
  8. data/httparty-0.24.2/Rakefile +10 -0
  9. data/httparty-0.24.2/bin/httparty +123 -0
  10. data/httparty-0.24.2/cucumber.yml +1 -0
  11. data/httparty-0.24.2/docs/README.md +223 -0
  12. data/httparty-0.24.2/examples/README.md +90 -0
  13. data/httparty-0.24.2/examples/aaws.rb +36 -0
  14. data/httparty-0.24.2/examples/basic.rb +28 -0
  15. data/httparty-0.24.2/examples/body_stream.rb +14 -0
  16. data/httparty-0.24.2/examples/crack.rb +19 -0
  17. data/httparty-0.24.2/examples/custom_parsers.rb +68 -0
  18. data/httparty-0.24.2/examples/delicious.rb +37 -0
  19. data/httparty-0.24.2/examples/google.rb +16 -0
  20. data/httparty-0.24.2/examples/headers_and_user_agents.rb +10 -0
  21. data/httparty-0.24.2/examples/idn.rb +10 -0
  22. data/httparty-0.24.2/examples/logging.rb +36 -0
  23. data/httparty-0.24.2/examples/microsoft_graph.rb +52 -0
  24. data/httparty-0.24.2/examples/multipart.rb +35 -0
  25. data/httparty-0.24.2/examples/nokogiri_html_parser.rb +19 -0
  26. data/httparty-0.24.2/examples/party_foul_mode.rb +90 -0
  27. data/httparty-0.24.2/examples/peer_cert.rb +9 -0
  28. data/httparty-0.24.2/examples/rescue_json.rb +17 -0
  29. data/httparty-0.24.2/examples/rubyurl.rb +14 -0
  30. data/httparty-0.24.2/examples/stackexchange.rb +24 -0
  31. data/httparty-0.24.2/examples/stream_download.rb +26 -0
  32. data/httparty-0.24.2/examples/tripit_sign_in.rb +44 -0
  33. data/httparty-0.24.2/examples/twitter.rb +31 -0
  34. data/httparty-0.24.2/examples/whoismyrep.rb +10 -0
  35. data/httparty-0.24.2/httparty.gemspec +32 -0
  36. data/httparty-0.24.2/lib/httparty/connection_adapter.rb +237 -0
  37. data/httparty-0.24.2/lib/httparty/cookie_hash.rb +23 -0
  38. data/httparty-0.24.2/lib/httparty/decompressor.rb +102 -0
  39. data/httparty-0.24.2/lib/httparty/exceptions.rb +66 -0
  40. data/httparty-0.24.2/lib/httparty/hash_conversions.rb +71 -0
  41. data/httparty-0.24.2/lib/httparty/headers_processor.rb +32 -0
  42. data/httparty-0.24.2/lib/httparty/logger/apache_formatter.rb +47 -0
  43. data/httparty-0.24.2/lib/httparty/logger/curl_formatter.rb +93 -0
  44. data/httparty-0.24.2/lib/httparty/logger/logger.rb +30 -0
  45. data/httparty-0.24.2/lib/httparty/logger/logstash_formatter.rb +62 -0
  46. data/httparty-0.24.2/lib/httparty/module_inheritable_attributes.rb +56 -0
  47. data/httparty-0.24.2/lib/httparty/net_digest_auth.rb +135 -0
  48. data/httparty-0.24.2/lib/httparty/parser.rb +157 -0
  49. data/httparty-0.24.2/lib/httparty/request/body.rb +125 -0
  50. data/httparty-0.24.2/lib/httparty/request/multipart_boundary.rb +13 -0
  51. data/httparty-0.24.2/lib/httparty/request/streaming_multipart_body.rb +190 -0
  52. data/httparty-0.24.2/lib/httparty/request.rb +466 -0
  53. data/httparty-0.24.2/lib/httparty/response/headers.rb +35 -0
  54. data/httparty-0.24.2/lib/httparty/response.rb +156 -0
  55. data/httparty-0.24.2/lib/httparty/response_fragment.rb +21 -0
  56. data/httparty-0.24.2/lib/httparty/text_encoder.rb +72 -0
  57. data/httparty-0.24.2/lib/httparty/utils.rb +13 -0
  58. data/httparty-0.24.2/lib/httparty/version.rb +5 -0
  59. data/httparty-0.24.2/lib/httparty.rb +699 -0
  60. data/httparty-0.24.2/script/release +42 -0
  61. data/httparty-0.24.2/website/css/common.css +47 -0
  62. data/httparty-0.24.2/website/index.html +73 -0
  63. data/piko-clean-pkg.gemspec +12 -0
  64. metadata +103 -0
@@ -0,0 +1,237 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HTTParty
4
+ # Default connection adapter that returns a new Net::HTTP each time
5
+ #
6
+ # == Custom Connection Factories
7
+ #
8
+ # If you like to implement your own connection adapter, subclassing
9
+ # HTTParty::ConnectionAdapter will make it easier. Just override
10
+ # the #connection method. The uri and options attributes will have
11
+ # all the info you need to construct your http connection. Whatever
12
+ # you return from your connection method needs to adhere to the
13
+ # Net::HTTP interface as this is what HTTParty expects.
14
+ #
15
+ # @example log the uri and options
16
+ # class LoggingConnectionAdapter < HTTParty::ConnectionAdapter
17
+ # def connection
18
+ # puts uri
19
+ # puts options
20
+ # Net::HTTP.new(uri)
21
+ # end
22
+ # end
23
+ #
24
+ # @example count number of http calls
25
+ # class CountingConnectionAdapter < HTTParty::ConnectionAdapter
26
+ # @@count = 0
27
+ #
28
+ # self.count
29
+ # @@count
30
+ # end
31
+ #
32
+ # def connection
33
+ # self.count += 1
34
+ # super
35
+ # end
36
+ # end
37
+ #
38
+ # === Configuration
39
+ # There is lots of configuration data available for your connection adapter
40
+ # in the #options attribute. It is up to you to interpret them within your
41
+ # connection adapter. Take a look at the implementation of
42
+ # HTTParty::ConnectionAdapter#connection for examples of how they are used.
43
+ # The keys used in options are
44
+ # * :+timeout+: timeout in seconds
45
+ # * :+open_timeout+: http connection open_timeout in seconds, overrides timeout if set
46
+ # * :+read_timeout+: http connection read_timeout in seconds, overrides timeout if set
47
+ # * :+write_timeout+: http connection write_timeout in seconds, overrides timeout if set (Ruby >= 2.6.0 required)
48
+ # * :+debug_output+: see HTTParty::ClassMethods.debug_output.
49
+ # * :+cert_store+: contains certificate data. see method 'attach_ssl_certificates'
50
+ # * :+pem+: contains pem client certificate data. see method 'attach_ssl_certificates'
51
+ # * :+p12+: contains PKCS12 client client certificate data. see method 'attach_ssl_certificates'
52
+ # * :+verify+: verify the server’s certificate against the ca certificate.
53
+ # * :+verify_peer+: set to false to turn off server verification but still send client certificate
54
+ # * :+ssl_ca_file+: see HTTParty::ClassMethods.ssl_ca_file.
55
+ # * :+ssl_ca_path+: see HTTParty::ClassMethods.ssl_ca_path.
56
+ # * :+ssl_version+: SSL versions to allow. see method 'attach_ssl_certificates'
57
+ # * :+ciphers+: The list of SSL ciphers to support
58
+ # * :+connection_adapter_options+: contains the hash you passed to HTTParty.connection_adapter when you configured your connection adapter
59
+ # * :+local_host+: The local address to bind to
60
+ # * :+local_port+: The local port to bind to
61
+ # * :+http_proxyaddr+: HTTP Proxy address
62
+ # * :+http_proxyport+: HTTP Proxy port
63
+ # * :+http_proxyuser+: HTTP Proxy user
64
+ # * :+http_proxypass+: HTTP Proxy password
65
+ #
66
+ # === Inherited methods
67
+ # * :+clean_host+: Method used to sanitize host names
68
+
69
+ class ConnectionAdapter
70
+ # Private: Regex used to strip brackets from IPv6 URIs.
71
+ StripIpv6BracketsRegex = /\A\[(.*)\]\z/
72
+
73
+ OPTION_DEFAULTS = {
74
+ verify: true,
75
+ verify_peer: true
76
+ }
77
+
78
+ # Public
79
+ def self.call(uri, options)
80
+ new(uri, options).connection
81
+ end
82
+
83
+ def self.default_cert_store
84
+ @default_cert_store ||= OpenSSL::X509::Store.new.tap do |cert_store|
85
+ cert_store.set_default_paths
86
+ end
87
+ end
88
+
89
+ attr_reader :uri, :options
90
+
91
+ def initialize(uri, options = {})
92
+ uri_adapter = options[:uri_adapter] || URI
93
+ raise ArgumentError, "uri must be a #{uri_adapter}, not a #{uri.class}" unless uri.is_a? uri_adapter
94
+
95
+ @uri = uri
96
+ @options = OPTION_DEFAULTS.merge(options)
97
+ end
98
+
99
+ def connection
100
+ host = clean_host(uri.host)
101
+ port = uri.port || (uri.scheme == 'https' ? 443 : 80)
102
+ if options.key?(:http_proxyaddr)
103
+ http = Net::HTTP.new(
104
+ host,
105
+ port,
106
+ options[:http_proxyaddr],
107
+ options[:http_proxyport],
108
+ options[:http_proxyuser],
109
+ options[:http_proxypass]
110
+ )
111
+ else
112
+ http = Net::HTTP.new(host, port)
113
+ end
114
+
115
+ http.use_ssl = ssl_implied?(uri)
116
+
117
+ attach_ssl_certificates(http, options)
118
+
119
+ if add_timeout?(options[:timeout])
120
+ http.open_timeout = options[:timeout]
121
+ http.read_timeout = options[:timeout]
122
+ http.write_timeout = options[:timeout]
123
+ end
124
+
125
+ if add_timeout?(options[:read_timeout])
126
+ http.read_timeout = options[:read_timeout]
127
+ end
128
+
129
+ if add_timeout?(options[:open_timeout])
130
+ http.open_timeout = options[:open_timeout]
131
+ end
132
+
133
+ if add_timeout?(options[:write_timeout])
134
+ http.write_timeout = options[:write_timeout]
135
+ end
136
+
137
+ if add_max_retries?(options[:max_retries])
138
+ http.max_retries = options[:max_retries]
139
+ end
140
+
141
+ if options[:debug_output]
142
+ http.set_debug_output(options[:debug_output])
143
+ end
144
+
145
+ if options[:ciphers]
146
+ http.ciphers = options[:ciphers]
147
+ end
148
+
149
+ # Bind to a specific local address or port
150
+ #
151
+ # @see https://bugs.ruby-lang.org/issues/6617
152
+ if options[:local_host]
153
+ http.local_host = options[:local_host]
154
+ end
155
+
156
+ if options[:local_port]
157
+ http.local_port = options[:local_port]
158
+ end
159
+
160
+ http
161
+ end
162
+
163
+ private
164
+
165
+ def add_timeout?(timeout)
166
+ timeout && (timeout.is_a?(Integer) || timeout.is_a?(Float))
167
+ end
168
+
169
+ def add_max_retries?(max_retries)
170
+ max_retries && max_retries.is_a?(Integer) && max_retries >= 0
171
+ end
172
+
173
+ def clean_host(host)
174
+ strip_ipv6_brackets(host)
175
+ end
176
+
177
+ def strip_ipv6_brackets(host)
178
+ StripIpv6BracketsRegex =~ host ? $1 : host
179
+ end
180
+
181
+ def ssl_implied?(uri)
182
+ uri.port == 443 || uri.scheme == 'https'
183
+ end
184
+
185
+ def verify_ssl_certificate?
186
+ !(options[:verify] == false || options[:verify_peer] == false)
187
+ end
188
+
189
+ def attach_ssl_certificates(http, options)
190
+ if http.use_ssl?
191
+ if options.fetch(:verify, true)
192
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
193
+ if options[:cert_store]
194
+ http.cert_store = options[:cert_store]
195
+ else
196
+ # Use the default cert store by default, i.e. system ca certs
197
+ http.cert_store = self.class.default_cert_store
198
+ end
199
+ else
200
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
201
+ end
202
+
203
+ # Client certificate authentication
204
+ # Note: options[:pem] must contain the content of a PEM file having the private key appended
205
+ if options[:pem]
206
+ http.cert = OpenSSL::X509::Certificate.new(options[:pem])
207
+ http.key = OpenSSL::PKey.read(options[:pem], options[:pem_password])
208
+ http.verify_mode = verify_ssl_certificate? ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
209
+ end
210
+
211
+ # PKCS12 client certificate authentication
212
+ if options[:p12]
213
+ p12 = OpenSSL::PKCS12.new(options[:p12], options[:p12_password])
214
+ http.cert = p12.certificate
215
+ http.key = p12.key
216
+ http.verify_mode = verify_ssl_certificate? ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
217
+ end
218
+
219
+ # SSL certificate authority file and/or directory
220
+ if options[:ssl_ca_file]
221
+ http.ca_file = options[:ssl_ca_file]
222
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
223
+ end
224
+
225
+ if options[:ssl_ca_path]
226
+ http.ca_path = options[:ssl_ca_path]
227
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
228
+ end
229
+
230
+ # This is only Ruby 1.9+
231
+ if options[:ssl_version] && http.respond_to?(:ssl_version=)
232
+ http.ssl_version = options[:ssl_version]
233
+ end
234
+ end
235
+ end
236
+ end
237
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ class HTTParty::CookieHash < Hash #:nodoc:
4
+ CLIENT_COOKIES = %w(path expires domain path secure httponly samesite)
5
+
6
+ def add_cookies(data)
7
+ case data
8
+ when Hash
9
+ merge!(data)
10
+ when String
11
+ data.split('; ').each do |cookie|
12
+ key, value = cookie.split('=', 2)
13
+ self[key.to_sym] = value if key
14
+ end
15
+ else
16
+ raise "add_cookies only takes a Hash or a String"
17
+ end
18
+ end
19
+
20
+ def to_cookie_string
21
+ select { |k, v| !CLIENT_COOKIES.include?(k.to_s.downcase) }.collect { |k, v| "#{k}=#{v}" }.join('; ')
22
+ end
23
+ end
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HTTParty
4
+ # Decompresses the response body based on the Content-Encoding header.
5
+ #
6
+ # Net::HTTP automatically decompresses Content-Encoding values "gzip" and "deflate".
7
+ # This class will handle "br" (Brotli) and "compress" (LZW) if the requisite
8
+ # gems are installed. Otherwise, it returns nil if the body data cannot be
9
+ # decompressed.
10
+ #
11
+ # @abstract Read the HTTP Compression section for more information.
12
+ class Decompressor
13
+
14
+ # "gzip" and "deflate" are handled by Net::HTTP
15
+ # hence they do not need to be handled by HTTParty
16
+ SupportedEncodings = {
17
+ 'none' => :none,
18
+ 'identity' => :none,
19
+ 'br' => :brotli,
20
+ 'compress' => :lzw,
21
+ 'zstd' => :zstd
22
+ }.freeze
23
+
24
+ # The response body of the request
25
+ # @return [String]
26
+ attr_reader :body
27
+
28
+ # The Content-Encoding algorithm used to encode the body
29
+ # @return [Symbol] e.g. :gzip
30
+ attr_reader :encoding
31
+
32
+ # @param [String] body - the response body of the request
33
+ # @param [Symbol] encoding - the Content-Encoding algorithm used to encode the body
34
+ def initialize(body, encoding)
35
+ @body = body
36
+ @encoding = encoding
37
+ end
38
+
39
+ # Perform decompression on the response body
40
+ # @return [String] the decompressed body
41
+ # @return [nil] when the response body is nil or cannot decompressed
42
+ def decompress
43
+ return nil if body.nil?
44
+ return body if encoding.nil? || encoding.strip.empty?
45
+
46
+ if supports_encoding?
47
+ decompress_supported_encoding
48
+ else
49
+ nil
50
+ end
51
+ end
52
+
53
+ protected
54
+
55
+ def supports_encoding?
56
+ SupportedEncodings.keys.include?(encoding)
57
+ end
58
+
59
+ def decompress_supported_encoding
60
+ method = SupportedEncodings[encoding]
61
+ if respond_to?(method, true)
62
+ send(method)
63
+ else
64
+ raise NotImplementedError, "#{self.class.name} has not implemented a decompression method for #{encoding.inspect} encoding."
65
+ end
66
+ end
67
+
68
+ def none
69
+ body
70
+ end
71
+
72
+ def brotli
73
+ return nil unless defined?(::Brotli)
74
+ begin
75
+ ::Brotli.inflate(body)
76
+ rescue StandardError
77
+ nil
78
+ end
79
+ end
80
+
81
+ def lzw
82
+ begin
83
+ if defined?(::LZWS::String)
84
+ ::LZWS::String.decompress(body)
85
+ elsif defined?(::LZW::Simple)
86
+ ::LZW::Simple.new.decompress(body)
87
+ end
88
+ rescue StandardError
89
+ nil
90
+ end
91
+ end
92
+
93
+ def zstd
94
+ return nil unless defined?(::Zstd)
95
+ begin
96
+ ::Zstd.decompress(body)
97
+ rescue StandardError
98
+ nil
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HTTParty
4
+ COMMON_NETWORK_ERRORS = [
5
+ EOFError,
6
+ Errno::ECONNABORTED,
7
+ Errno::ECONNREFUSED,
8
+ Errno::ECONNRESET,
9
+ Errno::EHOSTUNREACH,
10
+ Errno::EINVAL,
11
+ Errno::ENETUNREACH,
12
+ Errno::ENOTSOCK,
13
+ Errno::EPIPE,
14
+ Errno::ETIMEDOUT,
15
+ Net::HTTPBadResponse,
16
+ Net::HTTPHeaderSyntaxError,
17
+ Net::ProtocolError,
18
+ Net::ReadTimeout,
19
+ OpenSSL::SSL::SSLError,
20
+ SocketError,
21
+ Timeout::Error # Also covers subclasses like Net::OpenTimeout
22
+ ].freeze
23
+
24
+ # @abstract Exceptions raised by HTTParty inherit from Error
25
+ class Error < StandardError; end
26
+
27
+ # @abstract Exceptions raised by HTTParty inherit from this because it is funny
28
+ # and if you don't like fun you should be using a different library.
29
+ class Foul < Error; end
30
+
31
+ # Exception raised when you attempt to set a non-existent format
32
+ class UnsupportedFormat < Foul; end
33
+
34
+ # Exception raised when using a URI scheme other than HTTP or HTTPS
35
+ class UnsupportedURIScheme < Foul; end
36
+
37
+ # @abstract Exceptions which inherit from ResponseError contain the Net::HTTP
38
+ # response object accessible via the {#response} method.
39
+ class ResponseError < Foul
40
+ # Returns the response of the last request
41
+ # @return [Net::HTTPResponse] A subclass of Net::HTTPResponse, e.g.
42
+ # Net::HTTPOK
43
+ attr_reader :response
44
+
45
+ # Instantiate an instance of ResponseError with a Net::HTTPResponse object
46
+ # @param [Net::HTTPResponse]
47
+ def initialize(response)
48
+ @response = response
49
+ super(response)
50
+ end
51
+ end
52
+
53
+ # Exception that is raised when request has redirected too many times.
54
+ # Calling {#response} returns the Net:HTTP response object.
55
+ class RedirectionTooDeep < ResponseError; end
56
+
57
+ # Exception that is raised when request redirects and location header is present more than once
58
+ class DuplicateLocationHeader < ResponseError; end
59
+
60
+ # Exception that is raised when common network errors occur.
61
+ class NetworkError < Foul; end
62
+
63
+ # Exception that is raised when an absolute URI is used that doesn't match
64
+ # the configured base_uri, which could indicate an SSRF attempt.
65
+ class UnsafeURIError < Foul; end
66
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'erb'
4
+
5
+ module HTTParty
6
+ module HashConversions
7
+ # @return <String> This hash as a query string
8
+ #
9
+ # @example
10
+ # { name: "Bob",
11
+ # address: {
12
+ # street: '111 Ruby Ave.',
13
+ # city: 'Ruby Central',
14
+ # phones: ['111-111-1111', '222-222-2222']
15
+ # }
16
+ # }.to_params
17
+ # #=> "name=Bob&address[city]=Ruby Central&address[phones][]=111-111-1111&address[phones][]=222-222-2222&address[street]=111 Ruby Ave."
18
+ def self.to_params(hash)
19
+ hash.to_hash.map { |k, v| normalize_param(k, v) }.join.chop
20
+ end
21
+
22
+ # @param key<Object> The key for the param.
23
+ # @param value<Object> The value for the param.
24
+ #
25
+ # @return <String> This key value pair as a param
26
+ #
27
+ # @example normalize_param(:name, "Bob Jones") #=> "name=Bob%20Jones&"
28
+ def self.normalize_param(key, value)
29
+ normalized_keys = normalize_keys(key, value)
30
+
31
+ normalized_keys.flatten.each_slice(2).inject(''.dup) do |string, (k, v)|
32
+ string << "#{ERB::Util.url_encode(k)}=#{ERB::Util.url_encode(v.to_s)}&"
33
+ end
34
+ end
35
+
36
+ def self.normalize_keys(key, value)
37
+ stack = []
38
+ normalized_keys = []
39
+
40
+ if value.respond_to?(:to_ary)
41
+ if value.empty?
42
+ normalized_keys << ["#{key}[]", '']
43
+ else
44
+ normalized_keys = value.to_ary.flat_map do |element|
45
+ normalize_keys("#{key}[]", element)
46
+ end
47
+ end
48
+ elsif value.respond_to?(:to_hash)
49
+ stack << [key, value.to_hash]
50
+ else
51
+ normalized_keys << [key.to_s, value]
52
+ end
53
+
54
+ stack.each do |parent, hash|
55
+ hash.each do |child_key, child_value|
56
+ if child_value.respond_to?(:to_hash)
57
+ stack << ["#{parent}[#{child_key}]", child_value.to_hash]
58
+ elsif child_value.respond_to?(:to_ary)
59
+ child_value.to_ary.each do |v|
60
+ normalized_keys << normalize_keys("#{parent}[#{child_key}][]", v).flatten
61
+ end
62
+ else
63
+ normalized_keys << normalize_keys("#{parent}[#{child_key}]", child_value).flatten
64
+ end
65
+ end
66
+ end
67
+
68
+ normalized_keys
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HTTParty
4
+ class HeadersProcessor
5
+ attr_reader :headers, :options
6
+
7
+ def initialize(headers, options)
8
+ @headers = headers
9
+ @options = options
10
+ end
11
+
12
+ def call
13
+ return unless options[:headers]
14
+
15
+ options[:headers] = headers.merge(options[:headers]) if headers.any?
16
+ options[:headers] = Utils.stringify_keys(process_dynamic_headers)
17
+ end
18
+
19
+ private
20
+
21
+ def process_dynamic_headers
22
+ options[:headers].each_with_object({}) do |header, processed_headers|
23
+ key, value = header
24
+ processed_headers[key] = if value.respond_to?(:call)
25
+ value.arity == 0 ? value.call : value.call(options)
26
+ else
27
+ value
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HTTParty
4
+ module Logger
5
+ class ApacheFormatter #:nodoc:
6
+ TAG_NAME = HTTParty.name
7
+
8
+ attr_accessor :level, :logger
9
+
10
+ def initialize(logger, level)
11
+ @logger = logger
12
+ @level = level.to_sym
13
+ end
14
+
15
+ def format(request, response)
16
+ @request = request
17
+ @response = response
18
+
19
+ logger.public_send level, message
20
+ end
21
+
22
+ private
23
+
24
+ attr_reader :request, :response
25
+
26
+ def message
27
+ "[#{TAG_NAME}] [#{current_time}] #{response.code} \"#{http_method} #{path}\" #{content_length || '-'} "
28
+ end
29
+
30
+ def current_time
31
+ Time.now.strftime('%Y-%m-%d %H:%M:%S %z')
32
+ end
33
+
34
+ def http_method
35
+ request.http_method.name.split('::').last.upcase
36
+ end
37
+
38
+ def path
39
+ request.path.to_s
40
+ end
41
+
42
+ def content_length
43
+ response.respond_to?(:headers) ? response.headers['Content-Length'] : response['Content-Length']
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HTTParty
4
+ module Logger
5
+ class CurlFormatter #:nodoc:
6
+ TAG_NAME = HTTParty.name
7
+ OUT = '>'
8
+ IN = '<'
9
+
10
+ attr_accessor :level, :logger
11
+
12
+ def initialize(logger, level)
13
+ @logger = logger
14
+ @level = level.to_sym
15
+ @messages = []
16
+ end
17
+
18
+ def format(request, response)
19
+ @request = request
20
+ @response = response
21
+
22
+ log_request
23
+ log_response
24
+
25
+ logger.public_send level, messages.join("\n")
26
+ end
27
+
28
+ private
29
+
30
+ attr_reader :request, :response
31
+ attr_accessor :messages
32
+
33
+ def log_request
34
+ log_url
35
+ log_headers
36
+ log_query
37
+ log OUT, request.raw_body if request.raw_body
38
+ log OUT
39
+ end
40
+
41
+ def log_response
42
+ log IN, "HTTP/#{response.http_version} #{response.code}"
43
+ log_response_headers
44
+ log IN, "\n#{response.body}"
45
+ log IN
46
+ end
47
+
48
+ def log_url
49
+ http_method = request.http_method.name.split('::').last.upcase
50
+ uri = if request.options[:base_uri]
51
+ request.options[:base_uri] + request.path.path
52
+ else
53
+ request.path.to_s
54
+ end
55
+
56
+ log OUT, "#{http_method} #{uri}"
57
+ end
58
+
59
+ def log_headers
60
+ return unless request.options[:headers] && request.options[:headers].size > 0
61
+
62
+ log OUT, 'Headers: '
63
+ log_hash request.options[:headers]
64
+ end
65
+
66
+ def log_query
67
+ return unless request.options[:query]
68
+
69
+ log OUT, 'Query: '
70
+ log_hash request.options[:query]
71
+ end
72
+
73
+ def log_response_headers
74
+ headers = response.respond_to?(:headers) ? response.headers : response
75
+ response.each_header do |response_header|
76
+ log IN, "#{response_header.capitalize}: #{headers[response_header]}"
77
+ end
78
+ end
79
+
80
+ def log_hash(hash)
81
+ hash.each { |k, v| log(OUT, "#{k}: #{v}") }
82
+ end
83
+
84
+ def log(direction, line = '')
85
+ messages << "[#{TAG_NAME}] [#{current_time}] #{direction} #{line}"
86
+ end
87
+
88
+ def current_time
89
+ Time.now.strftime("%Y-%m-%d %H:%M:%S %z")
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'httparty/logger/apache_formatter'
4
+ require 'httparty/logger/curl_formatter'
5
+ require 'httparty/logger/logstash_formatter'
6
+
7
+ module HTTParty
8
+ module Logger
9
+ def self.formatters
10
+ @formatters ||= {
11
+ :curl => Logger::CurlFormatter,
12
+ :apache => Logger::ApacheFormatter,
13
+ :logstash => Logger::LogstashFormatter,
14
+ }
15
+ end
16
+
17
+ def self.add_formatter(name, formatter)
18
+ raise HTTParty::Error.new("Log Formatter with name #{name} already exists") if formatters.include?(name)
19
+ formatters.merge!(name.to_sym => formatter)
20
+ end
21
+
22
+ def self.build(logger, level, formatter)
23
+ level ||= :info
24
+ formatter ||= :apache
25
+
26
+ logger_klass = formatters[formatter] || Logger::ApacheFormatter
27
+ logger_klass.new(logger, level)
28
+ end
29
+ end
30
+ end