lib/google_timezone/base.rb in google_timezone-0.0.4 vs lib/google_timezone/base.rb in google_timezone-0.0.5

- old
+ new

@@ -4,12 +4,16 @@ module GoogleTimezone class Error < StandardError; end class Base - ALLOWED_PARAMS = [:language, :sensor, :timestamp, :client, :signature, :key] + ALLOWED_PARAMS = [:language, :timestamp, :client, :signature, :key] + class << self + attr_accessor :default_stub + end + def initialize(*args) @lat, @lon = if args.first.is_a? Array args.first else args[0..1] @@ -19,19 +23,19 @@ @options.reject! { |key, _| !ALLOWED_PARAMS.include?(key) } end def fetch location = [@lat, @lon].join(',') - params = { location: location, sensor: false, timestamp: Time.now.to_i }.merge(@options) + params = { location: location, timestamp: Time.now.to_i }.merge(@options) result = get_result(params) Result.new(result) end def fetch! - result = fetch - raise(GoogleTimezone::Error.new(result.result)) unless result.success? - result + fetch.tap do |result| + raise(GoogleTimezone::Error.new(result.result)) unless result.success? + end end private def hash_to_query(hash) @@ -45,9 +49,9 @@ def extract_options!(args) args.last.is_a?(::Hash) ? args.pop : {} end def get_result(params) - open(url(params)) { |r| JSON.parse(r.read) } + self.class.default_stub || open(url(params)) { |r| JSON.parse(r.read) } end end end