Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api keys in environment variables and helper class to do stuff with SimpleGeo Context results #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion lib/simple_geo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,3 +21,5 @@ class Unavailable < SimpleGeoError; end
class DecodeError < SimpleGeoError; end
class NoConnectionEstablished < SimpleGeoError; end
end

require 'simple_geo/client'
10 changes: 9 additions & 1 deletion lib/simple_geo/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 15 additions & 0 deletions lib/simple_geo/context.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 0 additions & 4 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
17 changes: 17 additions & 0 deletions spec/context_spec.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 0 additions & 4 deletions spec/feature_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down