crunchbase-ruby-library 0.1.7 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +66 -0
  3. data/.rubocop_todo.yml +47 -0
  4. data/.travis.yml +12 -0
  5. data/Gemfile +4 -0
  6. data/README.md +44 -54
  7. data/Rakefile +4 -1
  8. data/bin/console +15 -0
  9. data/bin/setup +8 -0
  10. data/crunchbase-ruby-library.gemspec +15 -12
  11. data/lib/crunchbase.rb +12 -3
  12. data/lib/crunchbase/api.rb +110 -102
  13. data/lib/crunchbase/client.rb +50 -0
  14. data/lib/crunchbase/exception.rb +13 -11
  15. data/lib/crunchbase/model.rb +50 -48
  16. data/lib/crunchbase/model/acquired_by.rb +23 -20
  17. data/lib/crunchbase/model/acquiree.rb +2 -1
  18. data/lib/crunchbase/model/acquirer.rb +2 -1
  19. data/lib/crunchbase/model/acquisition.rb +16 -19
  20. data/lib/crunchbase/model/address.rb +4 -6
  21. data/lib/crunchbase/model/advisory_role.rb +2 -2
  22. data/lib/crunchbase/model/board_members_and_advisor.rb +10 -9
  23. data/lib/crunchbase/model/category.rb +6 -7
  24. data/lib/crunchbase/model/competitor.rb +3 -2
  25. data/lib/crunchbase/model/current_team.rb +7 -11
  26. data/lib/crunchbase/model/customer.rb +3 -2
  27. data/lib/crunchbase/model/degree.rb +4 -5
  28. data/lib/crunchbase/model/entity.rb +98 -108
  29. data/lib/crunchbase/model/error.rb +3 -4
  30. data/lib/crunchbase/model/featured_team.rb +19 -0
  31. data/lib/crunchbase/model/founded_company.rb +2 -3
  32. data/lib/crunchbase/model/founder.rb +3 -4
  33. data/lib/crunchbase/model/fund.rb +8 -11
  34. data/lib/crunchbase/model/fund_raise.rb +7 -8
  35. data/lib/crunchbase/model/funding_round.rb +29 -24
  36. data/lib/crunchbase/model/headquarter.rb +2 -1
  37. data/lib/crunchbase/model/image.rb +6 -7
  38. data/lib/crunchbase/model/investment.rb +20 -27
  39. data/lib/crunchbase/model/investor.rb +6 -7
  40. data/lib/crunchbase/model/ipo.rb +12 -12
  41. data/lib/crunchbase/model/job.rb +11 -10
  42. data/lib/crunchbase/model/location.rb +20 -11
  43. data/lib/crunchbase/model/member.rb +3 -4
  44. data/lib/crunchbase/model/membership.rb +7 -8
  45. data/lib/crunchbase/model/new.rb +5 -6
  46. data/lib/crunchbase/model/office.rb +3 -4
  47. data/lib/crunchbase/model/organization.rb +39 -38
  48. data/lib/crunchbase/model/organization_summary.rb +7 -8
  49. data/lib/crunchbase/model/owned_by.rb +4 -5
  50. data/lib/crunchbase/model/parent_location.rb +3 -3
  51. data/lib/crunchbase/model/past_team.rb +7 -11
  52. data/lib/crunchbase/model/person.rb +53 -46
  53. data/lib/crunchbase/model/person_summary.rb +13 -14
  54. data/lib/crunchbase/model/primary_affiliation.rb +6 -6
  55. data/lib/crunchbase/model/primary_image.rb +3 -3
  56. data/lib/crunchbase/model/primary_location.rb +8 -7
  57. data/lib/crunchbase/model/product.rb +14 -13
  58. data/lib/crunchbase/model/product_summary.rb +4 -5
  59. data/lib/crunchbase/model/school.rb +3 -2
  60. data/lib/crunchbase/model/search.rb +39 -40
  61. data/lib/crunchbase/model/search_result.rb +7 -6
  62. data/lib/crunchbase/model/simple_organization.rb +9 -10
  63. data/lib/crunchbase/model/sub_organization.rb +3 -4
  64. data/lib/crunchbase/model/video.rb +4 -5
  65. data/lib/crunchbase/model/website.rb +7 -11
  66. data/lib/crunchbase/request.rb +8 -0
  67. data/lib/crunchbase/request/client.rb +69 -0
  68. data/lib/crunchbase/version.rb +2 -1
  69. data/spec/crunchbase.yml.example +2 -2
  70. data/spec/crunchbase/client_spec.rb +25 -0
  71. data/spec/crunchbase/data/board_members_and_advisors/facebook.json +462 -0
  72. data/spec/crunchbase/data/categories/facebook.json +67 -0
  73. data/spec/crunchbase/data/current_team/facebook.json +4335 -0
  74. data/spec/crunchbase/data/featured_team/facebook.json +286 -0
  75. data/spec/crunchbase/data/funding_rounds/37bd05f961af726ba3c1b279da842805.json +484 -0
  76. data/spec/crunchbase/data/funding_rounds/facebook.json +2049 -0
  77. data/spec/crunchbase/data/headquarters/facebook.json +50 -0
  78. data/spec/crunchbase/data/investors/facebook.json +776 -0
  79. data/spec/crunchbase/data/news/facebook.json +1426 -0
  80. data/spec/crunchbase/data/news/facebook_2.json +1426 -0
  81. data/spec/crunchbase/data/offices/facebook.json +50 -0
  82. data/spec/crunchbase/data/organizations/crunchbase.json +2845 -0
  83. data/spec/crunchbase/data/organizations/ekohe.json +654 -0
  84. data/spec/crunchbase/data/organizations/facebook-without-relationships.json +45 -0
  85. data/spec/crunchbase/data/organizations/facebook.json +6723 -0
  86. data/spec/crunchbase/data/organizations/mx-media-llc.json +7 -0
  87. data/spec/crunchbase/data/past_team/facebook.json +4336 -0
  88. data/spec/crunchbase/data/people/facebook_founders.json +148 -0
  89. data/spec/crunchbase/data/people/mark-zuckerberg.json +2602 -0
  90. data/spec/crunchbase/data/websites/facebook.json +74 -0
  91. data/spec/crunchbase/model/board_members_and_advisor_spec.rb +39 -11
  92. data/spec/crunchbase/model/category_spec.rb +47 -0
  93. data/spec/crunchbase/model/current_team_spec.rb +50 -0
  94. data/spec/crunchbase/model/featured_team_spec.rb +40 -0
  95. data/spec/crunchbase/model/founders_spec.rb +49 -0
  96. data/spec/crunchbase/model/fund_raise_spec.rb +4 -24
  97. data/spec/crunchbase/model/funding_round_spec.rb +75 -32
  98. data/spec/crunchbase/model/headquarter_spec.rb +37 -0
  99. data/spec/crunchbase/model/investment_spec.rb +4 -15
  100. data/spec/crunchbase/model/investor_spec.rb +37 -0
  101. data/spec/crunchbase/model/new_spec.rb +86 -0
  102. data/spec/crunchbase/model/office_spec.rb +29 -9
  103. data/spec/crunchbase/model/organization_spec.rb +160 -34
  104. data/spec/crunchbase/model/past_team_spec.rb +36 -14
  105. data/spec/crunchbase/model/person_spec.rb +21 -19
  106. data/spec/crunchbase/model/product_spec.rb +4 -31
  107. data/spec/crunchbase/model/search_spec.rb +25 -49
  108. data/spec/crunchbase/model/website_spec.rb +36 -9
  109. data/spec/crunchbase/support/api_helper.rb +19 -0
  110. data/spec/spec_helper.rb +14 -4
  111. metadata +103 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: baf0952d3a602e2915249cf794a819630a28d02a
