class Crunchbase::API

Constants

ORDER_CREATED_AT_ASC
ORDER_CREATED_AT_DESC
ORDER_UPDATED_AT_ASC
ORDER_UPDATED_AT_DESC
RESOURCE_LIST
RESOURCE_NAME

Must be overridden in subclasses

SUPPORTED_ENTITIES

Attributes

debug[RW]
key[RW]
redirect_limit[RW]
timeout[RW]

Public Class Methods

api_url() click to toggle source
# File lib/crunchbase/api.rb, line 56
def api_url
  [API_BASE_URL, '/', API_VERSION, '/'].join
end
collect_parameters(options) click to toggle source
# File lib/crunchbase/api.rb, line 106
def collect_parameters(options)
  options.map { |k, v| "#{k}=#{v}" }.join('&')
end
debugging(uri) click to toggle source
# File lib/crunchbase/api.rb, line 225
def debugging(uri)
  return unless debug

  puts '*' * 140
  puts "***  #{uri}  ***"
  puts '*' * 140
end
fetch(permalink, kclass_name) click to toggle source

Fetches URI for the permalink interface.

# File lib/crunchbase/api.rb, line 74
def fetch(permalink, kclass_name)
  get_json_response(api_url + "#{kclass_name}/#{permalink}")
end
funding_rounds_lists(permalink, category, options) click to toggle source
# File lib/crunchbase/api.rb, line 118
def funding_rounds_lists(permalink, category, options)
  lists_for_category('funding-rounds', permalink, category, options)
end
get_json_response(uri) click to toggle source

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.
# File lib/crunchbase/api.rb, line 135
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) do
    get_url_following_redirects(uri, @redirect_limit)
  end

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

  response['data']
end
get_url_following_redirects(uri_str, limit = 10) click to toggle source

Performs actual HTTP requests, recursively if a redirect response is encountered. Will raise HTTP error if response is not 200, 404, or 3xx.

# File lib/crunchbase/api.rb, line 153
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, 'User-Agent' => 'crunchbase-ruby-library')
  end

  case response
  when Net::HTTPSuccess, Net::HTTPNotFound, Net::HTTPInternalServerError, Net::HTTPConflict
    response.body
  when Net::HTTPRedirection
    get_url_following_redirects(response['location'], limit - 1)
  else
    response.error!
  end
end
list(options, resource_list) click to toggle source

Fetches URI for the search interface.

# File lib/crunchbase/api.rb, line 97
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) click to toggle source
# File lib/crunchbase/api.rb, line 122
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) click to toggle source
# File lib/crunchbase/api.rb, line 110
def organization_lists(permalink, category, options)
  lists_for_category('organizations', permalink, category, options)
end
parser() click to toggle source

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

# File lib/crunchbase/api.rb, line 67
def parser
  return Yajl::Parser if defined?(Yajl)

  JSON
end
person_lists(permalink, category, options) click to toggle source
# File lib/crunchbase/api.rb, line 114
def person_lists(permalink, category, options)
  lists_for_category('people', permalink, category, options)
end
post_json_response(uri, request_body) click to toggle source

Gets specified URI, and the object for request's body, then parses the returned JSON. Raises Timeout error

if request time exceeds set limit. Raises Exception if returned
JSON contains an error.
# File lib/crunchbase/api.rb, line 179
def post_json_response(uri, request_body)
  raise Exception, 'User key required, visit https://data.crunchbase.com/v3.1/docs' unless @key

  body_string = request_body.to_json

  resp = Timeout.timeout(@timeout) do
    post_url_following_redirects(uri, body_string, @redirect_limit)
  end

  response = parser.parse(resp)
  raise Exception, response['message'] unless response['message'].nil?

  response['data']
end
post_url_following_redirects(uri_str, body_string, limit = 10) click to toggle source

Performs actual HTTP requests, recursively if a redirect response is encountered. Will raise HTTP error if response is not 200, 404, or 3xx.

# File lib/crunchbase/api.rb, line 196
def post_url_following_redirects(uri_str, body_string, limit = 10)
  raise Exception, 'HTTP redirect too deep' if limit.zero?

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

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true if uri.scheme == 'https'

  req = Net::HTTP::Post.new(uri.request_uri)
  req.add_field('User-Agent', 'crunchbase-ruby-library')
  req.add_field('Content-Type', 'application/json')
  req.add_field('X-Cb-User-Key', @key)

  req.body = body_string

  response = http.start do |h|
    h.request req
  end

  case response
  when Net::HTTPSuccess, Net::HTTPNotFound, Net::HTTPInternalServerError, Net::HTTPConflict
    response.body
  when Net::HTTPRedirection
    post_url_following_redirects(response['location'], limit - 1)
  else
    response.error!
  end
end
single_entity(permalink, entity_name) click to toggle source
# File lib/crunchbase/api.rb, line 60
def single_entity(permalink, entity_name)
  raise CrunchException, 'Unsupported Entity Type' unless SUPPORTED_ENTITIES.keys.include?(entity_name)

  fetch(permalink, entity_name)
end