crunchbase4 0.1.0 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop_todo.yml +11 -1
  3. data/CHANGELOG.md +66 -0
  4. data/Gemfile.lock +4 -1
  5. data/README.md +572 -62
  6. data/crunchbase4.gemspec +7 -6
  7. data/lib/crunchbase.rb +30 -3
  8. data/lib/crunchbase/autocompletes/client.rb +75 -0
  9. data/lib/crunchbase/client.rb +9 -40
  10. data/lib/crunchbase/config.rb +1 -1
  11. data/lib/crunchbase/deleted_entities/client.rb +69 -0
  12. data/lib/crunchbase/entities/client.rb +35 -21
  13. data/lib/crunchbase/models.rb +11 -1
  14. data/lib/crunchbase/models/acquisition.rb +61 -0
  15. data/lib/crunchbase/models/address.rb +36 -0
  16. data/lib/crunchbase/models/autocomplete_entity.rb +25 -0
  17. data/lib/crunchbase/models/concerns/entity.rb +48 -0
  18. data/lib/crunchbase/models/concerns/mappings.rb +37 -0
  19. data/lib/crunchbase/models/deleted_entity.rb +26 -0
  20. data/lib/crunchbase/models/event_appearance.rb +39 -0
  21. data/lib/crunchbase/models/fund.rb +43 -0
  22. data/lib/crunchbase/models/funding_round.rb +1 -1
  23. data/lib/crunchbase/models/ipo.rb +48 -0
  24. data/lib/crunchbase/models/job.rb +42 -0
  25. data/lib/crunchbase/models/ownership.rb +39 -0
  26. data/lib/crunchbase/models/principal.rb +112 -0
  27. data/lib/crunchbase/searches/client.rb +57 -6
  28. data/lib/crunchbase/utilities/autocomplete.rb +51 -0
  29. data/lib/crunchbase/utilities/cb_model.rb +32 -0
  30. data/lib/crunchbase/utilities/deleted_entities.rb +47 -0
  31. data/lib/crunchbase/utilities/entity_endpoints.rb +125 -0
  32. data/lib/crunchbase/utilities/request.rb +47 -8
  33. data/lib/crunchbase/utilities/response.rb +33 -12
  34. data/lib/crunchbase/utilities/search_endpoints.rb +46 -0
  35. data/lib/crunchbase/utilities/search_query_parameters.rb +64 -0
  36. data/lib/crunchbase/utilities/veriables.rb +335 -0
  37. data/lib/crunchbase/version.rb +1 -1
  38. metadata +45 -12
  39. data/lib/crunchbase/models/entity.rb +0 -28
  40. data/lib/crunchbase/searches/organization.rb +0 -52
@@ -1,29 +1,80 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative '../utilities/request'
4
+ require_relative '../utilities/cb_model'
5
+
3
6
  module Crunchbase
4
7
  # Whole Searches endpoints
5
8
  module Searches
6
9
  # Send request for entities endpoints
10
+ #
11
+ # API doc:
12
+ # https://app.swaggerhub.com/apis-docs/Crunchbase/crunchbase-enterprise_api/1.0.1#/Search/post_searches_organizations
13
+ #
14
+ # Notes
15
+ # 1. Only can filter `Searchable: Yes` fields
16
+ # 2. 'name', 'website' can not use
7
17
  class Client
8
18
  include ::Crunchbase::Utilities::Request
19
+ include ::Crunchbase::Utilities::CbModel
20
+
21
+ attr_accessor :total_count, :count, :entities, :entity_type, :conditions
9
22
 
10
23
  ROOT_LIST = 'searches'
11
24
  LIMIT = 100
12
25
 
13
- def initialize(field_id, value)
14
- @field_id = field_id
15
- @value = value
26
+ def initialize(conditions, entity_type)
27
+ @conditions = conditions
28
+ @entity_type = entity_type
16
29
  end
17
30
 
18
31
  # Will include all attribute from API document
