Class: Crunchbase::API

Inherits:
Object
  • Object
show all
Defined in:
lib/crunchbase/api.rb

Constant Summary collapse

SUPPORTED_ENTITIES =
{
  'categories' => Model::Category,
  'organizations' => Model::OrganizationSummary,
  'people' => Model::PersonSummary,
  'products' => Model::ProductSummary,
  'ipos' => Model::Ipo,
  'funding_rounds' => Model::FundingRound,
  'funding-rounds' => Model::FundingRound,
  'acquisitions' => Model::Acquisition,
  'locations' => Model::Location,
  'offices' => Model::Office,
  'customers' => Model::Customer,
  'degrees' => Model::Degree,
  # 'experience' => nil,
  'primary_affiliation' => Model::PrimaryAffiliation,
  'videos' => Model::Video,
  'founded_companies' => Model::FoundedCompany,
  'primary_location' => Model::PrimaryLocation,
  'advisor_at' => Model::AdvisoryRole,
  'investors' => Model::Organization
}.freeze
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

.debugObject

Returns the value of attribute debug.



52
53
54
# File 'lib/crunchbase/api.rb', line 52

def debug
  @debug
end

.keyObject

Returns the value of attribute key.



52
53
54
# File 'lib/crunchbase/api.rb', line 52

def key
  @key
end

.redirect_limitObject

Returns the value of attribute redirect_limit.



52
53
54
# File 'lib/crunchbase/api.rb', line 52

def redirect_limit
  @redirect_limit
end

.timeout_limitObject

Returns the value of attribute timeout_limit.



52
53
54
# File 'lib/crunchbase/api.rb', line 52

def timeout_limit
  @timeout_limit
end

Class Method Details

.api_urlObject



54
55
56
# File 'lib/crunchbase/api.rb', line 54

def api_url
  API_BASE_URL.gsub(/\/$/, '') + '/v' + API_VERSION + '/'
end

.collect_parameters(options) ⇒ Object



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

def collect_parameters(options)
  require 'cgi'

  options.map { |k, v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join('&')
end

.debugging(uri) ⇒ Object



167
168
169
170
171
172
173
# File 'lib/crunchbase/api.rb', line 167

def debugging(uri)
  return unless debug

  puts '*' * 140
  puts "***  #{uri}  ***"
  puts '*' * 140
end

.fetch(permalink, kclass_name) ⇒ Object

Fetches URI for the permalink interface.



72
73
74
# File 'lib/crunchbase/api.rb', line 72

def fetch(permalink, kclass_name)
  get_json_response(api_url + "#{kclass_name}/#{permalink}")
end

.funding_rounds_lists(permalink, category, options) ⇒ Object



110
111
112
# File 'lib/crunchbase/api.rb', line 110

def 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 Exception if returned
JSON contains an error.

Raises:



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/crunchbase/api.rb', line 127

def get_json_response(uri)
  raise Exception, 'User key required, visit https://data.crunchbase.com/v3.1/docs' unless @key
  uri += "#{uri =~ /\?/ ? '&' : '?'}user_key=#{@key}"

  resp = Timeout.timeout(@timeout_limit) do
    get_url_following_redirects(uri, @redirect_limit)
  end

  response = parser.parse(resp)
  response = response[0] if response.is_a?(Array)
  raise Exception, message: response['message'], status: response['status'] unless response['message'].nil?

  response['data']
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.

Raises:



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/crunchbase/api.rb', line 144

def get_url_following_redirects(uri_str, limit = 10)
  raise Exception, 'HTTP redirect too deep' if limit.zero?

  uri = URI.parse(URI.encode(uri_str))

  debugging(uri)

  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.



87
88
89
90
91
92
93
94
# File 'lib/crunchbase/api.rb', line 87

def list(options, resource_list)
  options[:page]  = 1 if options[:page].nil?
  model_name      = options.delete(:model_name) || SUPPORTED_ENTITIES[resource_list]

  uri = api_url + "#{resource_list}?" + collect_parameters(options)

  Model::Search.new options, get_json_response(uri), model_name
end

.lists_for_category(classify_name, permalink, category, options) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/crunchbase/api.rb', line 114

def 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)}"

  Model::Search.new options, get_json_response(uri), model_name
end

.organization_lists(permalink, category, options) ⇒ Object



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

def organization_lists(permalink, category, options)
  lists_for_category('organizations', permalink, category, options)
end

.parserObject

Returns the JSON parser, whether that’s an instance of Yajl or JSON



65
66
67
68
69
# File 'lib/crunchbase/api.rb', line 65

def parser
  return Yajl::Parser if defined?(Yajl)

  JSON
end

.person_lists(permalink, category, options) ⇒ Object



106
107
108
# File 'lib/crunchbase/api.rb', line 106

def person_lists(permalink, category, options)
  lists_for_category('people', permalink, category, options)
end

.search(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 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)


58
59
60
61
62
# File 'lib/crunchbase/api.rb', line 58

def single_entity(permalink, entity_name)
  raise CrunchException, 'Unsupported Entity Type' unless SUPPORTED_ENTITIES.keys.include?(entity_name)

  fetch(permalink, entity_name)
end