lib/money_oxr/rates_store.rb in money-oxr-0.3.0 vs lib/money_oxr/rates_store.rb in money-oxr-0.4.0
- old
+ new
@@ -53,19 +53,24 @@
return true if last_updated_at.nil?
last_updated_at + max_age < Time.now
end
def load_from_api
+ # When loading from the API, set the last_updated_at to now.
+ # "timestamp" value in response may be days old (it may not update over
+ # the weekend)
+ now = Time.now
json = get_json_from_api
# Protect against saving or loading nil/bad data from API.
return unless json && json =~ /rates/
if cache_path
write_cache_file(json)
load_from_cache_path
else
load_json(json)
end
+ @last_updated_at = now
end
def get_json_from_api
open(api_uri).read
rescue OpenURI::HTTPError, SocketError
@@ -73,10 +78,10 @@
warn "#{$!.class}: #{$!.message}"
nil
end
def api_uri
- "https://openexchangerates.org/api/latest.json?source=#{source}&app_id=#{app_id}"
+ "https://openexchangerates.org/api/latest.json?base=#{source}&app_id=#{app_id}"
end
def load_from_cache_path
load_json(File.read(cache_path))
end