Class: Aranha::Parsers::SourceAddress::HashHttpBase
- Inherits:
-
Object
- Object
- Aranha::Parsers::SourceAddress::HashHttpBase
show all
- Defined in:
- lib/aranha/parsers/source_address/hash_http_base.rb
Constant Summary
collapse
- DEFAULT_BODY =
''
- DEFAULT_FOLLOW_REDIRECT =
true
{}.freeze
- DEFAULT_PARAMS =
{}.freeze
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.http_method ⇒ Object
16
17
18
|
# File 'lib/aranha/parsers/source_address/hash_http_base.rb', line 16
def http_method
const_get 'HTTP_METHOD'
end
|
.valid_source?(source) ⇒ Boolean
20
21
22
23
|
# File 'lib/aranha/parsers/source_address/hash_http_base.rb', line 20
def valid_source?(source)
source.is_a?(::Hash) &&
source.with_indifferent_access[:method].to_s.downcase.strip == http_method.to_s
end
|
Instance Method Details
#body ⇒ Object
38
39
40
|
# File 'lib/aranha/parsers/source_address/hash_http_base.rb', line 38
def body
param(:body, DEFAULT_BODY)
end
|
#content ⇒ Object
66
67
68
69
70
71
72
73
|
# File 'lib/aranha/parsers/source_address/hash_http_base.rb', line 66
def content
req = faraday_request
return req.body if req.status == 200
raise ::Aranha::Parsers::SourceAddress::FetchContentError.new(
"Get #{url} returned #{req.status.to_i}", req
)
end
|
#faraday_connection ⇒ Faraday
59
60
61
62
63
64
|
# File 'lib/aranha/parsers/source_address/hash_http_base.rb', line 59
def faraday_connection
::Faraday.new do |f|
f.request :gzip
f.response :follow_redirects if follow_redirect?
end
end
|
#follow_redirect? ⇒ Boolean
42
43
44
|
# File 'lib/aranha/parsers/source_address/hash_http_base.rb', line 42
def follow_redirect?
param(:follow_redirect, DEFAULT_FOLLOW_REDIRECT)
end
|
46
47
48
|
# File 'lib/aranha/parsers/source_address/hash_http_base.rb', line 46
def
param(:headers, DEFAULT_HEADERS)
end
|
#param(key, default_value) ⇒ Object
75
76
77
|
# File 'lib/aranha/parsers/source_address/hash_http_base.rb', line 75
def param(key, default_value)
source[key] || params[key] || default_value
end
|
#params ⇒ Object
79
80
81
|
# File 'lib/aranha/parsers/source_address/hash_http_base.rb', line 79
def params
source[:params].if_present(DEFAULT_PARAMS)
end
|
#serialize ⇒ Object
54
55
56
|
# File 'lib/aranha/parsers/source_address/hash_http_base.rb', line 54
def serialize
source.to_yaml
end
|
#url ⇒ Object
50
51
52
|
# File 'lib/aranha/parsers/source_address/hash_http_base.rb', line 50
def url
source.fetch(:url)
end
|