From c0588ac5343edc1fb2a9d98683b92d0ebfa11d8f Mon Sep 17 00:00:00 2001 From: Jon Cooper Date: Thu, 31 Mar 2011 12:32:47 -0700 Subject: [PATCH 1/2] Set token & secret from environment variables --- README.rdoc | 11 +++++++++-- lib/simple_geo.rb | 4 +++- lib/simple_geo/client.rb | 10 +++++++++- spec/client_spec.rb | 4 ---- spec/feature_spec.rb | 4 ---- 5 files changed, 21 insertions(+), 12 deletions(-) diff --git a/README.rdoc b/README.rdoc index 3702064..a842034 100644 --- a/README.rdoc +++ b/README.rdoc @@ -45,8 +45,15 @@ For more examples see: spec/client_spec.rb * You're going to require rspec json oauth fakeweb, vcr and autotest (plus their dependencies) == Adding tests -* If you want to add new tests, you will need to put a valid token and secret into a before block - similar to lines 4-6 in spec/client_spec.rb, but you'll have to enter valid credentials. -* Look at spec/features_spec.rb for examples of how to write a vcr test. First time it runs the remote connection. After that it uses the data stored in the vcr/cassette directory. Make sure to sanitize your yml vcr files before committing them by removing your oauth token which is stored there by default. + +If you want to add new tests, you will need to set a valid token and secret on environment variables. + +Set your API keys as environment variables (see https://simplegeo.com/tokens/) + +* export SIMPLEGEO_TOKEN=[your key] +* export SIMPLEGEO_SECRET=[your secret] + +Next, look at spec/features_spec.rb for examples of how to write a vcr test. First time it runs the remote connection. After that it uses the data stored in the vcr/cassette directory. Make sure to sanitize your yml vcr files before committing them by removing your oauth token which is stored there by default. == Copyright diff --git a/lib/simple_geo.rb b/lib/simple_geo.rb index 8fd3820..e8de5f1 100644 --- a/lib/simple_geo.rb +++ b/lib/simple_geo.rb @@ -5,8 +5,8 @@ require 'simple_geo/hash_utils' require 'simple_geo/connection' require 'simple_geo/endpoint' -require 'simple_geo/client' require 'simple_geo/record' +require 'simple_geo/context' module SimpleGeo API_VERSION = '1.0'.freeze @@ -21,3 +21,5 @@ class Unavailable < SimpleGeoError; end class DecodeError < SimpleGeoError; end class NoConnectionEstablished < SimpleGeoError; end end + +require 'simple_geo/client' diff --git a/lib/simple_geo/client.rb b/lib/simple_geo/client.rb index 07fc00c..47dda53 100644 --- a/lib/simple_geo/client.rb +++ b/lib/simple_geo/client.rb @@ -215,7 +215,15 @@ def put(endpoint, data=nil) @@connection.put endpoint, data end end - + + # Set API keys from the environment + + token, secret = [ENV['SIMPLEGEO_TOKEN'], ENV['SIMPLEGEO_SECRET']] + + if token && secret + self.set_credentials(token, secret) + end + end end diff --git a/spec/client_spec.rb b/spec/client_spec.rb index d59b176..708ca3e 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -1,10 +1,6 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper') describe "Client" do - before do - SimpleGeo::Client.set_credentials 'token', 'secret' - end - context "getting a record" do context "with an id for an existing record" do before do diff --git a/spec/feature_spec.rb b/spec/feature_spec.rb index 838c1a4..f8020c8 100644 --- a/spec/feature_spec.rb +++ b/spec/feature_spec.rb @@ -1,10 +1,6 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper') describe "Client" do - before do - SimpleGeo::Client.set_credentials('token', 'secret') - end - it "should return a feature when requesting it using the correct handle" do VCR.use_cassette("feature_valid", :record => :new_episodes) do From 20bc14b7830e766759b9752bbfebff56679f7b3f Mon Sep 17 00:00:00 2001 From: Jon Cooper Date: Thu, 31 Mar 2011 12:33:45 -0700 Subject: [PATCH 2/2] Added simple Context class to support country identification --- lib/simple_geo/context.rb | 15 +++++++++++++++ spec/context_spec.rb | 17 +++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 lib/simple_geo/context.rb create mode 100644 spec/context_spec.rb diff --git a/lib/simple_geo/context.rb b/lib/simple_geo/context.rb new file mode 100644 index 0000000..c6e4ead --- /dev/null +++ b/lib/simple_geo/context.rb @@ -0,0 +1,15 @@ +module SimpleGeo + class Context + def initialize(context) + @context = context + end + + def country + national_category = lambda { |feature| + false unless feature.has_key? :classifiers + true if feature[:classifiers].find{ |classifier| classifier[:category] == 'National' } + } + @context[:features].find(&national_category)[:name] + end + end +end diff --git a/spec/context_spec.rb b/spec/context_spec.rb new file mode 100644 index 0000000..df76fe3 --- /dev/null +++ b/spec/context_spec.rb @@ -0,0 +1,17 @@ +require File.expand_path(File.dirname(__FILE__) + '/spec_helper') + +describe "Context" do + before do + VCR.use_cassette("context", :record => :new_episodes) do + ip = '213.24.76.23' # www.fsb.ru + context_hash = SimpleGeo::Client.get_context_ip(ip) + @context = SimpleGeo::Context.new(context_hash) + end + end + + context "#country" do + it "should work" do + @context.country.should == "Russia" + end + end +end \ No newline at end of file