crunchbase-ruby-library 0.3.1 → 0.3.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 59dae45ccc627c105545d3862e2978249476c664
4
- data.tar.gz: 201f6b5b6e96b6ceb7c43f860f1479dc5bd39ba7
3
+ metadata.gz: 2b032af32aa76dd5da6cf9cdb2463bbee3cf0257
4
+ data.tar.gz: 74e32566892917c53c95aa26730a31c8791bbebf
5
5
  SHA512:
6
- metadata.gz: 9fa95c9721d6a6b73f2d952992198897b59da4d2f7e68f8bfa8b081b25fb8796a889015a87cceebeaaa175ec2ca7fb49f23a45be778f5e87239d6614325e5550
7
- data.tar.gz: e20fa1269b918d8fe6466a62c7539c9b81a919007ab60490c305277723252cfebdbe946a4a9e6223d51ea8cb063f8fc50be1cab54ad4a2cef5d89a6b8756d37c
6
+ metadata.gz: b210892b735f616b700fa57288d7d19d6c214e4f80c06de2acfec8b81b20ac328cbd3b75feed54eada18c5d7743aa1dd3e0c424faa0f137f163724738b8642c2
7
+ data.tar.gz: 8a3ad473ed48313fa8805377b766280f1ab67bed78eb2a4cedb77470d5040eb625c3860bcb505e9c1c674d75a13fff16ddd4ca85501328247d0f969190c07d6c
data/README.md CHANGED
@@ -42,7 +42,7 @@ cCeate the file `config/initializers/crunchbase.rb` in your rails project and ad
42
42
  - Acquisitions
43
43
  - Funding Rounds
44
44
 
45
- #### Searching...
45
+ #### Searching by...
46
46
 
47
47
  * client.search({query: "Google"}, 'organizations') # Full text search of an Organization's name, aliases
48
48
  * client.search({name: "Google"}, 'organizations') # Full text search limited to name and aliases
@@ -62,34 +62,58 @@ cCeate the file `config/initializers/crunchbase.rb` in your rails project and ad
62
62
  * pages
63
63
  * current_page
64
64
 
65
- ### Get Organization && RelationShips by the permalink
65
+ ### Get Organization by the permalink
66
66
 
67
- response = client.get('Organization', 'facebook')
67
+ => response = client.get('facebook', 'Organization')
68
68
 
69
69
  Relationship objects [ primary_image founders current_team investors owned_by sub_organizations headquarters offices products categories customers competitors members memberships funding_rounds investments acquisitions acquired_by ipo funds websites images videos news ]
70
70
 
71
- # Methods - Get Organization with one relationship data
71
+ methods: Get Organization with one relationship data
72
72
 
73
- 1. response = client.get('Organization', 'facebook', 'PastTeam')
73
+ => response = client.get('facebook', 'Organization', 'PastTeam')
74
74
  past_team.results.collect { |p| [p.title, p.person.first_name] }
75
75
 
76
76
  ....
77
77
 
78
78
  ### Get Person by the permalink
79
79
 