19
- def search
20
- search(root_uri, query_conditions)
32
+ def searches
33
+ wrapping!(
34
+ search(
35
+ root_uri,
36
+ data_raw.to_s.gsub('=>', ':')
37
+ )
38
+ )
21
39
  end
22
40
 
23
41
  private
24
42
 
43
+ def wrapping!(response)
44
+ query_results = search_results(response.dig('entities'))
45
+
46
+ self.total_count = response['count']
47
+ self.entities = query_results
48
+ self.count = query_results.size
49
+ self
50
+ end
51
+
52
+ def search_results(entities)
53
+ entities.each_with_object([]) do |entity, objects|
54
+ objects << cbobject.parse_response(entity, field_ids)
55
+ end
56
+ end
57
+
25
58
  def root_uri
26
- [ROOT_LIST, endpoint].compact.join('/')
59
+ [ROOT_LIST, kclass_name::RESOURCE_LIST].compact.join('/')
60
+ end
61
+
62
+ # Post request raw datas
63
+ def field_ids
64
+ @conditions['field_ids'] || cbobject.basis_fields
65
+ end
66
+
67
+ def limit
68
+ @conditions['limit'] || LIMIT
69
+ end
70
+
71
+ def data_raw
72
+ {
73
+ 'field_ids' => field_ids,
74
+ 'order' => @conditions['order'],
75
+ 'query' => @conditions['query'],
76
+ 'limit' => limit
77
+ }
27
78
  end
28
79
  end
29
80
  end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../autocompletes/client'
