Class: Crunchbase::API
- Inherits:
-
Object
- Object
- Crunchbase::API
- Defined in:
- lib/crunchbase/api.rb
Constant Summary collapse
- SUPPORTED_ENTITIES =
['organizations', 'organization', 'people', 'person', 'products', 'product', 'funding_rounds', 'funding-round', 'acquisition', 'ipo', 'fund-raise', 'locations', 'categories', 'offices', 'customers']
- RESOURCE_NAME =
Must be overridden in subclasses
"undefined"- RESOURCE_LIST =
"undefineds"- ORDER_CREATED_AT_ASC =
'created_at asc'- ORDER_CREATED_AT_DESC =
'created_at desc'- ORDER_UPDATED_AT_ASC =
'updated_at asc'- ORDER_UPDATED_AT_DESC =
'updated_at desc'
Class Attribute Summary collapse
-
.base_url ⇒ Object
Returns the value of attribute base_url.
-
.debug ⇒ Object
Returns the value of attribute debug.
-
.image_url ⇒ Object
Returns the value of attribute image_url.
-
.key ⇒ Object
Returns the value of attribute key.
-
.redirect_limit ⇒ Object
Returns the value of attribute redirect_limit.
-
.site_url ⇒ Object
Returns the value of attribute site_url.
-
.timeout_limit ⇒ Object
Returns the value of attribute timeout_limit.
-
.version ⇒ Object
Returns the value of attribute version.
Class Method Summary collapse
- .all(entity) ⇒ Object
- .api_url ⇒ Object
- .collect_parameters(options) ⇒ Object
-
.fetch(permalink, object_name) ⇒ Object
Fetches URI for the permalink interface.
-
.get_json_response(uri) ⇒ Object
Gets specified URI, then parses the returned JSON.
-
.get_url_following_redirects(uri_str, limit = 10) ⇒ Object
Performs actual HTTP requests, recursively if a redirect response is encountered.
-
.list(options, object_lists) ⇒ Object
Fetches URI for the search interface.
-
.lists_for_permalink(permalink, category, options) ⇒ Object
Searches for a permalink in a particular category.
-
.parser ⇒ Object
Returns the JSON parser, whether that’s an instance of Yajl or JSON.
-
.search(options, object_lists) ⇒ Object
Fetches URI for the search interface.
- .single_entity(permalink, entity_name) ⇒ Object
Class Attribute Details
.base_url ⇒ Object
Returns the value of attribute base_url.
36 37 38 |
# File 'lib/crunchbase/api.rb', line 36 def base_url @base_url end |
.debug ⇒ Object
Returns the value of attribute debug.
36 37 38 |
# File 'lib/crunchbase/api.rb', line 36 def debug @debug end |
.image_url ⇒ Object
Returns the value of attribute image_url.
36 37 38 |
# File 'lib/crunchbase/api.rb', line 36 def image_url @image_url end |
.key ⇒ Object
Returns the value of attribute key.
36 37 38 |
# File 'lib/crunchbase/api.rb', line 36 def key @key end |
.redirect_limit ⇒ Object
Returns the value of attribute redirect_limit.
36 37 38 |
# File 'lib/crunchbase/api.rb', line 36 def redirect_limit @redirect_limit end |
.site_url ⇒ Object
Returns the value of attribute site_url.
36 37 38 |
# File 'lib/crunchbase/api.rb', line 36 def site_url @site_url end |
.timeout_limit ⇒ Object
Returns the value of attribute timeout_limit.
36 37 38 |
# File 'lib/crunchbase/api.rb', line 36 def timeout_limit @timeout_limit end |
.version ⇒ Object
Returns the value of attribute version.
36 37 38 |
# File 'lib/crunchbase/api.rb', line 36 def version @version end |
Class Method Details
.all(entity) ⇒ Object
50 51 52 |
# File 'lib/crunchbase/api.rb', line 50 def self.all(entity) get_json_response( api_url + entity ) end |
.api_url ⇒ Object
39 40 41 |
# File 'lib/crunchbase/api.rb', line 39 def api_url base_url.gsub(/\/$/, '') + '/v/' + version + '/' end |
.collect_parameters(options) ⇒ Object
93 94 95 96 97 |
# File 'lib/crunchbase/api.rb', line 93 def self.collect_parameters() require "cgi" .map{|k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"}.join('&') end |
.fetch(permalink, object_name) ⇒ Object
Fetches URI for the permalink interface.
66 67 68 69 70 |
# File 'lib/crunchbase/api.rb', line 66 def self.fetch(permalink, object_name) uri = api_url + "#{object_name}/#{permalink}" get_json_response(uri) end |
.get_json_response(uri) ⇒ Object
Gets specified URI, then parses the returned JSON. Raises Timeout error if request time exceeds set limit. Raises CrunchException if returned JSON contains an error.
114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/crunchbase/api.rb', line 114 def self.get_json_response(uri) raise CrunchException, "User key required, visit http://developer.crunchbase.com" unless @key uri = uri + "#{uri.match('\?') ? "&" : "?"}user_key=#{@key}" resp = Timeout::timeout(@timeout_limit) { get_url_following_redirects(uri, @redirect_limit) } response = parser.parse(resp)["data"] raise CrunchException, response["error"] if response.class == Hash && response["error"] response end |
.get_url_following_redirects(uri_str, limit = 10) ⇒ Object
Performs actual HTTP requests, recursively if a redirect response is encountered. Will raise HTTP error if response is not 200, 404, or 3xx.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/crunchbase/api.rb', line 130 def self.get_url_following_redirects(uri_str, limit = 10) raise CrunchException, 'HTTP redirect too deep' if limit == 0 url = URI.parse(uri_str) if self.debug length = (uri_str.length + 10) puts "*"*length puts "*** #{url} ***" puts "*"*length end response = Net::HTTP.start(url.host, url.port) { |http| http.get(url.request_uri) } case response when Net::HTTPSuccess, Net::HTTPNotFound response.body when Net::HTTPRedirection get_url_following_redirects(response['location'], limit - 1) else response.error! end end |
.list(options, object_lists) ⇒ Object
Fetches URI for the search interface.
84 85 86 87 88 89 90 91 |
# File 'lib/crunchbase/api.rb', line 84 def self.list(, object_lists) [:page] = 1 if [:page].nil? model_name = .delete(:model_name) uri = api_url + "#{object_lists}?" + collect_parameters() Search.new , get_json_response(uri), SearchResult end |
.lists_for_permalink(permalink, category, options) ⇒ Object
Searches for a permalink in a particular category. Demo: api.crunchbase.com/v/2/organization/facebook/offices?user_key=key
101 102 103 104 105 106 107 108 109 |
# File 'lib/crunchbase/api.rb', line 101 def self.lists_for_permalink(permalink, category, ) [:page] = 1 if [:page].nil? [:order] = ORDER_CREATED_AT_ASC if [:order].nil? model_name = .delete(:model_name) uri = api_url + "organization/#{permalink}/#{category}?#{collect_parameters()}" Search.new , get_json_response(uri), model_name end |
.parser ⇒ Object
Returns the JSON parser, whether that’s an instance of Yajl or JSON
57 58 59 60 61 62 63 |
# File 'lib/crunchbase/api.rb', line 57 def self.parser if defined?(Yajl) Yajl::Parser else JSON end end |
.search(options, object_lists) ⇒ Object
Fetches URI for the search interface.
73 74 75 76 77 78 79 80 |
# File 'lib/crunchbase/api.rb', line 73 def self.search(, object_lists) [:page] = 1 if [:page].nil? [:order] = ORDER_CREATED_AT_ASC if [:order].nil? uri = api_url + "#{object_lists}?" + collect_parameters() get_json_response(uri) end |
.single_entity(permalink, entity_name) ⇒ Object
44 45 46 47 48 |
# File 'lib/crunchbase/api.rb', line 44 def self.single_entity(permalink, entity_name) raise CrunchException, "Unsupported Entity Type" unless SUPPORTED_ENTITIES.include?(entity_name) fetch(permalink, entity_name) end |