Permalink
Browse files

Require API key, add tests

  • Loading branch information...
1 parent 46b56ef commit c74a1c0d3fec1610e0432fc5f6e6379e390c0b48 @tylercunnion committed Oct 25, 2012
Showing with 25 additions and 4 deletions.
  1. +1 −0 .gitignore
  2. +6 −0 README.rdoc
  3. +3 −2 lib/crunchbase/api.rb
  4. +1 −0 spec/apikey.example.yml
  5. +8 −1 spec/crunchbase/api_spec.rb
  6. +6 −1 spec/spec_helper.rb
View
@@ -1,3 +1,4 @@
*.swp
/pkg
/doc
+/spec/apikey.yml
View
@@ -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
View
@@ -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)
@@ -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)
}
@@ -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
View
@@ -0,0 +1 @@
+key: <your_key_goes_here>
@@ -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
View
@@ -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.