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



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

Returns:

  • (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

#bodyObject



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

def body
  param(:body, DEFAULT_BODY)
end

#contentObject



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_connectionFaraday

Returns:

  • (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

Returns:

  • (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

#headersObject



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

def headers
  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

#paramsObject



79
80
81
# File 'lib/aranha/parsers/source_address/hash_http_base.rb', line 79

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

#serializeObject



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

def serialize
  source.to_yaml
end

#urlObject



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

def url
  source.fetch(:url)
end