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

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

Direct Known Subclasses

HashHttpGet, HashHttpPost

Defined Under Namespace

Classes: Headers

Constant Summary collapse

DEFAULT_BODY =
''
DEFAULT_FOLLOW_REDIRECT =
true
DEFAULT_HEADERS =
{}.freeze
DEFAULT_PARAMS =
{}.freeze
USER_AGENT_KEY =
'user-agent'
USER_AGENT_VALUE =
'aranha-parsers'

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#source_as_hash, #source_as_hash?, #source_as_uri, #source_as_uri?, #url

Class Method Details

.http_methodObject



13
14
15
# File 'lib/aranha/parsers/source_address/hash_http_base.rb', line 13

def http_method
  const_get 'HTTP_METHOD'
end

Instance Method Details

#bodyObject



27
28
29
# File 'lib/aranha/parsers/source_address/hash_http_base.rb', line 27

def body
  param(:body, DEFAULT_BODY)
end

#contentObject



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

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)


31
32
33
# File 'lib/aranha/parsers/source_address/hash_http_base.rb', line 31

def follow_redirect?
  param(:follow_redirect, DEFAULT_FOLLOW_REDIRECT)
end

#headersObject



35
36
37
# File 'lib/aranha/parsers/source_address/hash_http_base.rb', line 35

def headers
  param(:headers, DEFAULT_HEADERS)
end

#param(key, default_value) ⇒ Object



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

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

#paramsObject



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

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

#serializeObject



39
40
41
# File 'lib/aranha/parsers/source_address/hash_http_base.rb', line 39

def serialize
  source_as_hash.to_yaml
end

#uriAddressable::URI

Returns:

  • (Addressable::URI)


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

def uri
  ::Addressable::URI.parse(source_as_hash.fetch(:url))
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  source_as_hash? &&
    source_as_hash[:method].to_s.downcase.strip == self.class.http_method.to_s
end