4
+
5
+ module Crunchbase
6
+ # Utilities
7
+ module Utilities
8
+ # Autocomplete on Searches
9
+ module Autocomplete
10
+ # endpoint: /autocompletes
11
+ #
12
+ # Suggests matching Identifier entities based on the query and entity_def_ids provided.
13
+ #
14
+ # query * string
15
+ # Value to perform the autocomplete search with.
16
+ # collection_ids string
17
+ # A comma separated list of collection ids to search against.
18
+ # Leaving this blank means it will search across all identifiers.
19
+ # Entity defs can be constrained to specific facets by providing them as facet collections.
20
+ # Relationship collections will resolve to their underlying entity def.
21
+ #
22
+ # Collection ids are:
23
+ # organizations, people, funding_rounds, acquisitions, investments, events,
24
+ # press_references, funds, event_appearances, ipos, ownerships, categories,
25
+ # category_groups, locations, jobs
26
+ # limit integer
27
+ # Number of results to retrieve; default = 10, max = 25
28
+ #
29
+ #
30
+ # Example for organizations
31
+ #
32
+ # raw_data = {
33
+ # query: keyword,
34
+ # collection_ids: 'organizations'
35
+ # }
36
+ def autocomplete(keyword, **args)
37
+ crunchbase_autocompletes(wrapper_autocompletes_data(keyword, args))
38
+ end
39
+
40
+ private
41
+
42
+ def crunchbase_autocompletes(raw_data)
43
+ Crunchbase::Autocompletes::Client.new(raw_data).autocompletes
44
+ end
45
+
46
+ def wrapper_autocompletes_data(keyword, **args)
47
+ { query: keyword }.merge(args)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Crunchbase
4
+ # Utilities
5
+ module Utilities
6
+ # API Request
7
+ module CbModel
8
+ attr_accessor :entity_type
9
+
10
+ module_function
11
+
12
+ def cbobject
13
+ kclass_name.new
14
+ end
15
+
16
+ def kclass_name
17
+ @kclass_name ||= exact_kclass_object
18
+ end
19
+
20
+ def exact_kclass_object
21
+ return entity_type if entity_type.is_a?(Class)
22
+
23
+ cb_type = [
24
+ 'Crunchbase',
25
+ 'Models',
26
+ entity_type.split('_').map(&:capitalize).join
27
+ ].join('::')
28
+ Kernel.const_get("::#{cb_type}")
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../deleted_entities/client'
4
+
5
+ module Crunchbase
6
+ # Utilities
7
+ module Utilities
8
+ # Autocomplete on Searches
9
+ module DeletedEntities
10
+ # endpoint: /deleted_entities
11
+ #
12
+ # Retrieve deleted entities
13
+ # Retrieve deleted entities for a collection id
14
+ #
15
+ # API doc:
16
+ # https://app.swaggerhub.com/apis-docs/Crunchbase/crunchbase-enterprise_api/1.0.1#/Deleted%20Entities/get_deleted_entities
17
+ #
18
+ # Parameters:
19
+ # collection_ids: string
20
+ # Filter by collection id(s). Comma separated list of collection ids.
21
+ # E.g.
22
+ # organizations, people, funding_rounds, acquisitions, investments,
23
+ # events, press_references, funds, event_appearances, ipos, ownerships,
24
+ # categories, category_groups, locations, jobs
25
+ # before_id: string
26
+ # Used to paginate search results to the previous page. before_id should be the uuid of the first item in the current page.
27
+ # May not be provided simultaneously with after_id.
28
+ # after_id: string
29
+ # Used to paginate search results to the next page. after_id should be the uuid of the last item in the current page.
30
+ # May not be provided simultaneously with before_id.
31
+ # limit: integer
32
+ # Number of rows to return. Default is 100, min is 1, max is 1000.
33
+ # deleted_at_order: string
34
+ # Direction of sorting by deleted_at property
35
+ # Available values : asc, desc
36
+ def deleted_entities(**args)
37
+ crunchbase_deleted_entities(args)
38
+ end
39
+
40
+ private
41
+
42
+ def crunchbase_deleted_entities(args)
43
+ Crunchbase::DeletedEntities::Client.new(args).deleted_entities
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,125 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../errors'
4
+
5
+ module Crunchbase
6
+ # Utilities
7
+ module Utilities
8
+ # All Entity API endpoints
9
+ module EntityEndpoints
10
+ # For Entity
11
+ #
12
+ # API Parameters
13
+ #
14
+ # entity_id: UUID or permalink of desired entity
15
+ # card_id: A card to include on the resulting entity
16
+ # order: Field name with order direction (asc/desc)
17
+ # limit: Number of rows to return. Default is 100, min is 1, max is 100.
18
+
19
+ # Lookup an Organization or single card
20
+ def organization(entity_id, **card_args)
21
+ lookup_for('organization', entity_id, card_args)
22
+ end
23
+
24
+ # Lookup Organization's all cards
25
+ def organization_cards(entity_id, cards: [])
26
+ entities('organization', entity_id).fetch_cards(cards)
27
+ end
28
+
29
+ # Lookup a Person or single card
30
+ def person(entity_id, **card_args)
31
+ lookup_for('person', entity_id, card_args)
32
+ end
33
+
34
+ # Lookup Person's all cards
35
+ def person_cards(entity_id, cards: [])
36
+ entities('person', entity_id).fetch_cards(cards)
37
+ end
38
+
39
+ # Lookup a Funding Round or single card
40
+ def funding_round(entity_id, **card_args)
41
+ lookup_for('funding_round', entity_id, card_args)
42
+ end
43
+
44
+ # Lookup Funding Round's all cards
45
+ def funding_round_cards(entity_id, cards: [])
46
+ entities('funding_round', entity_id).fetch_cards(cards)
47
+ end
48
+
49
+ # Lookup an Acquisition or Single card
50
+ def acquisition(entity_id, **card_args)
51
+ lookup_for('acquisition', entity_id, card_args)
52
+ end
53
+
54
+ # Lookup Acquisition's all card
55
+ def acquisition_cards(entity_id, cards: [])
56
+ entities('acquisition', entity_id).fetch_cards(cards)
57
+ end
58
+
59
+ # Lookup an Investment or Single card
60
+ def investment(entity_id, **card_args)
61
+ lookup_for('investment', entity_id, card_args)
62
+ end
63
+
64
+ # Lookup Investment's all card
65
+ def investment_cards(entity_id, cards: [])
66
+ entities('investment', entity_id).fetch_cards(cards)
67
+ end
68
+
69
+ # Lookup an PressReference or Single card
70
+ def press_reference(entity_id, **card_args)
71
+ lookup_for('press_reference', entity_id, card_args)
72
+ end
73
+
74
+ # Lookup PressReference's all card
75
+ def press_reference_cards(entity_id, cards: [])
76
+ entities('press_reference', entity_id).fetch_cards(cards)
77
+ end
78
+
79
+ # Lookup an Ipo or Single card
80
+ def ipo(entity_id, **card_args)
81
+ lookup_for('ipo', entity_id, card_args)
82
+ end
83
+
84
+ # Lookup Ipo's all card
85
+ def ipo_cards(entity_id, cards: [])
86
+ entities('ipo', entity_id).fetch_cards(cards)
87
+ end
88
+
89
+ # Lookup an fund or Single card
90
+ def fund(entity_id, **card_args)
91
+ lookup_for('fund', entity_id, card_args)
92
+ end
93
+
94
+ # Lookup fund's all card
95
+ def fund_cards(entity_id, cards: [])
96
+ entities('fund', entity_id).fetch_cards(cards)
97
+ end
98
+
99
+ # Lookup an fund or Single card
100
+ def ownership(entity_id, **card_args)
101
+ lookup_for('ownership', entity_id, card_args)
102
+ end
103
+
104
+ # Lookup fund's all card
105
+ def ownership_cards(entity_id, cards: [])
106
+ entities('ownership', entity_id).fetch_cards(cards)
107
+ end
108
+
109
+ private
110
+
111
+ def entities(entity_type, entity_id)
112
+ Crunchbase::Entities::Client.new(entity_id, entity_type)
113
+ end
114
+
115
+ def lookup_for(entity_type, entity_id, **card_args)
116
+ kobject = entities(entity_type, entity_id)
117
+
118
+ card_id = card_args&.delete(:card_id)
119
+ return kobject.fetch if card_id.nil?
120
+
121
+ kobject.cards(card_id, card_args)
122
+ end
123
+ end
124
+ end
125
+ end
@@ -2,37 +2,76 @@
2
2
 
