Skip to content
Browse files

Add Typhoeus NetAdapter to show example of caching

  • Loading branch information...
1 parent e35efa9 commit a4ca6827c9b42e53083f1093cd28b50146610ef6 @mnoack mnoack committed Jan 12, 2014
Showing with 65 additions and 0 deletions.
  1. +1 −0 geokit.gemspec
  2. +1 −0 lib/geokit/geocoders.rb
  3. +13 −0 lib/geokit/net_adapter/typhoeus.rb
  4. +4 −0 test/helper.rb
  5. +46 −0 test/test_net_adapter.rb
View
1 geokit.gemspec
@@ -31,4 +31,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'coveralls'
spec.add_development_dependency 'vcr'
spec.add_development_dependency 'webmock' # used in vcr
+ spec.add_development_dependency 'typhoeus' # used in net_adapter
end
View
1 lib/geokit/geocoders.rb
@@ -1,5 +1,6 @@
require 'net/http'
require 'geokit/net_adapter/net_http'
+require 'geokit/net_adapter/typhoeus'
require 'ipaddr'
require 'rexml/document'
require 'yaml'
View
13 lib/geokit/net_adapter/typhoeus.rb
@@ -0,0 +1,13 @@
+module Geokit
+ module NetAdapter
+ class Typhoeus
+ def self.do_get(url)
+ ::Typhoeus.get(url.to_s)
+ end
+
+ def self.success?(response)
+ response.success?
+ end
+ end
+ end
+end
View
4 test/helper.rb
@@ -42,6 +42,10 @@ class MockSuccess < Net::HTTPSuccess #:nodoc: all
def initialize
@header = {}
end
+
+ def success? # Typhoeus
+ true
+ end
end
class MockFailure < Net::HTTPServiceUnavailable #:nodoc: all
View
46 test/test_net_adapter.rb
@@ -0,0 +1,46 @@
+require File.join(File.dirname(__FILE__), 'helper')
+
+# Base class for testing geocoders.
+class NetAdapterTest < Test::Unit::TestCase #:nodoc: all
+ class Geokit::Geocoders::CachedGeocoder < Geokit::Geocoders::Geocoder
+ def self.parse_json(hash)
+ hash
+ end
+ end
+
+ class SuperSimpleCache
+ def initialize
+ @cache = {}
+ end
+
+ def write(key, value)
+ @cache[key] = value
+ end
+
+ def fetch(key)
+ @cache[key]
+ end
+ end
+
+ RESULT = '{"name":"json"}'
+ RESULT_HASH = {"name" => "json"}
+
+ # Defines common test fixtures.
+ def setup
+ @url = 'http://www.cacheme.com'
+ @address = 'San Francisco, CA'
+ end
+
+ def test_cache
+ old_adapter = Geokit::Geocoders::net_adapter
+ Geokit::Geocoders::net_adapter = Geokit::NetAdapter::Typhoeus
+ Typhoeus::Config.cache = SuperSimpleCache
+ success = MockSuccess.new
+ success.expects(:body).returns(RESULT)
+ url = 'http://www.cacheme.com'
+ Geokit::NetAdapter::Typhoeus.expects(:do_get).with(@url).returns(success)
+ assert_equal RESULT_HASH, Geokit::Geocoders::CachedGeocoder.process(:json, @url)
+ Typhoeus::Config.cache = nil
+ Geokit::Geocoders::net_adapter = old_adapter
+ end
+end

0 comments on commit a4ca682

Please sign in to comment.
Something went wrong with that request. Please try again.