4
- data.tar.gz: 27d3fabb48e933aefe80e5a389a1b3ed9622b430
3
+ metadata.gz: 59dae45ccc627c105545d3862e2978249476c664
4
+ data.tar.gz: 201f6b5b6e96b6ceb7c43f860f1479dc5bd39ba7
5
5
  SHA512:
6
- metadata.gz: e5558201d8108adf4cf4d0a7cb1596b85e3e88230a21b10f399de019c1fef92a6546d5a7a8c4f2ea2774a76b38d1464857bae0aab14764b6ef71dc866080289d
7
- data.tar.gz: 8aaaed0ba98da197c134b53d96361afc139753ce59fac6a812de2c8e27f93fb7955e138a373f247541fb96cfcbb5136753280d041b6df5fb4ca100fc688d7f2a
6
+ metadata.gz: 9fa95c9721d6a6b73f2d952992198897b59da4d2f7e68f8bfa8b081b25fb8796a889015a87cceebeaaa175ec2ca7fb49f23a45be778f5e87239d6614325e5550
7
+ data.tar.gz: e20fa1269b918d8fe6466a62c7539c9b81a919007ab60490c305277723252cfebdbe946a4a9e6223d51ea8cb063f8fc50be1cab54ad4a2cef5d89a6b8756d37c
data/.rubocop.yml ADDED
@@ -0,0 +1,66 @@
1
+ # This is the configuration used to check the rubocop source code.
2
+
3
+ inherit_from: .rubocop_todo.yml
4
+
5
+ AllCops:
6
+ DisplayCopNames: true
7
+ TargetRubyVersion: 2.3
8
+ Exclude:
9
+ - '**/*.yml'
10
+ - 'tmp/**/*'
11
+
12
+ Style/StringLiterals:
13
+ Enabled: false
14
+
15
+ Documentation:
16
+ Enabled: false
17
+
18
+ Style/RegexpLiteral:
19
+ Enabled: false
20
+
21
+ Style/SpaceInsideHashLiteralBraces:
22
+ Enabled: false
23
+
24
+ Style/DotPosition:
25
+ EnforcedStyle: trailing
26
+
27
+ Style/FormatString:
28
+ Enabled: false
29
+
30
+ Lint/AssignmentInCondition:
31
+ Enabled: false
32
+
33
+ Style/EmptyLinesAroundAccessModifier:
34
+ Enabled: false
35
+
36
+ Style/SingleLineBlockParams:
37
+ Enabled: false
38
+
39
+ Style/NumericLiterals:
40
+ Exclude:
41
+ - 'spec/**/*'
42
+
43
+ Style/StringLiteralsInInterpolation:
44
+ Enabled: false
45
+
46
+ Style/CaseIndentation:
47
+ # Valid values are: case, end
48
+ IndentWhenRelativeTo: end
49
+ IndentOneStep: false
50
+
51
+ Lint/EndAlignment:
52
+ AlignWith: variable
53
+
54
+ Lint/UselessAssignment:
55
+ Enabled: false
56
+
57
+ Style/NumericLiterals:
58
+ Enabled: false
59
+
60
+ Metrics/BlockLength:
61
+ Exclude:
62
+ - "**/*_spec.rb"
63
+
64
+ Metrics/ModuleLength:
65
+ Exclude:
66
+ - "**/*_spec.rb"
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,47 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2017-08-28 16:18:43 +0800 using RuboCop version 0.45.0.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 7
10
+ Metrics/AbcSize:
11
+ Max: 53
12
+
13
+ # Offense count: 1
14
+ # Configuration parameters: CountComments.
15
+ Metrics/ClassLength:
16
+ Max: 250
17
+
18
+ # Offense count: 1
19
+ Metrics/CyclomaticComplexity:
20
+ Max: 7
21
+
22
+ # Offense count: 181
23
+ # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives.
24
+ # URISchemes: http, https
25
+ Metrics/LineLength:
26
+ Max: 267
27
+
28
+ # Offense count: 7
29
+ # Configuration parameters: CountComments.
30
+ Metrics/MethodLength:
31
+ Max: 28
32
+
33
+ # Offense count: 49
34
+ # Configuration parameters: EnforcedStyle, SupportedStyles.
35
+ # SupportedStyles: nested, compact
36
+ Style/ClassAndModuleChildren:
37
+ Enabled: false
38
+
39
+ # Offense count: 38
40
+ Style/Documentation:
41
+ Enabled: false
42
+
43
+ # Offense count: 16
44
+ # Configuration parameters: MinBodyLength.
45
+ Style/GuardClause:
46
+ Enabled: false
47
+
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ sudo: required
3
+ dist: trusty
4
+ rvm:
5
+ - 1.9.3
6
+ - 2.3.1
7
+
8
+ before_install:
9
+ - gem install bundler
10
+
11
+ after_success:
12
+ - coveralls --verbose
data/Gemfile CHANGED
@@ -1,4 +1,8 @@
1
+ # frozen_string_literal: true
1
2
  source 'https://rubygems.org'