3
3
  require 'faraday'
4
4
  require 'faraday_middleware'
5
+ require 'faraday_curl'
6
+ require 'logger'
7
+ require_relative '../errors'
5
8
 
6
9
  module Crunchbase
7
10
  # Utilities
8
11
  module Utilities
9
- # API Request
12
+ # Key Reminder
13
+ #
14
+ # entity_id must be provided in the request
15
+ # entity_id can be the uuid or the permalink of the entity
16
+ # you can pass your API key in the request's header if you do not want to pass the API key in the URL
10
17
  module Request
11
18
  module_function
12
19
 
20
+ # Autocompletes endpoint
21
+ def get(uri, *args)
22
+ fetch_request(uri, *args)
23
+ end
24
+
25
+ def deleted(uri, *args)
26
+ fetch_request(uri, *args)
27
+ end
28
+
29
+ # Entity endpoints
30
+ #
31
+ # https://app.swaggerhub.com/apis-docs/Crunchbase/crunchbase-enterprise_api/1.0.1#/Entity/get_entities_organizations__entity_id_
13
32
  def entity(uri, *args)
14
- response = Faraday.new(url: BASE_URI, headers: headers) do |faraday|
33
+ fetch_request(uri, *args)
34
+ end
35
+
36
+ # Search endpoints
37
+ #
38
+ # https://app.swaggerhub.com/apis-docs/Crunchbase/crunchbase-enterprise_api/1.0.1#/Search/post_searches_organizations
39
+ def search(uri, args)
40
+ response = Faraday.new(url: BASE_URI, headers: post_headers) do |faraday|
15
41
  faraday.adapter Faraday.default_adapter
16
42
  faraday.response :json
17
- end.get(uri, *args)
43
+ faraday.request :curl, ::Logger.new(STDOUT), :warn if debug_mode?
44
+ faraday.response :logger, ::Logger.new(STDOUT), bodies: true if debug_mode?
45
+ end.post(uri, args)
18
46
 
19
47
  return response.body if response.status == 200
20
48
 
