Class: Crunchbase::API

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

Class Method Summary collapse

Class Attribute Details

.base_urlObject

Returns the value of attribute base_url.



36
37
38
# File 'lib/crunchbase/api.rb', line 36

def base_url
  @base_url
end

.debugObject

Returns the value of attribute debug.



36
37
38
# File 'lib/crunchbase/api.rb', line 36

def debug
  @debug
end

.image_urlObject

Returns the value of attribute image_url.



36
37
38
# File 'lib/crunchbase/api.rb', line 36

def image_url
  @image_url
end

.keyObject

Returns the value of attribute key.



36
37
38
# File 'lib/crunchbase/api.rb', line 36

def key
  @key
end

.redirect_limitObject

Returns the value of attribute redirect_limit.



36
37
38
# File 'lib/crunchbase/api.rb', line 36

def redirect_limit
  @redirect_limit
end

.site_urlObject

Returns the value of attribute site_url.



36
37
38
# File 'lib/crunchbase/api.rb', line 36

def site_url
  @site_url
end

.timeout_limitObject

Returns the value of attribute timeout_limit.



36
37
38
# File 'lib/crunchbase/api.rb', line 36

def timeout_limit
  @timeout_limit
end

.versionObject

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_urlObject



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

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

Raises:



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.

Raises:



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(options, object_lists)
  options[:page]  = 1 if options[:page].nil?
  model_name      = options.delete(:model_name)
  
  uri = api_url + "#{object_lists}?" + collect_parameters(options)

  Search.new options, get_json_response(uri), SearchResult
end

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, 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 + "organization/#{permalink}/#{category}?#{collect_parameters(options)}"

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

.parserObject

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(options, object_lists)
  options[:page] = 1 if options[:page].nil?
  options[:order] = ORDER_CREATED_AT_ASC if options[:order].nil?
  
  uri = api_url + "#{object_lists}?" + collect_parameters(options)

  get_json_response(uri)
end

.single_entity(permalink, entity_name) ⇒ Object

Raises:



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