crunchbase_v2 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. data/.gitignore +25 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE.txt +22 -0
  4. data/README.rdoc +88 -0
  5. data/Rakefile +7 -0
  6. data/crunchbase.gemspec +24 -0
  7. data/lib/crunchbase.rb +36 -0
  8. data/lib/crunchbase/acquisition.rb +66 -0
  9. data/lib/crunchbase/api.rb +153 -0
  10. data/lib/crunchbase/board_members_and_advisor.rb +23 -0
  11. data/lib/crunchbase/category.rb +22 -0
  12. data/lib/crunchbase/cb_entity.rb +70 -0
  13. data/lib/crunchbase/competitor.rb +25 -0
  14. data/lib/crunchbase/crunch_exception.rb +6 -0
  15. data/lib/crunchbase/current_team.rb +26 -0
  16. data/lib/crunchbase/customer.rb +21 -0
  17. data/lib/crunchbase/founder.rb +21 -0
  18. data/lib/crunchbase/funding_round.rb +69 -0
  19. data/lib/crunchbase/headquarter.rb +27 -0
  20. data/lib/crunchbase/image.rb +21 -0
  21. data/lib/crunchbase/investment.rb +33 -0
  22. data/lib/crunchbase/ipo.rb +57 -0
  23. data/lib/crunchbase/location.rb +24 -0
  24. data/lib/crunchbase/new_item.rb +22 -0
  25. data/lib/crunchbase/office.rb +28 -0
  26. data/lib/crunchbase/organization.rb +246 -0
  27. data/lib/crunchbase/past_team.rb +29 -0
  28. data/lib/crunchbase/person.rb +31 -0
  29. data/lib/crunchbase/primary_image.rb +21 -0
  30. data/lib/crunchbase/product.rb +78 -0
  31. data/lib/crunchbase/relationship.rb +17 -0
  32. data/lib/crunchbase/search.rb +45 -0
  33. data/lib/crunchbase/search_result.rb +11 -0
  34. data/lib/crunchbase/sub_organization.rb +21 -0
  35. data/lib/crunchbase/version.rb +3 -0
  36. data/lib/crunchbase/website.rb +20 -0
  37. data/spec/apikey.example.yml +2 -0
  38. data/spec/bootstrap.rb +9 -0
  39. data/spec/crunchbase/acquisition_spec.rb +26 -0
  40. data/spec/crunchbase/api_spec.rb +8 -0
  41. data/spec/crunchbase/board_members_and_advisor_spec.rb +16 -0
  42. data/spec/crunchbase/category_spec.rb +25 -0
  43. data/spec/crunchbase/competitor_spec.rb +19 -0
  44. data/spec/crunchbase/current_team_spec.rb +17 -0
  45. data/spec/crunchbase/customer_spec.rb +20 -0
  46. data/spec/crunchbase/founder_spec.rb +20 -0
  47. data/spec/crunchbase/funding_round_spec.rb +26 -0
  48. data/spec/crunchbase/headquarter_spec.rb +17 -0
  49. data/spec/crunchbase/investment_spec.rb +20 -0
  50. data/spec/crunchbase/ipo_spec.rb +29 -0
  51. data/spec/crunchbase/location_spec.rb +16 -0
  52. data/spec/crunchbase/new_item_spec.rb +33 -0
  53. data/spec/crunchbase/office_spec.rb +19 -0
  54. data/spec/crunchbase/organization_spec.rb +62 -0
  55. data/spec/crunchbase/past_team_spec.rb +16 -0
  56. data/spec/crunchbase/person_spec.rb +23 -0
  57. data/spec/crunchbase/primary_image_spec.rb +21 -0
  58. data/spec/crunchbase/product_spec.rb +28 -0
  59. data/spec/crunchbase/search_result_spec.rb +16 -0
  60. data/spec/crunchbase/search_spec.rb +17 -0
  61. data/spec/crunchbase/sub_organization_spec.rb +21 -0
  62. data/spec/crunchbase/website_spec.rb +25 -0
  63. data/spec/fixtures/facebook-acquisition.js +82 -0
  64. data/spec/fixtures/facebook-funding-round.js +96 -0
  65. data/spec/fixtures/organization-facebook.js +1023 -0
  66. data/spec/fixtures/person-randi-zuckerberg.js +233 -0
  67. data/spec/spec_helper.rb +10 -0
  68. metadata +192 -0
