Class: Crunchbase::API

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

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(options)
  require "cgi"

  options.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

Visit: version/funding-rounds/#permalink/#category?user_key=key">https://api.crunchbase.com/v/#version/funding-rounds/#permalink/#category?user_key=key



102
103
104
# File 'lib/crunchbase/api.rb', line 102

def self.funding_rounds_lists(permalink, category, options)
  lists_for_category('funding-rounds', permalink, category, options)
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(options, resource_list)
  options[:page]  = 1 if options[:page].nil?
  model_name      = options.delete(:model_name)
  
  uri = api_url + "#{resource_list}?" + collect_parameters(options)

  Crunchbase::Model::Search.new options, 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, options)
  options[:page]  = 1 if options[:page].nil?
  options[:order] = ORDER_CREATED_AT_ASC if options[:order].nil?
  model_name      = options.delete(:model_name)

  uri = api_url + "#{classify_name}/#{permalink}/#{category}?#{collect_parameters(options)}"

  Crunchbase::Model::Search.new options, 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, options)
  lists_for_category('organizations', permalink, category, options)
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

Visit: version/people/#permalink/#category?user_key=key">https://api.crunchbase.com/v/#version/people/#permalink/#category?user_key=key



97
98
99
# File 'lib/crunchbase/api.rb', line 97

def self.person_lists(permalink, category, options)
  lists_for_category('people', permalink, category, options)
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(options, resource_list)
  options[:page] = 1 if options[:page].nil?
  options[:order] = ORDER_CREATED_AT_ASC if options[:order].nil?
  
  uri = api_url + "#{resource_list}?" + collect_parameters(options)

  get_json_response(uri)
end

.single_entity(permalink, entity_name) ⇒ Object

Raises:

  • (CrunchException)


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