21
- raise Error, response.reason_phrase
49
+ raise Error, response.body[0]['message']
22
50
  end
23
51
 
24
- def search(uri, args)
52
+ private
53
+
54
+ def fetch_request(uri, *args)
25
55
  response = Faraday.new(url: BASE_URI, headers: headers) do |faraday|
26
56
  faraday.adapter Faraday.default_adapter
27
57
  faraday.response :json
28
- end.post(uri, args)
58
+ faraday.response :logger, ::Logger.new(STDOUT), bodies: true if debug_mode?
59
+ end.get(uri, *args)
29
60
 
30
61
  return response.body if response.status == 200
31
62
 
32
- raise Error, response.reason_phrase
63
+ raise Error, response.status == 400 ? response.body[0]['message'] : response.body['error']
33
64
  end
34
65
 
35
- private
66
+ def debug_mode?
67
+ Crunchbase.config.debug || false
68
+ end
69
+
70
+ def post_headers
71
+ headers.merge(
72
+ 'Content-Type' => 'application/json'
73
+ )
74
+ end
36
75
 
37
76
  def headers
38
77
  {
@@ -10,18 +10,20 @@ module Crunchbase
10
10
  def dynamic_attributes(object, attribute_names, response)
11
11
  attribute_names.each do |attribute_name|
12
12
  attribute_value = field_value(attribute_name, response)
13
+ dynamic_define_method(object, attribute_name, attribute_value)
14
+ end
13
15
 
14
- # Manually creates methods for both getter and setter and then
15
- # sends a message to the new setter with the attribute_value
16
- object.class.send(:define_method, "#{attribute_name}=".to_sym) do |value|
17
- instance_variable_set('@' + attribute_name, value)
18
- end
16
+ [attribute_names & special_attributes].flatten.each do |attribute_name|
17
+ attribute_names.delete(attribute_name)
18
+ hash_datas = response&.dig(attribute_name)
19
19
 
20
- object.class.send(:define_method, attribute_name.to_sym) do
21
- instance_variable_get('@' + attribute_name.to_s)
22
- end
20
+ values = hash_datas&.map { |k, v| v if %w[uuid permalink value].include?(k) }&.compact || []
21
+ dynamic_define_method(object, attribute_name, values)
22
+ hash_datas&.keys&.each do |key|
23
+ next unless %w[uuid permalink].include?(key)
23
24
 
24
- object.send("#{attribute_name}=".to_sym, attribute_value)
25
+ dynamic_define_method(object, key, hash_datas&.dig(key))
26
+ end
25
27
  end
26
28
 
27
29
  object
@@ -29,13 +31,32 @@ module Crunchbase
29
31
 
30
32
  private
31
33
 
34
+ def dynamic_define_method(object, attribute_name, attribute_value)
35
+ # Manually creates methods for both getter and setter and then
36
+ # sends a message to the new setter with the attribute_value
37
+ object.class.send(:define_method, "#{attribute_name}=".to_sym) do |value|
38
+ instance_variable_set('@' + attribute_name, value)
39
+ end
40
+
41
+ object.class.send(:define_method, attribute_name.to_sym) do
42
+ instance_variable_get('@' + attribute_name.to_s)
43
+ end
44
+
45
+ object.send("#{attribute_name}=".to_sym, attribute_value)
46
+ end
47
+
48
+ # This is hash attributes
49
+ #
50
+ # 1. identifier
51
+ def special_attributes
52
+ %w[identifier]
53
+ end
54
+
32
55
  def field_value(name, data)
33
56
  value = data.dig(name)
34
57
 
35
58
  return value if value.nil? || value.is_a?(String)
36
- if value.is_a?(Array) && value[0].is_a?(Hash) && value[0].keys.include?('value')
37
- return value.collect { |e| e.dig('value') }
38
- end
59
+ return value.collect { |e| e.dig('value') } if value.is_a?(Array) && value[0].is_a?(Hash) && value[0].keys.include?('value')
39
60
  return value.dig('value') if value.is_a?(Hash) && value.keys.include?('value')
40
61
 
41
62
  value