Skip to content

Commit

Permalink
Require API key, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tylercunnion committed Oct 25, 2012
1 parent 46b56ef commit c74a1c0
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
*.swp
/pkg
/doc
/spec/apikey.yml
6 changes: 6 additions & 0 deletions README.rdoc
Expand Up @@ -69,6 +69,12 @@ list does not entail a delay every ten items. Of course, the list is enormous
(the list of all companies returns over 84,500 entries as of this writing), so
it's little consolation.

== Contributing

Contributions are welcome. Note that in order to run the test suite, you need to
include an API key in +spec/apikey.yml+. This file is ignored by git. An example
file is provided for convenience.

== Copyright

Copyright (c) 2011-12 Tyler Cunnion. This software is made available under the
Expand Down
5 changes: 3 additions & 2 deletions lib/crunchbase/api.rb
Expand Up @@ -20,7 +20,6 @@ class API
SUPPORTED_ENTITIES = ['person', 'company', 'financial-organization', 'product', 'service-provider']
@timeout_limit = 60
@redirect_limit = 2
@key = ''
class << self; attr_accessor :timeout_limit, :redirect_limit, :key end

def self.single_entity(permalink, entity_name)
Expand Down Expand Up @@ -69,6 +68,8 @@ def self.permalink(parameters, category)
# if request time exceeds set limit. Raises CrunchException if returned
# JSON contains an error.
def self.get_json_response(uri)
raise CrunchException, "API key required, visit http://developer.crunchbase.com" unless @key
uri = uri + "#{uri.match('\?') ? "&" : "?"}api_key=#{@key}"
resp = Timeout::timeout(@timeout_limit) {
get_url_following_redirects(uri, @redirect_limit)
}
Expand All @@ -83,7 +84,7 @@ def self.get_json_response(uri)
def self.get_url_following_redirects(uri_str, limit = 10)
raise CrunchException, 'HTTP redirect too deep' if limit == 0

url = URI.parse(uri_str + "?api=#{@key}")
url = URI.parse(uri_str)
response = Net::HTTP.start(url.host, url.port) { |http| http.get(url.request_uri) }
case response
when Net::HTTPSuccess, Net::HTTPNotFound
Expand Down
1 change: 1 addition & 0 deletions spec/apikey.example.yml
@@ -0,0 +1 @@
key: <your_key_goes_here>
9 changes: 8 additions & 1 deletion spec/crunchbase/api_spec.rb
Expand Up @@ -4,9 +4,16 @@
module Crunchbase
describe API do

it "should raise exception for missing API key" do
key = API.key
API.key = nil
expect { API.fetch('steve-jobs', 'person') }.to raise_error
API.key = key
end

it "should remove control characters" do
cargurus = File.open(File.join(File.dirname(__FILE__), "..", "fixtures", "cargurus.js")).read
API.should_receive(:get_url_following_redirects).with("http://api.crunchbase.com/v/1/company/cargurus.js", 2).and_return(cargurus)
API.should_receive(:get_url_following_redirects).with(/^http:\/\/api.crunchbase.com\/v\/1\/company\/cargurus.js/, 2).and_return(cargurus)
lambda { API.single_entity("cargurus", "company") }.should_not raise_error
end

Expand Down
7 changes: 6 additions & 1 deletion spec/spec_helper.rb
@@ -1,4 +1,9 @@
$LOAD_PATH << File.join(File.dirname(__FILE__), "..", "lib")
require 'rspec'
require 'crunchbase'
require 'date'
require 'date'
require 'yaml'

key_yaml = YAML.load_file(File.join(File.dirname(__FILE__),'apikey.yml'))

Crunchbase::API.key = key_yaml["key"]

0 comments on commit c74a1c0

Please sign in to comment.