Class: Aranha::Parsers::SourceAddress::HashHttpBase

Inherits:
Object
  • Object
show all
Defined in:
lib/aranha/parsers/source_address/hash_http_base.rb

Direct Known Subclasses

HashHttpGet, HashHttpPost

Constant Summary collapse

DEFAULT_BODY =
''
DEFAULT_FOLLOW_REDIRECT =
true
DEFAULT_HEADERS =
{}.freeze
DEFAULT_PARAMS =
{}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.http_methodObject



15
16
17
# File 'lib/aranha/parsers/source_address/hash_http_base.rb', line 15

def http_method
  const_get 'HTTP_METHOD'
end

.valid_source?(source) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
# File 'lib/aranha/parsers/source_address/hash_http_base.rb', line 19

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

#bodyObject



37
38
39
# File 'lib/aranha/parsers/source_address/hash_http_base.rb', line 37

def body
  param(:body, DEFAULT_BODY)
end

#contentObject



57
58
59
60
61
62
# File 'lib/aranha/parsers/source_address/hash_http_base.rb', line 57

def content
  request = http_request
  request.response.body_str
rescue ::EacEnvs::Http::Error => e
  raise ::Aranha::Parsers::SourceAddress::FetchContentError, e.message, request
end

#follow_redirect?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/aranha/parsers/source_address/hash_http_base.rb', line 41

def follow_redirect?
  param(:follow_redirect, DEFAULT_FOLLOW_REDIRECT)
end

#headersObject



45
46
47
# File 'lib/aranha/parsers/source_address/hash_http_base.rb', line 45

def headers
  param(:headers, DEFAULT_HEADERS)
end

#param(key, default_value) ⇒ Object



64
65
66
# File 'lib/aranha/parsers/source_address/hash_http_base.rb', line 64

def param(key, default_value)
  source[key] || params[key] || default_value
end

#paramsObject



68
69
70
# File 'lib/aranha/parsers/source_address/hash_http_base.rb', line 68

def params
  source[:params].if_present(DEFAULT_PARAMS)
end

#serializeObject



53
54
55
# File 'lib/aranha/parsers/source_address/hash_http_base.rb', line 53

def serialize
  source.to_yaml
end

#urlObject



49
50
51
# File 'lib/aranha/parsers/source_address/hash_http_base.rb', line 49

def url
  source.fetch(:url)
end