Module: GdsApi::TestHelpers::FactCave

Includes:
CommonResponses
Defined in:
lib/gds_api/test_helpers/fact_cave.rb

Constant Summary collapse

FACT_CAVE_ENDPOINT =
Plek.current.find('fact-cave')

Instance Method Summary collapse

Methods included from CommonResponses

#acronymize_slug, #plural_response_base, #response_base, #titleize_slug

Instance Method Details

#fact_cave_does_not_have_a_fact(slug) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/gds_api/test_helpers/fact_cave.rb', line 20

def fact_cave_does_not_have_a_fact(slug)
  response = {
    "_response_info" => { "status" => "not found" }
  }

  stub_request(:get, "#{FACT_CAVE_ENDPOINT}/facts/#{slug}")
    .to_return(:body => response.to_json, :status => 404)
  stub_request(:get, "#{FACT_CAVE_ENDPOINT}/facts/#{slug}.json")
    .to_return(:body => response.to_json, :status => 404)
end

#fact_cave_has_a_fact(slug, value, options = {}) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/gds_api/test_helpers/fact_cave.rb', line 11

def fact_cave_has_a_fact(slug, value, options={})
  response = fact_for_slug(slug, value, options)

  stub_request(:get, "#{FACT_CAVE_ENDPOINT}/facts/#{slug}")
    .to_return(:body => response.to_json, :status => 200)
  stub_request(:get, "#{FACT_CAVE_ENDPOINT}/facts/#{slug}.json")
    .to_return(:body => response.to_json, :status => 200)
end

#fact_for_slug(slug, value = "Sample Value", options = {}) ⇒ Object

Creates a sample fact-cave response hash.

slug - the slug for the fact (also used to generate the title)
value - the raw value for the fact
options:
  formatted_value - the formatted value to use.  If unspecified, this is generated based on the type
  type - the type of the fact - one of [:currency, :date, :numeric, :text].  Defaults to :text
  currency - for the :currency type, an optional currency symbol to prepend to the generated formatted_value
  unit - for :numeric types, an optional unit to append to the generated formatted_value


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gds_api/test_helpers/fact_cave.rb', line 39

def fact_for_slug(slug, value = "Sample Value", options = {})
  formatted_value = options[:formatted_value]
  formatted_value ||= case options[:type]
  when :date
    value.strftime("%e %B %Y")
  when :numeric
    "#{value.to_s}#{options[:unit]}"
  when :currency
    "#{options[:currency]}#{sprintf("%.2f", value)}"
  when :text, nil
    value
  else
    raise "Unknown fact type #{options[:type]}"
  end

  singular_response_base.merge({
    "id" => "#{FACT_CAVE_ENDPOINT}/facts/#{slug}",
    "details" => {
      "description" => "",
      "value" => value,
      "formatted_value" => formatted_value,
    },
    "name" => titleize_slug(slug),
    "updated_at" => Time.now.utc.xmlschema,
  })
end