Module: AWSCosts::Cache

Extended by:
Cache
Included in:
Cache
Defined in:
lib/awscosts/cache.rb

Constant Summary collapse

BASE_URI =
"http://aws.amazon.com"
BASE_JSONP_URI =
"http://a0.awsstatic.com"

Instance Method Summary collapse

Instance Method Details

#get(uri, base_uri = BASE_URI) {|| ... } ⇒ Object

Yields:

  • ()


10
11
12
13
# File 'lib/awscosts/cache.rb', line 10

def get uri, base_uri = BASE_URI, &block
  cache["#{base_uri}#{uri}"] ||= Oj::load(HTTParty.get("#{base_uri}#{uri}").body)
  yield cache["#{base_uri}#{uri}"]
end

#get_jsonp(uri, base_uri = BASE_JSONP_URI) {|| ... } ⇒ Object

Yields:

  • ()


15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/awscosts/cache.rb', line 15

def get_jsonp uri, base_uri = BASE_JSONP_URI, &block
  attempts = 0
  cache["#{base_uri}#{uri}"] ||= begin
    extract_json_from_callback(HTTParty.get("#{base_uri}#{uri}").body)
  rescue NoMethodError
    attempts += 1
    retry if attempts < 5
    raise "Failed to retrieve or parse data for #{base_uri}#{uri}"
  end

  yield cache["#{base_uri}#{uri}"]
end