google_knowledge_graph 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e1d72961ddce5869648605bc27111604c38fc610a14bdf3e951813e5461a542c
4
+ data.tar.gz: b7a68aeb13bb398c736c0e70f34b777aed0d7fffe4a41955c44450f174273c7c
5
+ SHA512:
6
+ metadata.gz: 2d8b790aa84d29eb1d4e934536d0d0ae178e6b14982d21f6b29f4091cd3409faf876ed552120e77da9ac7c6683e1970d5fa59bba5eb0411ae2774b0b33124036
7
+ data.tar.gz: 59519faabfd2572d89bc842aecc1ed1cb4f635f3411cd2dbc4ad9af0e9d157ecfa8445ba780b10223b1db60388c1a9442d358e99992073d11fa99494b22e3e2b
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "https://rubygems.org"
2
+ gemspec
@@ -0,0 +1,65 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ google_knowledge_graph (0.1.0)
5
+ http (~> 4.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ addressable (2.7.0)
11
+ public_suffix (>= 2.0.2, < 5.0)
12
+ crack (0.4.3)
13
+ safe_yaml (~> 1.0.0)
14
+ diff-lcs (1.3)
15
+ domain_name (0.5.20190701)
16
+ unf (>= 0.0.5, < 1.0.0)
17
+ ffi (1.12.2)
18
+ ffi-compiler (1.0.1)
19
+ ffi (>= 1.0.0)
20
+ rake
21
+ hashdiff (1.0.1)
22
+ http (4.4.1)
23
+ addressable (~> 2.3)
24
+ http-cookie (~> 1.0)
25
+ http-form_data (~> 2.2)
26
+ http-parser (~> 1.2.0)
27
+ http-cookie (1.0.3)
28
+ domain_name (~> 0.5)
29
+ http-form_data (2.3.0)
30
+ http-parser (1.2.1)
31
+ ffi-compiler (>= 1.0, < 2.0)
32
+ public_suffix (4.0.5)
33
+ rake (13.0.1)
34
+ rspec (3.9.0)
35
+ rspec-core (~> 3.9.0)
36
+ rspec-expectations (~> 3.9.0)
37
+ rspec-mocks (~> 3.9.0)
38
+ rspec-core (3.9.2)
39
+ rspec-support (~> 3.9.3)
40
+ rspec-expectations (3.9.2)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.9.0)
43
+ rspec-mocks (3.9.1)
44
+ diff-lcs (>= 1.2.0, < 2.0)
45
+ rspec-support (~> 3.9.0)
46
+ rspec-support (3.9.3)
47
+ safe_yaml (1.0.5)
48
+ unf (0.1.4)
49
+ unf_ext
50
+ unf_ext (0.0.7.7)
51
+ webmock (3.8.3)
52
+ addressable (>= 2.3.6)
53
+ crack (>= 0.3.2)
54
+ hashdiff (>= 0.4.0, < 2.0.0)
55
+
56
+ PLATFORMS
57
+ ruby
58
+
59
+ DEPENDENCIES
60
+ google_knowledge_graph!
61
+ rspec (~> 3.0)
62
+ webmock (~> 3.0)
63
+
64
+ BUNDLED WITH
65
+ 2.1.4
@@ -0,0 +1,144 @@
1
+ # google knowledge graph client
2
+
3
+ A ruby client for Google's knowledge graph to:
4
+ - getting a knowledge entity by id
5
+ - searching entities, while set limits by types, languages, and score
6
+
7
+ ## Usage
8
+
9
+ ### installing
10
+
11
+ ```
12
+ gem install google_knowledge_graph
13
+ ```
14
+ or in your Gemfile
15
+ ```
16
+ gem 'google_knowledge_graph'
17
+ ```
18
+ then
19
+ ```
20
+ bundle install
21
+ ```
22
+
23
+ ### config
24
+
25
+ You'll need an API key to make a request, see the [doc](https://developers.google.com/knowledge-graph) for more information.
26
+
27
+ ```
28
+ GoogleKnowledgeGraph.api_key = 'foo'
29
+ ```
30
+
31
+ ### getting knowledge entity by id
32
+
33
+ > google knowledge graph responses entity result with @id like `kg:/m/0dl567`,
34
+ yet it expects id being `/m/0dl567` when querying the service
35
+
36
+ ```
37
+ entity = GoogleKnowledgeGraph.get '/m/05pbsry'
38
+
39
+ # the `data` field contains the original payload
40
+ entity.data
41
+ => {
42
+ "url": "http://www.nbc.com/community-show/",
43
+ "detailedDescription": {
44
+ "url": "https://en.wikipedia.org/wiki/Community_(TV_series)",
45
+ "license": "https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License",
46
+ "articleBody": "Community is an American sitcom television series created by Dan Harmon that aired on NBC and Yahoo! Screen from September 17, 2009, to June 2, 2015. "
47
+ },
48
+ "@id": "kg:/m/05pbsry",
49
+ "name": "Community",
50
+ "@type": [
51
+ "Thing",
52
+ "TVSeries"
53
+ ],
54
+ "description": "American sitcom",
55
+ "image": {
56
+ "url": "https://www.wikiwand.com/en/Community_(TV_series)",
57
+ "contentUrl": "https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRvfDLB01VDeWHLm-4R68LHym6-RzrQxe2PJF9aLNDw96kL7D-2"
58
+ }
59
+ }
60
+ ```
61
+
62
+ > For detail of the response, see [the reference](https://developers.google.com/knowledge-graph/reference/rest/v1)
63
+
64
+ #### shorthand methods
65
+
66
+ For your convenience, there're a few shorthand methods to `dig` into the data
67
+
68
+ ```
69
+ entity.id
70
+ => 'kg:/m/05pbsry'
71
+
72
+ entity.id_without_prefix
73
+ => '/m/05pbsry'
74
+
75
+ entity.id_without_prefix
76
+ => '/m/05pbsry'
77
+
78
+ entity.types
79
+ => ['Thing', 'TVSeries']
80
+
81
+ entity.name
82
+ => 'Community'
83
+
84
+ entity.url
85
+ => 'http://www.nbc.com/community-show/'
86
+
87
+ entity.image
88
+ => {
89
+ 'url': 'https://www.wikiwand.com/en/Community_(TV_series)',
90
+ 'contentUrl': 'https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRvfDLB01VDeWHLm-4R68LHym6-RzrQxe2PJF9aLNDw96kL7D-2'
91
+ }
92
+
93
+ entity.image_url # data.dig 'image', 'contentUrl'
94
+ => 'https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRvfDLB01VDeWHLm-4R68LHym6-RzrQxe2PJF9aLNDw96kL7D-2'
95
+
96
+ entity.description
97
+ => 'American sitcom'
98
+
99
+ entity.detailed_description
100
+ => {
101
+ 'license': 'https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License',
102
+ 'articleBody': 'Community is an American sitcom television series created by Dan Harmon that aired on NBC and Yahoo! Screen from September 17, 2009, to June 2, 2015. ',
103
+ 'url': 'https://en.wikipedia.org/wiki/Community_(TV_series)'
104
+ }
105
+
106
+ entity.description_text # data.dig 'detailedDescription', 'articleBody'
107
+ => 'Community is an American sitcom television series created by Dan Harmon that aired on NBC and Yahoo! Screen from September 17, 2009, to June 2, 2015. '
108
+
109
+ entity.description_url # data.dig 'detailedDescription', 'url'
110
+ => 'https://en.wikipedia.org/wiki/Community_(TV_series)'
111
+ ```
112
+
113
+ ### searching entities
114
+
115
+ ```
116
+ GoogleKnowledgeGraph.search 'community'
117
+ => [
118
+ {
119
+ entity: …,
120
+ score: 1000,
121
+ },
122
+ {
123
+ entity: …,
124
+ score: 800,
125
+ },
126
+
127
+ ]
128
+ ```
129
+
130
+ #### options
131
+
132
+ ```
133
+ GoogleKnowledgeGraph.search 'community', types: ['TVSeries']
134
+ => [
135
+
136
+ ]
137
+ ```
138
+
139
+ > Currently, `types` is the only option being supported.
140
+ > For detail of the options, see [the reference](https://developers.google.com/knowledge-graph/reference/rest/v1).
141
+
142
+ ## Development
143
+
144
+ Use `rspec` or `bundle exec rspec` to run the test suite.
@@ -0,0 +1,21 @@
1
+ $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
2
+
3
+ require 'google_knowledge_graph/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'google_knowledge_graph'
7
+ s.version = GoogleKnowledgeGraph::VERSION
8
+ s.licenses = ['MIT']
9
+ s.summary = "Ruby client for Google's Knowledge Graph"
10
+ s.authors = ['Nami WANG']
11
+ s.files = `git ls-files -z`.split("\x0")
12
+ s.homepage = 'https://github.com/ref-wiki/google_knowledge_graph'
13
+ s.metadata = {
14
+ 'source_code_uri' => 'https://github.com/ref-wiki/google_knowledge_graph'
15
+ }
16
+
17
+ s.add_development_dependency 'rspec', '~> 3.0'
18
+ s.add_development_dependency 'webmock', '~> 3.0'
19
+
20
+ s.add_runtime_dependency 'http', '~> 4.0'
21
+ end
@@ -0,0 +1,56 @@
1
+ require 'http'
2
+
3
+ require 'google_knowledge_graph/entity'
4
+
5
+ module GoogleKnowledgeGraph
6
+ API_URL = 'https://kgsearch.googleapis.com/v1/entities:search'
7
+
8
+ @api_key = nil
9
+
10
+ class << self
11
+ attr_accessor :api_key
12
+
13
+ def get id
14
+ ensure_api_key!
15
+ raise StandardError.new('InvalidIdStartingWithKG') if id.start_with? 'kg:'
16
+
17
+ response = request(
18
+ key: api_key,
19
+ limit: 1,
20
+ ids: [id]
21
+ )
22
+
23
+ return nil unless response.status.success?
24
+
25
+ Entity.new(response.body)
26
+ end
27
+
28
+ def search query, types: []
29
+ ensure_api_key!
30
+
31
+ response = request(
32
+ key: api_key,
33
+ query: query,
34
+ types: types,
35
+ )
36
+
37
+ return [] unless response.status.success?
38
+
39
+ JSON.parse(response.body)['itemListElement']
40
+ .map{ |item| {
41
+ entity: Entity.new(item['result']),
42
+ score: item['resultScore']
43
+ }}
44
+ end
45
+
46
+ private
47
+
48
+ def ensure_api_key!
49
+ raise StandardError.new('EmptyAPIKey') unless api_key
50
+ end
51
+
52
+ def request params
53
+ HTTP.get API_URL, params: params
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,53 @@
1
+ module GoogleKnowledgeGraph
2
+ class Entity
3
+ attr_reader :data
4
+
5
+ def initialize data
6
+ @data = data
7
+ end
8
+
9
+ def id
10
+ @data['@id']
11
+ end
12
+
13
+ def id_without_prefix
14
+ id.delete_prefix 'kg:'
15
+ end
16
+
17
+ def types
18
+ @data['@type']
19
+ end
20
+
21
+ def name
22
+ @data['name']
23
+ end
24
+
25
+ def url
26
+ @data['url']
27
+ end
28
+
29
+ def image
30
+ @data['image']
31
+ end
32
+
33
+ def image_url
34
+ @data.dig 'image', 'contentUrl'
35
+ end
36
+
37
+ def description
38
+ @data['description']
39
+ end
40
+
41
+ def detailed_description
42
+ @data['detailedDescription']
43
+ end
44
+
45
+ def description_text
46
+ detailed_description.dig 'articleBody'
47
+ end
48
+
49
+ def description_url
50
+ detailed_description.dig 'url'
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,3 @@
1
+ module GoogleKnowledgeGraph
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,19 @@
1
+ {
2
+ "url": "http://www.nbc.com/community-show/",
3
+ "detailedDescription": {
4
+ "url": "https://en.wikipedia.org/wiki/Community_(TV_series)",
5
+ "license": "https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License",
6
+ "articleBody": "Community is an American sitcom television series created by Dan Harmon that aired on NBC and Yahoo! Screen from September 17, 2009, to June 2, 2015. "
7
+ },
8
+ "@id": "kg:/m/05pbsry",
9
+ "name": "Community",
10
+ "@type": [
11
+ "Thing",
12
+ "TVSeries"
13
+ ],
14
+ "description": "American sitcom",
15
+ "image": {
16
+ "url": "https://www.wikiwand.com/en/Community_(TV_series)",
17
+ "contentUrl": "https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRvfDLB01VDeWHLm-4R68LHym6-RzrQxe2PJF9aLNDw96kL7D-2"
18
+ }
19
+ }
@@ -0,0 +1,36 @@
1
+ {
2
+ "@context": {
3
+ "goog": "http://schema.googleapis.com/",
4
+ "kg": "http://g.co/kg",
5
+ "resultScore": "goog:resultScore",
6
+ "EntitySearchResult": "goog:EntitySearchResult",
7
+ "@vocab": "http://schema.org/",
8
+ "detailedDescription": "goog:detailedDescription"
9
+ },
10
+ "@type": "ItemList",
11
+ "itemListElement": [
12
+ {
13
+ "@type": "EntitySearchResult",
14
+ "result": {
15
+ "url": "http://www.nbc.com/community-show/",
16
+ "detailedDescription": {
17
+ "url": "https://en.wikipedia.org/wiki/Community_(TV_series)",
18
+ "license": "https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License",
19
+ "articleBody": "Community is an American sitcom television series created by Dan Harmon that aired on NBC and Yahoo! Screen from September 17, 2009, to June 2, 2015. "
20
+ },
21
+ "@id": "kg:/m/05pbsry",
22
+ "name": "Community",
23
+ "@type": [
24
+ "Thing",
25
+ "TVSeries"
26
+ ],
27
+ "description": "American sitcom",
28
+ "image": {
29
+ "url": "https://www.wikiwand.com/en/Community_(TV_series)",
30
+ "contentUrl": "https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRvfDLB01VDeWHLm-4R68LHym6-RzrQxe2PJF9aLNDw96kL7D-2"
31
+ }
32
+ },
33
+ "resultScore": 0
34
+ }
35
+ ]
36
+ }
@@ -0,0 +1,296 @@
1
+ {
2
+ "@context": {
3
+ "kg": "http://g.co/kg",
4
+ "@vocab": "http://schema.org/",
5
+ "goog": "http://schema.googleapis.com/",
6
+ "EntitySearchResult": "goog:EntitySearchResult",
7
+ "resultScore": "goog:resultScore",
8
+ "detailedDescription": "goog:detailedDescription"
9
+ },
10
+ "@type": "ItemList",
11
+ "itemListElement": [
12
+ {
13
+ "resultScore": 4182.677734375,
14
+ "result": {
15
+ "name": "Community",
16
+ "image": {
17
+ "url": "https://zh.wikipedia.org/wiki/File:East_Asian_Community.svg",
18
+ "contentUrl": "https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcRWLe5EY3TmNaXpA8MaaxounsLYmH_H0J8a8ypz9h_R8Vo4XnZ_"
19
+ },
20
+ "@type": [
21
+ "Thing"
22
+ ],
23
+ "@id": "kg:/m/01qls",
24
+ "detailedDescription": {
25
+ "license": "https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License",
26
+ "url": "https://en.wikipedia.org/wiki/Community",
27
+ "articleBody": "A community is a social unit with commonality such as norms, religion, values, customs, or identity. Communities may share a sense of place situated in a given geographical area or in virtual space through communication platforms. "
28
+ }
29
+ },
30
+ "@type": "EntitySearchResult"
31
+ },
32
+ {
33
+ "result": {
34
+ "@type": [
35
+ "Thing"
36
+ ],
37
+ "name": "Community",
38
+ "detailedDescription": {
39
+ "license": "https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License",
40
+ "url": "https://en.wikipedia.org/wiki/Community_(ecology)",
41
+ "articleBody": "In ecology, a community is a group or association of populations of two or more different species occupying the same geographical area at the same time, also known as a biocoenosis. The term community has a variety of uses. "
42
+ },
43
+ "@id": "kg:/g/120lq9cv"
44
+ },
45
+ "resultScore": 1027.097412109375,
46
+ "@type": "EntitySearchResult"
47
+ },
48
+ {
49
+ "@type": "EntitySearchResult",
50
+ "result": {
51
+ "@id": "kg:/m/05pbsry",
52
+ "@type": [
53
+ "Thing",
54
+ "TVSeries"
55
+ ],
56
+ "detailedDescription": {
57
+ "url": "https://en.wikipedia.org/wiki/Community_(TV_series)",
58
+ "articleBody": "Community is an American sitcom television series created by Dan Harmon that aired on NBC and Yahoo! Screen from September 17, 2009, to June 2, 2015. ",
59
+ "license": "https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License"
60
+ },
61
+ "name": "Community",
62
+ "image": {
63
+ "url": "https://www.wikiwand.com/en/Community_(TV_series)",
64
+ "contentUrl": "https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRvfDLB01VDeWHLm-4R68LHym6-RzrQxe2PJF9aLNDw96kL7D-2"
65
+ },
66
+ "url": "http://www.nbc.com/community-show/",
67
+ "description": "American sitcom"
68
+ },
69
+ "resultScore": 967.75714111328125
70
+ },
71
+ {
72
+ "resultScore": 125.786506652832,
73
+ "result": {
74
+ "name": "Online community",
75
+ "@id": "kg:/m/09k4rpx",
76
+ "detailedDescription": {
77
+ "articleBody": "An online community, also called an internet community or web community, is a virtual community whose members interact with each other primarily via the Internet. ",
78
+ "url": "https://en.wikipedia.org/wiki/Online_community",
79
+ "license": "https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License"
80
+ },
81
+ "@type": [
82
+ "Thing"
83
+ ]
84
+ },
85
+ "@type": "EntitySearchResult"
86
+ },
87
+ {
88
+ "result": {
89
+ "@id": "kg:/g/11dxpv9v7r",
90
+ "@type": [
91
+ "Thing",
92
+ "Corporation",
93
+ "Organization"
94
+ ],
95
+ "name": "Shimmur, Inc.",
96
+ "description": "Company"
97
+ },
98
+ "resultScore": 95.504226684570312,
99
+ "@type": "EntitySearchResult"
100
+ },
101
+ {
102
+ "@type": "EntitySearchResult",
103
+ "resultScore": 94.383293151855469,
104
+ "result": {
105
+ "@type": [
106
+ "Thing"
107
+ ],
108
+ "@id": "kg:/m/0949wd",
109
+ "name": "Community",
110
+ "detailedDescription": {
111
+ "articleBody": "A community is an administrative division found in Belgium, Canada, Georgia, Greece, Iceland, Trinidad and Tobago, Ukraine, Wales, and the League of Nations Class A mandates.",
112
+ "license": "https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License",
113
+ "url": "https://en.wikipedia.org/wiki/Community_(administrative_division)"
114
+ }
115
+ }
116
+ },
117
+ {
118
+ "@type": "EntitySearchResult",
119
+ "result": {
120
+ "name": "Community",
121
+ "@type": [
122
+ "Book",
123
+ "Thing"
124
+ ],
125
+ "description": "Book by Zygmunt Bauman",
126
+ "@id": "kg:/m/06rpcjb"
127
+ },
128
+ "resultScore": 89.038459777832031
129
+ },
130
+ {
131
+ "result": {
132
+ "detailedDescription": {
133
+ "url": "https://en.wikipedia.org/wiki/Community_(trade_union)",
134
+ "license": "https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License",
135
+ "articleBody": "Community is a UK trade union which formed in 2004. The union represents workers in a diverse range of sectors, including iron and steel, domestic appliance manufacturing, clothing, textiles, footwear, road transport, betting and gaming and call centres, as well as workers in voluntary organisations, workshops for visually impaired and disabled people, community-care providers and housing associations. "
136
+ },
137
+ "name": "Community",
138
+ "image": {
139
+ "url": "https://en.wikipedia.org/wiki/Community_(trade_union)",
140
+ "contentUrl": "https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcQ9TJBpc9YXhLPiBLg6FjCKkMtxPh0pCqRW9rU8vi93K7NtXbUO"
141
+ },
142
+ "@id": "kg:/m/09w295",
143
+ "@type": [
144
+ "Thing",
145
+ "Organization"
146
+ ],
147
+ "description": "Trade union"
148
+ },
149
+ "resultScore": 54.606101989746087,
150
+ "@type": "EntitySearchResult"
151
+ },
152
+ {
153
+ "resultScore": 52.932155609130859,
154
+ "result": {
155
+ "@type": [
156
+ "Thing",
157
+ "Corporation",
158
+ "Organization"
159
+ ],
160
+ "description": "Company",
161
+ "@id": "kg:/g/11fy28qjb3",
162
+ "name": "Origin Bank"
163
+ },
164
+ "@type": "EntitySearchResult"
165
+ },
166
+ {
167
+ "result": {
168
+ "@type": [
169
+ "Thing"
170
+ ],
171
+ "@id": "kg:/m/04vzrb",
172
+ "name": "Community",
173
+ "detailedDescription": {
174
+ "articleBody": "A community is a division of land in Wales that forms the lowest tier of local government in Wales. Welsh communities are analogous to civil parishes in England. In 2016 there were 870 communities in Wales.\nUntil 1974 Wales was divided into civil parishes. ",
175
+ "url": "https://en.wikipedia.org/wiki/Community_(Wales)",
176
+ "license": "https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License"
177
+ }
178
+ },
179
+ "resultScore": 29.166910171508789,
180
+ "@type": "EntitySearchResult"
181
+ },
182
+ {
183
+ "resultScore": 7,
184
+ "result": {
185
+ "@id": "kg:/g/11jg4yxs03",
186
+ "name": "Community",
187
+ "@type": [
188
+ "Thing"
189
+ ]
190
+ },
191
+ "@type": "EntitySearchResult"
192
+ },
193
+ {
194
+ "resultScore": 7,
195
+ "result": {
196
+ "name": "Community",
197
+ "@id": "kg:/g/11b8c6hfq7",
198
+ "@type": [
199
+ "Thing"
200
+ ]
201
+ },
202
+ "@type": "EntitySearchResult"
203
+ },
204
+ {
205
+ "@type": "EntitySearchResult",
206
+ "resultScore": 7,
207
+ "result": {
208
+ "@id": "kg:/g/11j31d66yy",
209
+ "name": "Community",
210
+ "description": "Literary series",
211
+ "@type": [
212
+ "Thing",
213
+ "BookSeries"
214
+ ]
215
+ }
216
+ },
217
+ {
218
+ "result": {
219
+ "name": "Community",
220
+ "@id": "kg:/g/1q5j4dbr_",
221
+ "@type": [
222
+ "Thing"
223
+ ]
224
+ },
225
+ "resultScore": 7,
226
+ "@type": "EntitySearchResult"
227
+ },
228
+ {
229
+ "result": {
230
+ "name": "Community",
231
+ "@id": "kg:/g/11j60bn08p",
232
+ "@type": [
233
+ "Thing"
234
+ ]
235
+ },
236
+ "@type": "EntitySearchResult",
237
+ "resultScore": 7
238
+ },
239
+ {
240
+ "result": {
241
+ "@type": [
242
+ "Thing"
243
+ ],
244
+ "name": "Community",
245
+ "@id": "kg:/g/1q5jzbqnt"
246
+ },
247
+ "@type": "EntitySearchResult",
248
+ "resultScore": 7
249
+ },
250
+ {
251
+ "result": {
252
+ "name": "Community",
253
+ "@type": [
254
+ "Thing"
255
+ ],
256
+ "@id": "kg:/g/11j609507g"
257
+ },
258
+ "@type": "EntitySearchResult",
259
+ "resultScore": 7
260
+ },
261
+ {
262
+ "result": {
263
+ "name": "Community",
264
+ "@id": "kg:/g/11b6gpd__m",
265
+ "@type": [
266
+ "Thing"
267
+ ],
268
+ "description": "Fictional character"
269
+ },
270
+ "@type": "EntitySearchResult",
271
+ "resultScore": 7
272
+ },
273
+ {
274
+ "resultScore": 7,
275
+ "@type": "EntitySearchResult",
276
+ "result": {
277
+ "@id": "kg:/g/11h4ck18mm",
278
+ "@type": [
279
+ "Thing"
280
+ ],
281
+ "name": "Community"
282
+ }
283
+ },
284
+ {
285
+ "resultScore": 7,
286
+ "@type": "EntitySearchResult",
287
+ "result": {
288
+ "name": "Community",
289
+ "@id": "kg:/g/11hc_qh9dn",
290
+ "@type": [
291
+ "Thing"
292
+ ]
293
+ }
294
+ }
295
+ ]
296
+ }
@@ -0,0 +1,12 @@
1
+ {
2
+ "@context": {
3
+ "detailedDescription": "goog:detailedDescription",
4
+ "EntitySearchResult": "goog:EntitySearchResult",
5
+ "goog": "http://schema.googleapis.com/",
6
+ "resultScore": "goog:resultScore",
7
+ "@vocab": "http://schema.org/",
8
+ "kg": "http://g.co/kg"
9
+ },
10
+ "@type": "ItemList",
11
+ "itemListElement": []
12
+ }
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe GoogleKnowledgeGraph::Entity do
4
+ let(:entity_data){ JSON.parse File.read('spec/fixtures/entity_data.json') }
5
+ let(:entity){ GoogleKnowledgeGraph::Entity.new entity_data }
6
+
7
+ it 'init from data' do
8
+ expect(entity).to be_a GoogleKnowledgeGraph::Entity
9
+ expect(entity.data).to include entity_data
10
+ end
11
+
12
+ it 'provides shorthand methods' do
13
+ expect(entity.id).to eq 'kg:/m/05pbsry'
14
+ expect(entity.id_without_prefix).to eq '/m/05pbsry'
15
+ expect(entity.types).to eq ['Thing', 'TVSeries']
16
+ expect(entity.name).to eq 'Community'
17
+ expect(entity.url).to eq 'http://www.nbc.com/community-show/'
18
+ expect(entity.image).to include(
19
+ 'url' => 'https://www.wikiwand.com/en/Community_(TV_series)',
20
+ 'contentUrl' => 'https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRvfDLB01VDeWHLm-4R68LHym6-RzrQxe2PJF9aLNDw96kL7D-2'
21
+ )
22
+ expect(entity.image_url).to eq 'https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRvfDLB01VDeWHLm-4R68LHym6-RzrQxe2PJF9aLNDw96kL7D-2'
23
+ expect(entity.description).to eq 'American sitcom'
24
+ expect(entity.detailed_description).to include(
25
+ 'license' => 'https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License',
26
+ 'articleBody' => 'Community is an American sitcom television series created by Dan Harmon that aired on NBC and Yahoo! Screen from September 17, 2009, to June 2, 2015. ',
27
+ 'url' => 'https://en.wikipedia.org/wiki/Community_(TV_series)'
28
+ )
29
+ expect(entity.description_text).to eq 'Community is an American sitcom television series created by Dan Harmon that aired on NBC and Yahoo! Screen from September 17, 2009, to June 2, 2015. '
30
+ expect(entity.description_url).to eq 'https://en.wikipedia.org/wiki/Community_(TV_series)'
31
+ end
32
+ end
@@ -0,0 +1,106 @@
1
+ require 'spec_helper'
2
+
3
+ describe GoogleKnowledgeGraph do
4
+ before :each do
5
+ GoogleKnowledgeGraph.api_key = nil
6
+ end
7
+
8
+ context 'Version' do
9
+ it 'claims a version' do
10
+ expect(GoogleKnowledgeGraph::VERSION).not_to be_nil
11
+ end
12
+ end
13
+
14
+ context 'Configuration' do
15
+ it 'allows setting of the api_key' do
16
+ GoogleKnowledgeGraph.api_key = 'foo'
17
+ expect(GoogleKnowledgeGraph.api_key).to eq 'foo'
18
+ end
19
+ end
20
+
21
+ context 'Fetching knowledge entity' do
22
+ before :each do
23
+ GoogleKnowledgeGraph.api_key = 'foo'
24
+ end
25
+
26
+ describe '#self.get' do
27
+ it 'raises error for empty api_key' do
28
+ GoogleKnowledgeGraph.api_key = nil
29
+
30
+ expect {
31
+ GoogleKnowledgeGraph.get 'some_id'
32
+ }.to raise_error 'EmptyAPIKey'
33
+ end
34
+
35
+ it 'raise error for id starting with `kg:`' do
36
+ expect {
37
+ GoogleKnowledgeGraph.get 'kg:/m/05pbsry'
38
+ }.to raise_error 'InvalidIdStartingWithKG'
39
+ end
40
+
41
+ it 'returns an entity' do
42
+ stub_request(:get, 'https://kgsearch.googleapis.com/v1/entities:search?ids=/m/05pbsry&key=foo&limit=1')
43
+ .to_return(status: 200, body: File.read('spec/fixtures/getting_entity.json'))
44
+
45
+ expect(GoogleKnowledgeGraph.get '/m/05pbsry').to be_a GoogleKnowledgeGraph::Entity
46
+ end
47
+
48
+ it 'returns nil for bad request' do
49
+ stub_request(:get, /kgsearch.googleapis.com/)
50
+ .to_return(status: 403)
51
+
52
+ expect(GoogleKnowledgeGraph.get 'fakeid').to be_nil
53
+ end
54
+ end
55
+
56
+ describe '#self.search' do
57
+ it 'raises error for empty api_key' do
58
+ GoogleKnowledgeGraph.api_key = nil
59
+
60
+ expect {
61
+ GoogleKnowledgeGraph.search 'some query'
62
+ }.to raise_error 'EmptyAPIKey'
63
+ end
64
+
65
+ it 'returns an empty list for bad request' do
66
+ stub_request(:get, /kgsearch.googleapis.com/)
67
+ .to_return(status: 403)
68
+
69
+ expect(GoogleKnowledgeGraph.search 'some query').to eq []
70
+ end
71
+
72
+ it 'returns an empty list if nothing is found' do
73
+ stub_request(:get, 'https://kgsearch.googleapis.com/v1/entities:search?key=foo&query=somequerydoesnotexist')
74
+ .to_return(status: 200, body: File.read('spec/fixtures/searching_entities_empty.json'))
75
+
76
+ expect(GoogleKnowledgeGraph.search 'somequerydoesnotexist').to eq []
77
+ end
78
+
79
+ it 'returns a list of hash including entity and score' do
80
+ stub_request(:get, 'https://kgsearch.googleapis.com/v1/entities:search?key=foo&query=community')
81
+ .to_return(status: 200, body: File.read('spec/fixtures/searching_entities.json'))
82
+
83
+ expect(GoogleKnowledgeGraph.search 'community').to all include(
84
+ entity: a_kind_of(GoogleKnowledgeGraph::Entity),
85
+ score: be_a(Numeric)
86
+ )
87
+ end
88
+
89
+ it 'provides an option `types` to limit entity types' do
90
+ stub_request(:get, /kgsearch.googleapis.com\/v1\/entities:search/)
91
+ .to_return(status: 200, body: File.read('spec/fixtures/searching_entities.json'))
92
+
93
+ GoogleKnowledgeGraph.search 'community', types: ['TVSeries']
94
+
95
+ expect(
96
+ # not the standard types[]=a&types[]=b format
97
+ a_request(:get, 'https://kgsearch.googleapis.com/v1/entities:search?types=Thing&types=TVSeries')
98
+ .with(query: {
99
+ key: 'foo',
100
+ query: 'community',
101
+ })
102
+ ).to have_been_made.once
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,11 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require 'google_knowledge_graph'
5
+
6
+ require 'webmock/rspec'
7
+ WebMock.disable_net_connect!(allow_localhost: true)
8
+
9
+ # optional config
10
+ # RSpec.configure do |config|
11
+ # end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google_knowledge_graph
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nami WANG
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-06-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: webmock
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: http
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.0'
55
+ description:
56
+ email:
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - ".rspec"
62
+ - Gemfile
63
+ - Gemfile.lock
64
+ - README.md
65
+ - google_knowledge_graph.gemspec
66
+ - lib/google_knowledge_graph.rb
67
+ - lib/google_knowledge_graph/entity.rb
68
+ - lib/google_knowledge_graph/version.rb
69
+ - spec/fixtures/entity_data.json
70
+ - spec/fixtures/getting_entity.json
71
+ - spec/fixtures/searching_entities.json
72
+ - spec/fixtures/searching_entities_empty.json
73
+ - spec/google_knowledge_graph/entity_spec.rb
74
+ - spec/google_knowledge_graph_spec.rb
75
+ - spec/spec_helper.rb
76
+ homepage: https://github.com/ref-wiki/google_knowledge_graph
77
+ licenses:
78
+ - MIT
79
+ metadata:
80
+ source_code_uri: https://github.com/ref-wiki/google_knowledge_graph
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubygems_version: 3.1.2
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Ruby client for Google's Knowledge Graph
100
+ test_files: []