Class: Crunchbase::API
- Inherits:
-
Object
- Object
- Crunchbase::API
- Defined in:
- lib/crunchbase/api.rb
Constant Summary collapse
- SUPPORTED_ENTITIES =
['organizations', 'people', 'products', 'funding_rounds', 'funding-rounds', 'acquisitions', 'ipos', 'locations', 'categories', 'offices', 'customers', 'degrees', 'experience', 'primary_affiliation', 'videos', 'founded_companies', 'primary_location', 'advisor_at']
- 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
- .api_url ⇒ Object
- .collect_parameters(options) ⇒ Object
- .debug_log!(uri) ⇒ Object
-
.fetch(permalink, object_name) ⇒ Object
Fetches URI for the permalink interface.
-
.funding_rounds_lists(permalink, category, options) ⇒ Object
Visit: API.version/funding-rounds/#permalink/#category?user_key=key">https://api.crunchbase.com/v/#API.version/funding-rounds/#permalink/#category?user_key=key.
-
.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, resource_list) ⇒ Object
Fetches URI for the search interface.
- .lists_for_category(classify_name, permalink, category, options) ⇒ Object
- .organization_lists(permalink, category, options) ⇒ Object
-
.parser ⇒ Object
Returns the JSON parser, whether that's an instance of Yajl or JSON.
-
.person_lists(permalink, category, options) ⇒ Object
Visit: API.version/people/#permalink/#category?user_key=key">https://api.crunchbase.com/v/#API.version/people/#permalink/#category?user_key=key.
-
.search(options, resource_list) ⇒ 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
.api_url ⇒ Object
38 39 40 |
# File 'lib/crunchbase/api.rb', line 38 def api_url base_url.gsub(/\/$/, '') + '/v/' + version + '/' end |
.collect_parameters(options) ⇒ Object
86 87 88 89 90 |
# File 'lib/crunchbase/api.rb', line 86 def self.collect_parameters() require "cgi" .map{|k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"}.join('&') end |
.debug_log!(uri) ⇒ Object
158 159 160 161 162 |
# File 'lib/crunchbase/api.rb', line 158 def self.debug_log!(uri) puts "*"*120 puts "*** #{uri} ***" puts "*"*120 end |
.fetch(permalink, object_name) ⇒ Object
Fetches URI for the permalink interface.
61 62 63 |
# File 'lib/crunchbase/api.rb', line 61 def self.fetch(permalink, object_name) get_json_response( api_url + "#{object_name}/#{permalink}" ) end |
.funding_rounds_lists(permalink, category, options) ⇒ Object
102 103 104 |
# File 'lib/crunchbase/api.rb', line 102 def self.funding_rounds_lists(permalink, category, ) lists_for_category('funding-rounds', permalink, category, ) end |
.get_json_response(uri) ⇒ Object
Gets specified URI, then parses the returned JSON. Raises Timeout error if request time exceeds set limit. Raises Crunchbase::Exception if returned JSON contains an error.
119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/crunchbase/api.rb', line 119 def self.get_json_response(uri) raise Crunchbase::Exception, "User key required, visit http://data.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 Crunchbase::Exception, response["error"] if response.class == Hash && response["error"] && response["error"]["code"] != 500 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.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/crunchbase/api.rb', line 135 def self.get_url_following_redirects(uri_str, limit = 10) raise Crunchbase::Exception, 'HTTP redirect too deep' if limit == 0 uri = URI.parse(uri_str) debug_log!(uri) if debug http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true if uri.scheme == 'https' response = http.start do |h| h.request Net::HTTP::Get.new(uri.request_uri) end case response when Net::HTTPSuccess, Net::HTTPNotFound, Net::HTTPInternalServerError response.body when Net::HTTPRedirection get_url_following_redirects(response['location'], limit - 1) else response.error! end end |
.list(options, resource_list) ⇒ Object
Fetches URI for the search interface.
77 78 79 80 81 82 83 84 |
# File 'lib/crunchbase/api.rb', line 77 def self.list(, resource_list) [:page] = 1 if [:page].nil? model_name = .delete(:model_name) uri = api_url + "#{resource_list}?" + collect_parameters() Crunchbase::Model::Search.new , get_json_response(uri), model_name end |
.lists_for_category(classify_name, permalink, category, options) ⇒ Object
106 107 108 109 110 111 112 113 114 |
# File 'lib/crunchbase/api.rb', line 106 def self.lists_for_category(classify_name, permalink, category, ) [:page] = 1 if [:page].nil? [:order] = ORDER_CREATED_AT_ASC if [:order].nil? model_name = .delete(:model_name) uri = api_url + "#{classify_name}/#{permalink}/#{category}?#{collect_parameters()}" Crunchbase::Model::Search.new , get_json_response(uri), model_name end |
.organization_lists(permalink, category, options) ⇒ Object
92 93 94 |
# File 'lib/crunchbase/api.rb', line 92 def self.organization_lists(permalink, category, ) lists_for_category('organizations', permalink, category, ) end |
.parser ⇒ Object
Returns the JSON parser, whether that's an instance of Yajl or JSON
52 53 54 55 56 57 58 |
# File 'lib/crunchbase/api.rb', line 52 def self.parser if defined?(Yajl) Yajl::Parser else JSON end end |
.person_lists(permalink, category, options) ⇒ Object
97 98 99 |
# File 'lib/crunchbase/api.rb', line 97 def self.person_lists(permalink, category, ) lists_for_category('people', permalink, category, ) end |
.search(options, resource_list) ⇒ Object
Fetches URI for the search interface.
66 67 68 69 70 71 72 73 |
# File 'lib/crunchbase/api.rb', line 66 def self.search(, resource_list) [:page] = 1 if [:page].nil? [:order] = ORDER_CREATED_AT_ASC if [:order].nil? uri = api_url + "#{resource_list}?" + collect_parameters() get_json_response(uri) end |
.single_entity(permalink, entity_name) ⇒ Object
43 44 45 46 47 |
# File 'lib/crunchbase/api.rb', line 43 def self.single_entity(permalink, entity_name) raise CrunchException, "Unsupported Entity Type" unless SUPPORTED_ENTITIES.include?(entity_name) fetch(permalink, entity_name) end |