Module: GoogleKnowledgeGraph

Defined in:
lib/google_knowledge_graph.rb,
lib/google_knowledge_graph/entity.rb,
lib/google_knowledge_graph/version.rb

Defined Under Namespace

Classes: Entity

Constant Summary collapse

API_URL =
'https://kgsearch.googleapis.com/v1/entities:search'
VERSION =
'0.1.3'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_keyObject

Returns the value of attribute api_key.



11
12
13
# File 'lib/google_knowledge_graph.rb', line 11

def api_key
  @api_key
end

Class Method Details

.get(id) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/google_knowledge_graph.rb', line 13

def get id
  ensure_api_key!
  id.delete_prefix! 'kg:'

  response = request(
    key: api_key,
    limit: 1,
    ids: [id]
  )

  return nil unless response.status.success?
  return nil unless data = JSON.parse(response.body).dig('itemListElement', 0, 'result')

  Entity.new(data)
end

.search(query, types: []) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/google_knowledge_graph.rb', line 29

def search query, types: []
  ensure_api_key!

  response = request(
    key: api_key,
    query: query,
    types: types,
  )

  return [] unless response.status.success?

  JSON.parse(response.body)['itemListElement']
    .map{ |item| {
      entity: Entity.new(item['result']),
      score: item['resultScore']
    }}
end