80
- person = client.get('Person', permalink)
81
-
82
- #<Crunchbase::Model::Person:0x007fc185215f68 @type_name="Person", @uuid="a578dcf9859ec8b52182e3aa3c383b13", ...>
83
-
84
- people = client.list('Person', page)
85
-
86
- people.results
87
-
88
- [ #<Crunchbase::Model::PersonSummary:...>,
89
- #<Crunchbase::Model::PersonSummary: ...>,
90
- #<Crunchbase::Model::PersonSummary: ...>,
91
- #<Crunchbase::Model::PersonSummary: ...>
92
- ...... ]
80
+ => person = client.get(permalink, 'Person')
81
+ => #<Crunchbase::Model::Person:0x007fc185215f68 @type_name="Person", @uuid="a578dcf9859ec8b52182e3aa3c383b13", ...>
82
+
83
+ => people = client.list('Person', page: page)
84
+ => people.results
85
+ => [
86
+ #<Crunchbase::Model::PersonSummary:...>,
87
+ #<Crunchbase::Model::PersonSummary: ...>,
88
+ #<Crunchbase::Model::PersonSummary: ...>,
89
+ #<Crunchbase::Model::PersonSummary: ...>
90
+ ......
91
+ ]
92
+
93
+ ### Batch search
94
+ Pass in an array of requests to perform a batch search. Following parameters should be present in each request: `type`, `uuid` and `relationships` (https://data.crunchbase.com/docs/using-the-api#batch-search-capabilities).<br/>
95
+ Max 10 requests per search allowed.
96
+
97
+ => requests = [
98
+ { type: 'Organization', uuid: 'valid_uuid', relationships: ["<relationship_name>"] },
99
+ { type: 'Person', uuid: 'valid_uuid', relationships: ["<relationship_name>"] },
100
+ { type: 'Organization', uuid: 'invalid_uuid', relationships: [] },
101
+ ]
102
+ => batch_search = client.batch_search(requests)
103
+ => batch_search = #<Crunchbase::Model::BatchSearch:0x007f9bd7e850d8 @results=[#<Crunchbase::Model::Organization:0x007f9bd7e84f98, ...>]>
104
+ => batch_search.results
105
+ => [
106
+ #<Crunchbase::Model::Organization:0x007f9bd7e84f98, ...>,
107
+ #<Crunchbase::Model::Person:0x007f9bda0d9d78, ...>,
108
+ #<Crunchbase::Model::Error:0x007fd6b6818758 @code="LA404", @message="Not Found - Entity invalid_uuid doesn't exist" ...> // Error object returned due to invalid uuid passed
109
+ ]
110
+
111
+
112
+ ### How to debug in the console
113
+
114
+ $ ruby bin/console OR ./bin/console
115
+ => client = Crunchbase::Client.new
116
+ => ...
93
117
 
94
118
  ### Contributing
95
119
 
@@ -101,4 +125,4 @@ cCeate the file `config/initializers/crunchbase.rb` in your rails project and ad
101
125
 
102
126
  ### Copyright
103
127
 
104
- Copyright © 2015-05 Encore Shao. See LICENSE for details.
128
+ Copyright © 2017-10 Encore Shao. See LICENSE for details.
@@ -16,7 +16,9 @@ module Crunchbase
16
16
  SUPPORTED_ENTITIES = {
17
17
  'categories' => Model::Category,
18
18
  'organizations' => Model::OrganizationSummary,
19
+ 'organization' => Model::Organization,
19
20
  'people' => Model::PersonSummary,
21
+ 'person' => Model::Person,
20
22
  'products' => Model::ProductSummary,
21
23
  'ipos' => Model::Ipo,
22
24
  'funding_rounds' => Model::FundingRound,
@@ -83,6 +85,14 @@ module Crunchbase
83
85
  get_json_response(uri)
84
86
  end
85
87
 
88
+ # Fetches URI for the search interface and adapts the payload.
89
+ def batch_search(requests_array)
90
+ uri = "#{api_url}batch"
91
+ request_body = { requests: requests_array }
92
+
93
+ post_json_response(uri, request_body)
94
+ end
95
+
86
96
  # Fetches URI for the search interface.
87
97
  def list(options, resource_list)
88
98
  options[:page] = 1 if options[:page].nil?
@@ -151,7 +161,7 @@ module Crunchbase
151
161
  http = Net::HTTP.new(uri.host, uri.port)
152
162
  http.use_ssl = true if uri.scheme == 'https'
153
163
  response = http.start do |h|
154
- h.request Net::HTTP::Get.new(uri.request_uri)
164
+ h.request Net::HTTP::Get.new(uri.request_uri, {'User-Agent' => 'crunchbase-ruby-library'})
155
165
  end
156
166
 
157
167
  case response
@@ -164,6 +174,55 @@ module Crunchbase
164
174
  end
165
175
  end
166
176
 
177
+ # Gets specified URI, and the object for request's body, then parses the returned JSON. Raises Timeout error
178
+ # if request time exceeds set limit. Raises Exception if returned
179
+ # JSON contains an error.
180
+ def post_json_response(uri, request_body)
181
+ raise Exception, 'User key required, visit https://data.crunchbase.com/v3.1/docs' unless @key
182
+
183
+ body_string = request_body.to_json
184
+
185
+ resp = Timeout.timeout(@timeout_limit) do
186
+ post_url_following_redirects(uri, body_string, @redirect_limit)
187
+ end
188
+
189
+ response = parser.parse(resp)
190
+ raise Exception, message: response['message'], status: response['status'] unless response['message'].nil?
191
+
192
+ response['data']
193
+ end
194
+
195
+ # Performs actual HTTP requests, recursively if a redirect response is
196
+ # encountered. Will raise HTTP error if response is not 200, 404, or 3xx.
197
+ def post_url_following_redirects(uri_str, body_string, limit = 10)
198
+ raise Exception, 'HTTP redirect too deep' if limit.zero?
199
+
200
+ uri = URI.parse(URI.encode(uri_str))
201
+
202
+ http = Net::HTTP.new(uri.host, uri.port)
203
+ http.use_ssl = true if uri.scheme == 'https'
204
+
205
+ req = Net::HTTP::Post.new(uri.request_uri)
206
+ req.add_field('User-Agent', 'crunchbase-ruby-library')
207
+ req.add_field('Content-Type', 'application/json')
208
+ req.add_field('X-Cb-User-Key', @key)
209
+
210
+ req.body = body_string
211
+
212
+ response = http.start do |h|
213
+ h.request req
214
+ end
215
+
216
+ case response
217
+ when Net::HTTPSuccess, Net::HTTPNotFound, Net::HTTPInternalServerError
218
+ response.body
219
+ when Net::HTTPRedirection
220
+ post_url_following_redirects(response['location'], limit - 1)
221
+ else
222
+ response.error!
223
+ end
224
+ end
225
+
167
226
  def debugging(uri)
168
227
  return unless debug
169
228
 
@@ -42,6 +42,15 @@ module Crunchbase
42
42
  kclass(kclass_name).organization_lists(permalink)
43
43
  end
44
44
 
45
+ # Runs a batch search and returns matched results
46
+ # Params:
47
+ # +requests+:: An array of hashes with following attributes: { 'type': String, 'uuid': String, 'relationships': String[] }
48
+ def batch_search(requests)
49
+ return [] if requests.nil?
50
+
51
+ kclass('BatchSearch').batch_search(requests)
52
+ end
53
+
45
54
  private
46
55
  def kclass(kclass_name)
47
56
  Crunchbase::Model.const_get kclass_name
@@ -9,6 +9,7 @@ module Crunchbase
9
9
  autoload :Acquisition, 'crunchbase/model/acquisition'
10
10
  autoload :Address, 'crunchbase/model/address'
11
11
  autoload :AdvisoryRole, 'crunchbase/model/advisory_role'
12
+ autoload :BatchSearch, 'crunchbase/model/batch_search'
12
13
  autoload :BoardMembersAndAdvisor, 'crunchbase/model/board_members_and_advisor'
13
14
  autoload :Category, 'crunchbase/model/category'
14
15
  autoload :Competitor, 'crunchbase/model/competitor'
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Crunchbase
5
+ module Model
6
+ class BatchSearch < Entity
7
+ include Enumerable
8
+
9
+ attr_reader :results
10
+
11
+ alias items results
12
+
13
+ def initialize(json)
14
+ @results = []
15
+
16
+ populate_results(json)
17
+ end
18
+
19
+ def populate_results(json)
20
+ @results = []
21
+
22
+ @results = json['items'].map do |r|
23
+ next Error.new(r['error']) if r.key?('error')
24
+
25
+ kclass = kclass_name(r['type'])
26
+ kclass.new(r)
27
+ end
28
+ end
29
+
30
+ def self.batch_search(requests)
31
+ raise ConfigurationException, 'Invalid argument. Please pass in an array as an argument' unless requests.is_a?(Array)
32
+ raise InvalidRequestException, "Too many requests(#{requests.length}) in batch, max allowed 10" if requests.length > 10
33
+ raise MissingParamsException, 'Missing :type or :uuid parameter in some requests' if requests.any? { |request| !request.key?(:type) || !request.key?(:uuid) }
34
+ return [] if requests.empty?
35
+
36
+ BatchSearch.new API.batch_search(requests)
37
+ end
38
+
39
+ private
40
+
41
+ def kclass_name(model_name)
42
+ self.class.kclass_name(model_name.downcase)
43
+ end
44
+
45
+ end
46
+ end
47
+ end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Crunchbase
3
- VERSION = '0.3.1'
3
+ VERSION = '0.3.2'
4
4
  end
@@ -21,5 +21,15 @@ module Crunchbase
21
21
  expect(facebook_data1.permalink).to eq('facebook')
22
22
  end
23
23
  end
24
+
25
+ context '#get batched data' do
26
+ let(:response) { client.batch_search([{ type: 'Organization', uuid: 'df6628127f970b439d3e12f64f504fbb' }]) }
27
+
28
+ it 'should return batch search data' do
29
+ expect(response).to be_an_instance_of(Crunchbase::Model::BatchSearch)
30
+ expect(response.results).to be_an_instance_of(Array)
31
+ expect(response.results).to be_present
32
+ end
33
+ end
24
34
  end
25
35
  end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Crunchbase::Model
4
+ RSpec.describe BatchSearch do
5
+
6
+ context 'perform invalid batch_search' do
7
+ it 'should raise ConfigurationException for non-array argument' do
8
+ expect { BatchSearch.batch_search({}) }.to raise_error Crunchbase::ConfigurationException
9
+ end
10
+
11
+ it 'should raise InvalidRequestException for too many requests' do
12
+ expect { BatchSearch.batch_search(Array.new(11)) }.to raise_error Crunchbase::InvalidRequestException
13
+ end
14
+
15
+ it 'should raise MissingParamsException for invalid array item (without type and uuid fields)' do
16
+ invalid_array_arguments = [
17
+ [{}],
18
+ [{ type: 'Organization' }],
19
+ [{ type: 'Organization', uuid: 'unique_id' }, {}],
20
+ [{ type: 'Organization', uuid: 'unique_id' }, { uuid: 'unique_id' }]
21
+ ]
22
+
23
+ invalid_array_arguments.each do |invalid_argument|
24
+ expect { BatchSearch.batch_search(invalid_argument) }.to raise_error Crunchbase::MissingParamsException
25
+ end
26
+ end
27
+
28
+ it 'should return an empty array for empty array argument' do
29
+ results = BatchSearch.batch_search([])
30
+ expect(results).to be_empty
31
+ end
32
+
33
+ it 'should return an instance of Error for invalid uuid' do
34
+ invalid_request = [{ type: 'Organization', uuid: 'invalid_id' }]
35
+ results = BatchSearch.batch_search(invalid_request)
36
+
37
+ expect(results.results.first).to be_an_instance_of(Crunchbase::Model::Error)
38
+ end
39
+ end
40
+
41
+ context 'perform valid batch_search' do
42
+ let(:requests) {
43
+ [
44
+ { type: 'Organization', uuid: 'df6628127f970b439d3e12f64f504fbb' },
45
+ { type: 'Organization', uuid: '0ea85f0296e15c42feebb786ecab2939' },
46
+ { type: 'Person', uuid: 'a01b8d46d31133337c34aa3ae9c03f22' }
47
+ ]
48
+ }
49
+ let(:batch_search) { BatchSearch.batch_search(requests) }
50
+
51
+ it 'should return new instance of BatchSearch' do
52
+ expect(batch_search).to be_an_instance_of(BatchSearch)
53
+ end
54
+
55
+ it 'should return an array of results' do
56
+ results = batch_search.results
57
+ expect(results).to be_an_instance_of(Array)
58
+ expect(results).not_to be_empty
59
+ end
60
+
61
+ it 'should return an array of valid model objects' do
62
+ results = batch_search.results
63
+ expect(results[0]).to be_a(Crunchbase::Model::Organization)
64
+ expect(results[1]).to be_a(Crunchbase::Model::Organization)
65
+ expect(results[2]).to be_a(Crunchbase::Model::Person)
66
+ end
67
+ end
68
+ end
69
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crunchbase-ruby-library
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Encore Shao
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-13 00:00:00.000000000 Z
11
+ date: 2018-06-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -126,6 +126,7 @@ files:
126
126
  - lib/crunchbase/model/acquisition.rb
127
127
  - lib/crunchbase/model/address.rb
128
128
  - lib/crunchbase/model/advisory_role.rb
129
+ - lib/crunchbase/model/batch_search.rb
129
130
  - lib/crunchbase/model/board_members_and_advisor.rb
130
131
  - lib/crunchbase/model/category.rb
131
132
  - lib/crunchbase/model/competitor.rb
@@ -195,6 +196,7 @@ files:
195
196
  - spec/crunchbase/data/people/facebook_founders.json
196
197
  - spec/crunchbase/data/people/mark-zuckerberg.json
197
198
  - spec/crunchbase/data/websites/facebook.json
199
+ - spec/crunchbase/model/batch_search_spec.rb
198
200
  - spec/crunchbase/model/board_members_and_advisor_spec.rb
199
201
  - spec/crunchbase/model/category_spec.rb
200
202
  - spec/crunchbase/model/current_team_spec.rb
@@ -262,6 +264,7 @@ test_files:
262
264
  - spec/crunchbase/data/people/facebook_founders.json
263
265
  - spec/crunchbase/data/people/mark-zuckerberg.json
264
266
  - spec/crunchbase/data/websites/facebook.json
267
+ - spec/crunchbase/model/batch_search_spec.rb
265
268
  - spec/crunchbase/model/board_members_and_advisor_spec.rb
266
269
  - spec/crunchbase/model/category_spec.rb
267
270
  - spec/crunchbase/model/current_team_spec.rb