data/.gitignore ADDED
@@ -0,0 +1,25 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+
24
+ *.swp
25
+ spec/apikey.yml
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in crunchbase.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Encore Shao
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,88 @@
1
+ = Crunchbase API
2
+
3
+ Ruby API wrapper for [Crunchbase API](https://developer.crunchbase.com/docs) version 2.
4
+
5
+ == Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'crunchbase', github: 'encoreshao/crunchbase_v2'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ == Usage Examples
16
+
17
+ Config your user_key, debug somewhere like development.rb.
18
+
19
+ Crunchbase::API.key = 'YOUR_API_KEY'
20
+ Crunchbase::API.debug = false
21
+
22
+ === Search
23
+
24
+ Searching the Crunchbase is possible with the Search class, The Search will return a list consisting of objects of the SearchResult type.
25
+
26
+ s = Crunchbase::Search.search("facebook")
27
+ OR
28
+ s = Crunchbase::Search.search({ domain_name: 'facebook.com' }, 'organizations')
29
+ s.total_items || s.per_page || s.pages || s.current_page
30
+ all_results = s.results
31
+ all_results.each { |r| puts r.name }
32
+
33
+ === Organizations && RelationShip List Items
34
+
35
+ Get information by the permalink, Example:
36
+
37
+ company = Crunchbase::Organization.get("facebook")
38
+ Relationship objects
39
+ [ competitors customers founders funding_rounds ipos products sub_organizations ]
40
+ Friendly relationship objects
41
+ [ past_teams current_teams acquisitions offices headquarters categories investments primary_images images websites new_items board_members_and_advisors ]
42
+ company.competitors Only return Top 8 items
43
+ company.competitors_total_items Return competitors total items count
44
+ company.competitors.each do |e|
45
+ competitor = a.fetch
46
+ puts competitor.name
47
+ end
48
+ OR
49
+ company.websites
50
+
51
+ If you want all competitors items, Please do:
52
+
53
+ all_competitors = Competitor.lists_for_permalink(company.permalink)
54
+ all_competitors.per_page
55
+ all_competitors.current_page
56
+ all_competitors.size
57
+ all_competitors.results Return all competitors items
58
+
59
+ === People
60
+
61
+ person = Crunchbase::Person.get( permalink )
62
+
63
+ #<Crunchbase::Person:0x007fc95bca3880 @type_name="Person" ..... >
64
+
65
+ people = Crunchbase::Person.list( page )
66
+
67
+ people.results
68
+
69
+ [#<Crunchbase::SearchResult:0x007fd16ae18b40 ...>,
70
+ #<Crunchbase::SearchResult:0x007fd16ae18870 ...>,
71
+ #<Crunchbase::SearchResult:0x007fd16ae185a0 ...>,
72
+ #<Crunchbase::SearchResult:0x007fd16ae182f8 ...>
73
+ ......]
74
+
75
+
76
+ === How to use Location & Category ....
77
+
78
+ Such as a Person
79
+
80
+ == Contributing
81
+
82
+ Contributions are welcome. Note that in order to run the test suite, you need to
83
+ include an API key in +spec/apikey.yml+. This file is ignored by git. An example
84
+ file is provided for convenience.
85
+
86
+ == Copyright
87
+
88
+ Copyright © 2014-08 Encore Shao. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'crunchbase/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "crunchbase_v2"
8
+ spec.version = Crunchbase::VERSION
9
+ spec.authors = ["Encore Shao"]
10
+ spec.email = ["encore@ekohe.com"]
11
+ spec.summary = %q{ Wrapper for Crunchbase API version 2 }
12
+ spec.description = %q{ Wrapper for Crunchbase API version 2 }
13
+ spec.homepage = "https://github.com/encoreshao/crunchbase_v2"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec", "~> 2.10.0"
24
+ end
data/lib/crunchbase.rb ADDED
@@ -0,0 +1,36 @@
1
+ require "crunchbase/api"
2
+ require "crunchbase/crunch_exception"
3
+ require "crunchbase/cb_entity"
4
+ require "crunchbase/location"
5
+ require "crunchbase/category"
6
+ require "crunchbase/acquisition"
7
+ require "crunchbase/competitor"
8
+ require "crunchbase/customer"
9
+ require "crunchbase/funding_round"
10
+ require "crunchbase/founder"
11
+ require "crunchbase/headquarter"
12
+ require "crunchbase/sub_organization"
13
+ require "crunchbase/image"
14
+ require "crunchbase/primary_image"
15
+ require "crunchbase/investment"
16
+ require "crunchbase/ipo"
17
+ require "crunchbase/new_item"
18
+ require "crunchbase/office"
19
+ require "crunchbase/organization"
20
+ require "crunchbase/past_team"
21
+ require "crunchbase/current_team"
22
+ require "crunchbase/person"
23
+ require "crunchbase/product"
24
+ require "crunchbase/relationship"
25
+ require "crunchbase/search"
26
+ require "crunchbase/search_result"
27
+ require "crunchbase/website"
28
+ require "crunchbase/board_members_and_advisor"
29
+ require "crunchbase/version"
30
+
31
+
32
+ module Crunchbase
33
+ class << self
34
+ # coding...
35
+ end
36
+ end
@@ -0,0 +1,66 @@
1
+ # encoding: utf-8
2
+
3
+ # http://api.crunchbase.com/v/2/organization/facebook/acquisitions?user_key=key
4
+ module Crunchbase
5
+ class Acquisition < CBEntity
6
+ RESOURCE_NAME = 'acquisition'
7
+ RESOURCE_LIST = 'acquisitions'
8
+
9
+ attr_reader :announced_on, :announced_on_trust_code, :price_currency_code,
10
+ :permalink, :name, :payment_type, :price, :disposition_of_acquired,
11
+ :acquisition_type, :acquisition_status, :price_usd, :created_at, :updated_at
12
+
13
+ attr_reader :acquirers, :acquirees, :new_items
14
+
15
+ attr_reader :acquirers_total_items, :acquirees_total_items, :new_items_total_items
16
+
17
+ def initialize(json)
18
+ @type_name = json['type']
19
+ properties = json['properties']
20
+ relationships = json['relationships']
21
+
22
+ @announced_on = properties['announced_on'] && DateTime.parse(properties['announced_on'])
23
+ @announced_on_trust_code = properties['announced_on_trust_code']
24
+ @price_currency_code = properties['price_currency_code']
25
+ @permalink = properties['permalink']
26
+ @name = properties['name']
27
+ @payment_type = properties['payment_type']
28
+ @price = properties['price']
29
+ @disposition_of_acquired = properties['disposition_of_acquired']
30
+ @acquisition_type = properties['acquisition_type']
31
+ @acquisition_status = properties['acquisition_status']
32
+ @price_usd = properties['price_usd']
33
+ @created_at = Time.at(properties['created_at']).utc
34
+ @updated_at = Time.at(properties['updated_at']).utc
35
+
36
+ @acquirers_list = relationships['acquirer']
37
+ @acquirees_list = relationships['acquiree']
38
+ @new_items_list = relationships['news']
39
+ end
40
+
41
+ def acquirers
42
+ @acquirers ||= Relationship.array_from_list(@acquirers_list)
43
+ end
44
+
45
+ def acquirees
46
+ @acquirees ||= Relationship.array_from_list(@acquirees_list)
47
+ end
48
+
49
+ def new_items
50
+ @new_items ||= NewItem.array_from_list(@new_items_list)
51
+ end
52
+
53
+ def acquirers_total_items
54
+ @acquirers_total_items ||= Relationship.total_items_from_list(@acquirers_list)
55
+ end
56
+
57
+ def acquirees_total_items
58
+ @acquirees_total_items ||= Relationship.total_items_from_list(@acquirees_list)
59
+ end
60
+
61
+ def new_items_total_items
62
+ @new_items_total_items ||= NewItem.total_items_from_list(@new_items_list)
63
+ end
64
+ end
65
+
66
+ end
@@ -0,0 +1,153 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'net/http'
3
+
4
+ begin
5
+ require 'yajl'
6
+ rescue LoadError
7
+ require 'json'
8
+ end
9
+
10
+ require 'timeout'
11
+
12
+ module Crunchbase
13
+
14
+ class API
15
+ SUPPORTED_ENTITIES = ['organizations', 'organization', 'people', 'person', 'products', 'product', 'funding_rounds', 'funding-round', 'acquisition', 'ipo', 'fund-raise', 'locations', 'categories', 'offices', 'customers']
16
+
17
+ @timeout_limit = 60
18
+ @redirect_limit = 2
19
+ @version = '2'
20
+ @base_url = 'http://api.crunchbase.com'
21
+ @site_url = "http://www.crunchbase.com"
22
+ @image_url = "http://images.crunchbase.com/"
23
+ @debug = false
24
+
25
+ # Must be overridden in subclasses
26
+ RESOURCE_NAME = "undefined"
27
+ RESOURCE_LIST = "undefineds"
28
+
29
+ ORDER_CREATED_AT_ASC = 'created_at asc'
30
+ ORDER_CREATED_AT_DESC = 'created_at desc'
31
+ ORDER_UPDATED_AT_ASC = 'updated_at asc'
32
+ ORDER_UPDATED_AT_DESC = 'updated_at desc'
33
+
34
+ class << self
35
+ attr_accessor :timeout_limit, :redirect_limit, :key, :base_url, :version,
36
+ :debug, :image_url, :site_url
37
+
38
+ def api_url
39
+ base_url.gsub(/\/$/, '') + '/v/' + version + '/'
40
+ end
41
+ end
42
+
43
+ def self.single_entity(permalink, entity_name)
44
+ raise CrunchException, "Unsupported Entity Type" unless SUPPORTED_ENTITIES.include?(entity_name)
45
+
46
+ fetch(permalink, entity_name)
47
+ end
48
+
49
+ def self.all(entity)
50
+ get_json_response( api_url + entity )
51
+ end
52
+
53
+ private
54
+
55
+ # Returns the JSON parser, whether that's an instance of Yajl or JSON
56
+ def self.parser
57
+ if defined?(Yajl)
58
+ Yajl::Parser
59
+ else
60
+ JSON
61
+ end
62
+ end
63
+
64
+ # Fetches URI for the permalink interface.
65
+ def self.fetch(permalink, object_name)
66
+ uri = api_url + "#{object_name}/#{permalink}"
67
+
68
+ get_json_response(uri)
69
+ end
70
+
71
+ # Fetches URI for the search interface.
72
+ def self.search(options, object_lists)
73
+ options[:page] = 1 if options[:page].nil?
74
+ options[:order] = ORDER_CREATED_AT_ASC if options[:order].nil?
75
+
76
+ uri = api_url + "#{object_lists}?" + collect_parameters(options)
77
+
78
+ get_json_response(uri)
79
+ end
80
+
81
+
82
+ # Fetches URI for the search interface.
83
+ def self.list(options, object_lists)
84
+ options[:page] = 1 if options[:page].nil?
85
+ model_name = options.delete(:model_name)
86
+
87
+ uri = api_url + "#{object_lists}?" + collect_parameters(options)
88
+
89
+ Search.new options, get_json_response(uri), SearchResult
90
+ end
91
+
92
+ def self.collect_parameters(options)
93
+ require "cgi"
94
+
95
+ options.map{|k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"}.join('&')
96
+ end
97
+
98
+ # Searches for a permalink in a particular category.
99
+ # Demo: http://api.crunchbase.com/v/2/organization/facebook/offices?user_key=key
100
+ def self.lists_for_permalink(permalink, category, options)
101
+ options[:page] = 1 if options[:page].nil?
102
+ options[:order] = ORDER_CREATED_AT_ASC if options[:order].nil?
103
+ model_name = options.delete(:model_name)
104
+
105
+ uri = api_url + "organization/#{permalink}/#{category}?#{collect_parameters(options)}"
106
+
107
+ Search.new options, get_json_response(uri), model_name
108
+ end
109
+
110
+ # Gets specified URI, then parses the returned JSON. Raises Timeout error
111
+ # if request time exceeds set limit. Raises CrunchException if returned
112
+ # JSON contains an error.
113
+ def self.get_json_response(uri)
114
+ raise CrunchException, "User key required, visit http://developer.crunchbase.com" unless @key
115
+ uri = uri + "#{uri.match('\?') ? "&" : "?"}user_key=#{@key}"
116
+
117
+ resp = Timeout::timeout(@timeout_limit) {
118
+ get_url_following_redirects(uri, @redirect_limit)
119
+ }
120
+ response = parser.parse(resp)["data"]
121
+
122
+ raise CrunchException, response["error"] if response.class == Hash && response["error"]
123
+
124
+ response
125
+ end
126
+
127
+ # Performs actual HTTP requests, recursively if a redirect response is
128
+ # encountered. Will raise HTTP error if response is not 200, 404, or 3xx.
129
+ def self.get_url_following_redirects(uri_str, limit = 10)
130
+ raise CrunchException, 'HTTP redirect too deep' if limit == 0
131
+
132
+ url = URI.parse(uri_str)
133
+
134
+ if self.debug
135
+ length = (uri_str.length + 10)
136
+ puts "*"*length
137
+ puts "*** #{url} ***"
138
+ puts "*"*length
139
+ end
140
+
141
+ response = Net::HTTP.start(url.host, url.port) { |http| http.get(url.request_uri) }
142
+ case response
143
+ when Net::HTTPSuccess, Net::HTTPNotFound
144
+ response.body
145
+ when Net::HTTPRedirection
146
+ get_url_following_redirects(response['location'], limit - 1)
147
+ else
148
+ response.error!
149
+ end
150
+ end
151
+
152
+ end
153
+ end
@@ -0,0 +1,23 @@
1
+ # encode: utf-8
2
+
3
+ # http://api.crunchbase.com/v/2/organization/facebook/board_members_and_advisors
4
+
5
+ module Crunchbase
6
+ class BoardMembersAndAdvisor < CBEntity
7
+ RESOURCE_LIST = 'board_members_and_advisors'
8
+
9
+ attr_reader :type_name, :first_name, :last_name, :title, :started_on, :path,
10
+ :created_at, :updated_at
11
+
12
+ def initialize(json)
13
+ @type_name = json['type']
14
+ @first_name = json['first_name']
15
+ @last_name = json['last_name']
16
+ @title = json['title']
17
+ @path = json['path']
18
+ @started_on = json['started_on'] && DateTime.parse(json['started_on'])
19
+ @created_at = Time.at(json['created_at']).utc
20
+ @updated_at = Time.at(json['updated_at']).utc
21
+ end
22
+ end
23
+ end