2
3
 
4
+ gem 'coveralls', require: false
5
+ gem 'pry-coolline', require: false
6
+
3
7
  # Specify your gem's dependencies in crunchbase-ruby-library.gemspec
4
8
  gemspec
data/README.md CHANGED
@@ -1,8 +1,12 @@
1
1
  # Crunchbase
2
2
 
3
- Crunchbase API Version 3 - Ruby Library [CrunchBase Data Hub](http://data.crunchbase.com/v3).
3
+ Crunchbase API v3.1 - Ruby Library [CrunchBase Data Hub](https://data.crunchbase.com/v3.1/docs/using-the-api).
4
4
 
5
- ## Installation
5
+ [![Gem Version](https://badge.fury.io/rb/crunchbase-ruby-library.svg)](https://badge.fury.io/rb/crunchbase-ruby-library)
6
+ [![Build Status](https://travis-ci.org/encoreshao/crunchbase-ruby-library.svg?branch=master)](https://travis-ci.org/encoreshao/crunchbase-ruby-library)
7
+ [![Coverage Status](https://coveralls.io/repos/github/encoreshao/crunchbase-ruby-library/badge.svg)](https://coveralls.io/github/encoreshao/crunchbase-ruby-library)
8
+
9
+ ### How to installation
6
10
 
7
11
  Add this line to your application's Gemfile:
8
12
 
@@ -16,82 +20,68 @@ Or install it yourself as:
16
20
 
17
21
  $ gem install crunchbase-ruby-library
18
22
 
19
- ## Usage Examples
23
+ ### Certificate (User Key)
20
24
 
21
- Config your user_key, debug somewhere like development.rb, Recommended directory config/initializers/crunchbase.rb
25
+ cCeate the file `config/initializers/crunchbase.rb` in your rails project and add user_key.
22
26
 
23
27
  require 'crunchbase'
24
28
 
25
- Crunchbase::API.key = 'user_key'
29
+ Crunchbase::API.key = 'user_key'
26
30
  Crunchbase::API.debug = false
27
31
 
28
- ## Search Organization OR Person OR Product OR IPO
29
-
30
- Retrieve the way, Please use Search Class. The Search Will Return a list consisting of objects of the OrganizationSummary | PersonSummary | ProductSummary type. Example:
31
-
32
- Query Orgnization
33
-
34
- Method 1
35
- response = Crunchbase::Model::Search.search({query: "Google"}, 'organizations')
36
-
37
- Method 2
38
- response = Crunchbase::Model::Search.search({name: "Google"}, 'organizations')
39
-
40
- Method 3
41
- response = Crunchbase::Model::Search.search({domain_name: "google.com"}, 'organizations')
42
-
43
- response.total_items || response.per_page || response.pages || response.current_page
44
- response.results.each { |r| puts r.name }
45
-
32
+ ### Creating request client
46
33
 
47
- Query Person
34
+ client = Crunchbase::Client.new
48
35
 
49
- response = Crunchbase::Model::Search.search({query: "encore"}, 'people')
36
+ ### Searchable items
50
37
 
51
- response.results.each { |i| [i.first_name, i.last_name] }
38
+ - Organization
39
+ - Person
40
+ - Product
41
+ - IPO
42
+ - Acquisitions
43
+ - Funding Rounds
52
44
 
53
- Query Product
45
+ #### Searching...
54
46
 
55
- response = Crunchbase::Model::Search.search({}, 'products')
47
+ * client.search({query: "Google"}, 'organizations') # Full text search of an Organization's name, aliases
48
+ * client.search({name: "Google"}, 'organizations') # Full text search limited to name and aliases
49
+ * client.search({domain_name: "google.com"}, 'organizations') # Text search of an Organization's domain_name
50
+ * client.search({name: "encore"}, 'people') # A full-text query of name only
51
+ * client.search({query: "encore"}, 'people') # A full-text query of name, title, and company
52
+ * client.search({types: "investor"}, 'people') # Filter by type (currently, either this is empty, or is simply "investor")
53
+ * client.search({}, 'products')
54
+ * client.search({}, 'ipos')
55
+ * client.search({}, 'acquisitions')
56
+ * client.search({}, 'funding-rounds')
56
57
 
57
- response.results.each { |i| i.name }
58
+ returned response included data on below:
59
+ * results
60
+ * total_items
61
+ * per_page
62
+ * pages
63
+ * current_page
58
64
 
59
- Query IPO
65
+ ### Get Organization && RelationShips by the permalink
60
66
 
61
- response = Crunchbase::Model::Search.search({}, 'ipos')
62
-
63
- response.results.each { |i| i.name }
64
-
65
- ## Get Organization && RelationShips
66
-
67
- Get information by the permalink, Example:
68
-
69
- response = Crunchbase::Model::Organization.get("facebook")
67
+ response = client.get('Organization', 'facebook')
70
68
 
71
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 ]
72
70
 
73
- response.founders
74
- response.competitors_total_items
75
- response.websites
71
+ # Methods - Get Organization with one relationship data
76
72
 
77
- NOTE: If you want all `past_team` `board_members_and_advisors` items, Please do:
78
-
79
- # Methods
80
-
81
- past_team = Crunchbase::Model::PastTeam.organization_lists("facebook")
73
+ 1. response = client.get('Organization', 'facebook', 'PastTeam')
82
74
  past_team.results.collect { |p| [p.title, p.person.first_name] }
83
75
 
84
76
  ....
85
77
 
86
- ## Person
87
-
88
- Get information by the permalink, Example:
78
+ ### Get Person by the permalink
89
79
 
90
- person = Crunchbase::Model::Person.get( permalink )
80
+ person = client.get('Person', permalink)
91
81
 
92
82
  #<Crunchbase::Model::Person:0x007fc185215f68 @type_name="Person", @uuid="a578dcf9859ec8b52182e3aa3c383b13", ...>
93
83
 
94
- people = Crunchbase::Model::Person.list( page )
84
+ people = client.list('Person', page)
95
85
 
96
86
  people.results
97
87
 
@@ -101,7 +91,7 @@ Get information by the permalink, Example:
101
91
  #<Crunchbase::Model::PersonSummary: ...>
102
92
  ...... ]
103
93
 
104
- ## Contributing
94
+ ### Contributing
105
95
 
106
96
  1. Fork it ( https://github.com/encoreshao/crunchbase-ruby-library/fork )
107
97
  2. Create your feature branch (`git checkout -b my-new-feature`)
@@ -109,6 +99,6 @@ Get information by the permalink, Example:
109
99
  4. Push to the branch (`git push origin my-new-feature`)
110
100
  5. Create a new Pull Request
111
101
 
112
- ## Copyright
102
+ ### Copyright
113
103
 
114
104
  Copyright © 2015-05 Encore Shao. See LICENSE for details.
data/Rakefile CHANGED
@@ -1,2 +1,5 @@
1
- require "bundler/gem_tasks"
1
+ # frozen_string_literal: true
2
+ require 'rspec/core/rake_task'
3
+ require 'bundler/gem_tasks'
2
4
 
5
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'crunchbase'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ require "pry"
12
+ Pry.start
13
+
14
+ # require 'irb'
15
+ # IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -1,25 +1,28 @@
1
1
  # coding: utf-8
2
+ # frozen_string_literal: true
2
3
  lib = File.expand_path('../lib', __FILE__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
5
  require 'crunchbase/version'
5
6
 
6
7
  Gem::Specification.new do |spec|
7
- spec.name = "crunchbase-ruby-library"
8
+ spec.name = 'crunchbase-ruby-library'
8
9
  spec.version = Crunchbase::VERSION
9
- spec.authors = ["Encore Shao"]
10
- spec.email = ["encore.shao@gmail.com"]
11
- spec.summary = %q{ Ruby wrapper for Crunchbase API version 3 }
12
- spec.description = %q{ Ruby wrapper for Crunchbase API version 3 }
13
- spec.homepage = "https://github.com/encoreshao/crunchbase-ruby-library"
14
- spec.license = "MIT"
10
+ spec.authors = ['Encore Shao']
11
+ spec.email = ['encore.shao@gmail.com']
12
+ spec.summary = 'A Ruby wrapper for Crunchbase API v3.1'
13
+ spec.description = 'A Ruby wrapper for Crunchbase API version 3.1'
14
+ spec.homepage = 'https://github.com/encoreshao/crunchbase-ruby-library'
15
+ spec.license = 'MIT'
15
16
 
16
17
  spec.files = `git ls-files -z`.split("\x0")
17
18
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
19
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
20
+ spec.require_paths = ['lib']
20
21
 
21
- spec.add_development_dependency "bundler", "~> 1.6"
22
- spec.add_development_dependency "rake"
23
- spec.add_development_dependency "rspec", "~> 3.0"
24
- spec.add_development_dependency "rspec-its"
22
+ spec.add_development_dependency 'bundler', '~> 1.6'
23
+ spec.add_development_dependency 'rake'
24
+ spec.add_development_dependency 'pry'
25
+ spec.add_development_dependency 'rspec', '~> 3.0'
26
+ spec.add_development_dependency 'rspec-its'
27
+ spec.add_development_dependency 'rubocop'
25
28
  end
data/lib/crunchbase.rb CHANGED
@@ -1,12 +1,21 @@
1
+ # frozen_string_literal: true
1
2
  require 'singleton'
2
3
  require 'time'
3
4
 
4
- require "crunchbase/version"
5
- require 'crunchbase/api'
5
+ require 'json'
6
+ require 'crunchbase/version'
6
7
  require 'crunchbase/model'
7
- require "crunchbase/exception"
8
+ require 'crunchbase/request'
9
+ require 'crunchbase/client'
10
+ require 'crunchbase/api'
11
+ require 'crunchbase/exception'
8
12
 
9
13
  module Crunchbase
14
+ API_VERSION = '3.1'
15
+ API_BASE_URL = 'https://api.crunchbase.com'
16
+ WEB_SITE_URL = 'https://www.crunchbase.com'
17
+ IMAGE_URL = 'https://res.cloudinary.com/crunchbase-production/'
18
+
10
19
  class << self
11
20
  end
12
21
  end
@@ -1,4 +1,5 @@
1
1
  # encoding: utf-8
2
+ # frozen_string_literal: true
2
3
 
3
4
  require 'net/http'
4
5
 
@@ -12,157 +13,164 @@ require 'timeout'
12
13
 
13
14
  module Crunchbase
14
15
  class API
15
-
16
- SUPPORTED_ENTITIES = ['organizations', 'people', 'products', 'funding_rounds', 'funding-rounds', 'acquisitions', 'ipos', 'locations', 'categories', 'offices', 'customers', 'degrees', 'experience', 'primary_affiliation', 'videos', 'founded_companies', 'primary_location', 'advisor_at']
16
+ SUPPORTED_ENTITIES = {
17
+ 'categories' => Model::Category,
18
+ 'organizations' => Model::OrganizationSummary,
19
+ 'people' => Model::PersonSummary,
20
+ 'products' => Model::ProductSummary,
21
+ 'ipos' => Model::Ipo,
22
+ 'funding_rounds' => Model::FundingRound,
23
+ 'funding-rounds' => Model::FundingRound,
24
+ 'acquisitions' => Model::Acquisition,
25
+ 'locations' => Model::Location,
26
+ 'offices' => Model::Office,
27
+ 'customers' => Model::Customer,
28
+ 'degrees' => Model::Degree,
29
+ # 'experience' => nil,
30
+ 'primary_affiliation' => Model::PrimaryAffiliation,
31
+ 'videos' => Model::Video,
32
+ 'founded_companies' => Model::FoundedCompany,
33
+ 'primary_location' => Model::PrimaryLocation,
34
+ 'advisor_at' => Model::AdvisoryRole,
35
+ 'investors' => Model::Organization
36
+ }.freeze
17
37
 
18
38
  @timeout_limit = 60
19
39
  @redirect_limit = 2
20
- @version = '3'
21
- @base_url = 'https://api.crunchbase.com'
22
- @site_url = "https://www.crunchbase.com"
23
- @image_url = "https://res.cloudinary.com/crunchbase-production/"
24
- @debug = false
40
+ @debug = false
25
41
 
26
42
  # Must be overridden in subclasses
27
- RESOURCE_NAME = "undefined"
28
- RESOURCE_LIST = "undefineds"
43
+ RESOURCE_NAME = 'undefined'
44
+ RESOURCE_LIST = 'undefineds'
29
45
 
30
- ORDER_CREATED_AT_ASC = 'created_at asc'
31
- ORDER_CREATED_AT_DESC = 'created_at desc'
32
- ORDER_UPDATED_AT_ASC = 'updated_at asc'
33
- ORDER_UPDATED_AT_DESC = 'updated_at desc'
46
+ ORDER_CREATED_AT_ASC = 'created_at ASC'
47
+ ORDER_CREATED_AT_DESC = 'created_at DESC'
48
+ ORDER_UPDATED_AT_ASC = 'updated_at ASC'
49
+ ORDER_UPDATED_AT_DESC = 'updated_at DESC'
34
50
 
35
51
  class << self
36
- attr_accessor :timeout_limit, :redirect_limit, :key, :base_url, :version, :debug, :image_url, :site_url
52
+ attr_accessor :timeout_limit, :redirect_limit, :key, :debug
37
53
 
38
54
  def api_url
39
- base_url.gsub(/\/$/, '') + '/v/' + version + '/'
55
+ API_BASE_URL.gsub(/\/$/, '') + '/v' + API_VERSION + '/'
40
56
  end
41
- end
42
57
 
43
- def self.single_entity(permalink, entity_name)
44
- raise CrunchException, "Unsupported Entity Type" unless SUPPORTED_ENTITIES.include?(entity_name)
58
+ def single_entity(permalink, entity_name)
59
+ raise CrunchException, 'Unsupported Entity Type' unless SUPPORTED_ENTITIES.keys.include?(entity_name)
45
60
 
46
- fetch(permalink, entity_name)
47
- end
61
+ fetch(permalink, entity_name)
62
+ end
48
63
 
49
- private
64
+ # Returns the JSON parser, whether that's an instance of Yajl or JSON
65
+ def parser
66
+ return Yajl::Parser if defined?(Yajl)
50
67
 
51
- # Returns the JSON parser, whether that's an instance of Yajl or JSON
52
- def self.parser
53
- if defined?(Yajl)
54
- Yajl::Parser
55
- else
56
68
  JSON
57
69
  end
58
- end
59
-
60
- # Fetches URI for the permalink interface.
61
- def self.fetch(permalink, object_name)
62
- get_json_response( api_url + "#{object_name}/#{permalink}" )
63
- end
64
-
65
- # Fetches URI for the search interface.
66
- def self.search(options, resource_list)
67
- options[:page] = 1 if options[:page].nil?
68
- options[:order] = ORDER_CREATED_AT_ASC if options[:order].nil?
69
70
 
70
- uri = api_url + "#{resource_list}?" + collect_parameters(options)
71
+ # Fetches URI for the permalink interface.
72
+ def fetch(permalink, kclass_name)
73
+ get_json_response(api_url + "#{kclass_name}/#{permalink}")
74
+ end
71
75
 
72
- get_json_response(uri)
73
- end
76
+ # Fetches URI for the search interface.
77
+ def search(options, resource_list)
78
+ options[:page] = 1 if options[:page].nil?
79
+ options[:order] = ORDER_CREATED_AT_ASC if options[:order].nil?
74
80
 
81
+ uri = api_url + "#{resource_list}?" + collect_parameters(options)
75
82
 
76
- # Fetches URI for the search interface.
77
- def self.list(options, resource_list)
78
- options[:page] = 1 if options[:page].nil?
79
- model_name = options.delete(:model_name)
83
+ get_json_response(uri)
84
+ end
80
85
 
81
- uri = api_url + "#{resource_list}?" + collect_parameters(options)
86
+ # Fetches URI for the search interface.
87
+ def list(options, resource_list)
88
+ options[:page] = 1 if options[:page].nil?
89
+ model_name = options.delete(:model_name) || SUPPORTED_ENTITIES[resource_list]
82
90
 
83
- Crunchbase::Model::Search.new options, get_json_response(uri), model_name
84
- end
91
+ uri = api_url + "#{resource_list}?" + collect_parameters(options)
85
92
 
86
- def self.collect_parameters(options)
87
- require "cgi"
93
+ Model::Search.new options, get_json_response(uri), model_name
94
+ end
88
95
 
89
- options.map{|k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"}.join('&')
90
- end
96
+ def collect_parameters(options)
97
+ require 'cgi'
91
98
 
92
- def self.organization_lists(permalink, category, options)
93
- lists_for_category('organizations', permalink, category, options)
94
- end
99
+ options.map { |k, v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join('&')
100
+ end
95
101
 
96
- # Visit: https://api.crunchbase.com/v/#{version}/people/#{permalink}/#{category}?user_key=key
97
- def self.person_lists(permalink, category, options)
98
- lists_for_category('people', permalink, category, options)
99
- end
102
+ def organization_lists(permalink, category, options)
103
+ lists_for_category('organizations', permalink, category, options)
104
+ end
100
105
 
101
- # Visit: https://api.crunchbase.com/v/#{version}/funding-rounds/#{permalink}/#{category}?user_key=key
102
- def self.funding_rounds_lists(permalink, category, options)
103
- lists_for_category('funding-rounds', permalink, category, options)
104
- end
106
+ def person_lists(permalink, category, options)
107
+ lists_for_category('people', permalink, category, options)
108
+ end
105
109
 
106
- def self.lists_for_category(classify_name, permalink, category, options)
107
- options[:page] = 1 if options[:page].nil?
108
- options[:order] = ORDER_CREATED_AT_ASC if options[:order].nil?
109
- model_name = options.delete(:model_name)
110
+ def funding_rounds_lists(permalink, category, options)
111
+ lists_for_category('funding-rounds', permalink, category, options)
112
+ end
110
113
 
111
- uri = api_url + "#{classify_name}/#{permalink}/#{category}?#{collect_parameters(options)}"
114
+ def lists_for_category(classify_name, permalink, category, options)
115
+ options[:page] = 1 if options[:page].nil?
116
+ options[:order] = ORDER_CREATED_AT_ASC if options[:order].nil?
117
+ model_name = options.delete(:model_name)
112
118
 
113
- Crunchbase::Model::Search.new options, get_json_response(uri), model_name
114
- end
119
+ uri = api_url + "#{classify_name}/#{permalink}/#{category}?#{collect_parameters(options)}"
115
120
 
116
- # Gets specified URI, then parses the returned JSON. Raises Timeout error
117
- # if request time exceeds set limit. Raises Crunchbase::Exception if returned
118
- # JSON contains an error.
119
- def self.get_json_response(uri)
120
- raise Crunchbase::Exception, "User key required, visit http://data.crunchbase.com" unless @key
121
- uri = uri + "#{uri.match('\?') ? "&" : "?"}user_key=#{@key}"
121
+ Model::Search.new options, get_json_response(uri), model_name
122
+ end
122
123
 
123
- resp = Timeout::timeout(@timeout_limit) {
124
- get_url_following_redirects(uri, @redirect_limit)
125
- }
124
+ # Gets specified URI, then parses the returned JSON. Raises Timeout error
125
+ # if request time exceeds set limit. Raises Exception if returned
126
+ # JSON contains an error.
127
+ def get_json_response(uri)
128
+ raise Exception, 'User key required, visit https://data.crunchbase.com/v3.1/docs' unless @key
129
+ uri += "#{uri =~ /\?/ ? '&' : '?'}user_key=#{@key}"
126
130
 
127
- _response = parser.parse(resp)
128
- raise Crunchbase::Exception, { message: _response["error"], code: _response["status"].to_i } unless _response["error"].blank?
131
+ resp = Timeout.timeout(@timeout_limit) do
132
+ get_url_following_redirects(uri, @redirect_limit)
133
+ end
129
134
 
130
- response = _response["data"]
131
- raise Crunchbase::Exception, response["error"] if response.class == Hash && response["error"] && response["error"]["code"] != 500
135
+ response = parser.parse(resp)
136
+ response = response[0] if response.is_a?(Array)
137
+ raise Exception, message: response['message'], status: response['status'] unless response['message'].nil?
132
138
 
133
- response
134
- end
139
+ response['data']
140
+ end
135
141
 
136
- # Performs actual HTTP requests, recursively if a redirect response is
137
- # encountered. Will raise HTTP error if response is not 200, 404, or 3xx.
138
- def self.get_url_following_redirects(uri_str, limit = 10)
139
- raise Crunchbase::Exception, 'HTTP redirect too deep' if limit == 0
142
+ # Performs actual HTTP requests, recursively if a redirect response is
143
+ # encountered. Will raise HTTP error if response is not 200, 404, or 3xx.
144
+ def get_url_following_redirects(uri_str, limit = 10)
145
+ raise Exception, 'HTTP redirect too deep' if limit.zero?
140
146
 
141
- uri = URI.parse(URI.encode(uri_str))
147
+ uri = URI.parse(URI.encode(uri_str))
142
148
 
143
- debug_log!(uri) if debug
149
+ debugging(uri)
144
150
 
145
- http = Net::HTTP.new(uri.host, uri.port)
146
- http.use_ssl = true if uri.scheme == 'https'
147
- response = http.start do |h|
148
- h.request Net::HTTP::Get.new(uri.request_uri)
149
- end
151
+ http = Net::HTTP.new(uri.host, uri.port)
152
+ http.use_ssl = true if uri.scheme == 'https'
153
+ response = http.start do |h|
154
+ h.request Net::HTTP::Get.new(uri.request_uri)
155
+ end
150
156
 
151
- case response
157
+ case response
152
158
  when Net::HTTPSuccess, Net::HTTPNotFound, Net::HTTPInternalServerError
153
159
  response.body
154
160
  when Net::HTTPRedirection
155
161
  get_url_following_redirects(response['location'], limit - 1)
156
162
  else
157
163
  response.error!
164
+ end
158
165
  end
159
- end
160
166
 
161
- def self.debug_log!(uri)
162
- puts "*"*120
163
- puts "*** #{uri} ***"
164
- puts "*"*120
165
- end
167
+ def debugging(uri)
168
+ return unless debug
166
169
 
170
+ puts '*' * 140
171
+ puts "*** #{uri} ***"
172
+ puts '*' * 140
173
+ end
174
+ end
167
175
  end
168
176
  end