Class: Determinator::Retrieve::HttpRetriever

Inherits:
Object
  • Object
show all
Defined in:
lib/determinator/retrieve/http_retriever.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection:) ⇒ HttpRetriever

Returns a new instance of HttpRetriever.

Raises:

  • (ArgumentError)


7
8
9
10
# File 'lib/determinator/retrieve/http_retriever.rb', line 7

def initialize(connection:)
  raise ArgumentError, "client must be a Faraday::Connection" unless connection.is_a?(Faraday::Connection)
  @connection = connection
end

Instance Method Details

#after_retrieve(&block) ⇒ Object



40
41
42
# File 'lib/determinator/retrieve/http_retriever.rb', line 40

def after_retrieve(&block)
  @after_retrieve = block
end

#before_retrieve(&block) ⇒ Object



36
37
38
# File 'lib/determinator/retrieve/http_retriever.rb', line 36

def before_retrieve(&block)
  @before_retrieve = block
end

#get_name(url) ⇒ String?

Returns a feature name given a actor-tracking url. Used so we are able to expire a cache using a feature name given an event url.

Not intended to be generic, and makes no guarantees about support for alternative url schemes.

Parameters:

  • url (String)

    a actor tracking url

Returns:

  • (String, nil)

    a feature name or nil



32
33
34
# File 'lib/determinator/retrieve/http_retriever.rb', line 32

def get_name(url)
  (url.match('features\/(.*)\z') || [])[1]
end

#retrieve(name) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/determinator/retrieve/http_retriever.rb', line 12

def retrieve(name)
  before_hook
  response = @connection.get("/features/#{name}")
  after_hook(response.status)
  return Determinator::Serializers::JSON.load(response.body) if response.status == 200
  return MissingResponse.new if response.status == 404
rescue => e
  Determinator.notice_error(e)
  after_hook(500, e)
  ErrorResponse.new
end