Module: ReevooMark::Document::Factory

Defined in:
lib/reevoomark/document/factory.rb

Defined Under Namespace

Classes: HeaderSet

Constant Summary collapse

HEADER_MAPPING =
{
  :review_count => 'X-Reevoo-ReviewCount',
  :offer_count => 'X-Reevoo-OfferCount',
  :conversation_count => 'X-Reevoo-ConversationCount',
  :best_price => 'X-Reevoo-BestPrice'
}

Class Method Summary collapse

Class Method Details

.from_response(response) ⇒ Object

Factory method for building a document from a HTTP response.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/reevoomark/document/factory.rb', line 26

def self.from_response(response)
  headers = HeaderSet.new(response.headers)

  counts = HEADER_MAPPING.inject(Hash.new(0)){ |acc, (name, header)|
    acc.merge(name => headers[header].to_i)
  }

  if cache_header = headers['Cache-Control']
    max_age = cache_header.match("max-age=([0-9]+)")[1].to_i
  else
    max_age = 300
  end

  age = headers['Age'].to_i

  ReevooMark::Document.new(
    Time.now,
    max_age,
    age,
    response.status_code,
    response.body,
    counts
  )
end

.new_error_documentObject



51
52
53
# File 'lib/reevoomark/document/factory.rb', line 51

def self.new_error_document
  ReevooMark::Document.new(Time.now, 300, 0, 599, "", {})
end