Class: Kasabi::Reconcile::Client

Inherits:
BaseClient show all
Defined in:
lib/kasabi/api/reconcile.rb

Instance Attribute Summary

Attributes inherited from BaseClient

#apikey, #client, #endpoint

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseClient

#client_options, #get, #post, #validate_response

Constructor Details

#initialize(endpoint, options = {}) ⇒ Client

Initialize the client to work with a specific endpoint

The options hash can contain the following values:

  • :apikey: required. apikey authorized to use the API

  • :client: HTTPClient object instance



12
13
14
# File 'lib/kasabi/api/reconcile.rb', line 12

def initialize(endpoint, options={})
  super(endpoint, options)
end

Class Method Details

.make_property_filter(value, name = nil, id = nil) ⇒ Object

Construct a property filter

A property name or identifier must be specified. Both are legal but it is up to the service to decide which one it uses. Some services may have restrictions on whether they support names or identifiers.

value:: a single value, or an array of string or number or object literal, e.g., "Japan"
name::  string, property name, e.g., "country"
id:: string, property ID, e.g., "/people/person/nationality" in the Freebase ID space


108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/kasabi/api/reconcile.rb', line 108

def Client.make_property_filter(value, name=nil, id=nil)
  if name == nil and id == nil
    raise "Must specify at least a property name or property identifier"
  end
  
  filter = Hash.new
  filter[:v] = value
  filter[:p] = name if name != nil
  filter[:pid] = id if id != nil
    
  return filter
end

.make_queries(labels, limit = 3, type_strict = :any, type = nil, properties = nil) ⇒ Object

Make an array of reconciliation queries using a standard set of options for limiting, type matching and property filtering

label

text to reconcile on

limit

limit number of results, default is 3

type_strict

how to perform type matching, legal values are :any (default), :all, :should

type

string identifier of type, or array of string identifiers

properties

property filters, see make_property_filter



72
73
74
75
76
77
78
# File 'lib/kasabi/api/reconcile.rb', line 72

def Client.make_queries(labels, limit=3, type_strict=:any, type=nil, properties=nil)
  queries = []
  labels.each do |label|
    queries << Client.make_query(label, limit, type_strict, type, properties)
  end
  return queries
end

.make_query(label, limit = 3, type_strict = :any, type = nil, properties = nil) ⇒ Object

Make a reconciliation query

label

text to reconcile on

limit

limit number of results, default is 3

type_strict

how to perform type matching, legal values are :any (default), :all, :should

type

string identifier of type, or array of string identifiers

properties

property filters, see make_property_filter



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/kasabi/api/reconcile.rb', line 87

def Client.make_query(label, limit=3, type_strict=:any, type=nil, properties=nil)
  query = Hash.new
  query[:query] = label
  query[:limit] = limit
  query[:type_strict] = type_strict
            
  query[:type] = type if type != nil
  query[:properties] = properties if properties != nil
  
  return query
end

Instance Method Details

#reconcile(query, &block) ⇒ Object

Full reconciliation request, allows specifying of additional parameters

Returns the array of results from the reconciliation request

Accepts a block to support iteration through the results. Method will yield each result and its index.



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/kasabi/api/reconcile.rb', line 27

def reconcile(query, &block)
  response = get( @endpoint, {"query" => query.to_json } )
  validate_response(response)
  results = JSON.parse( response.content )
  if results["result"] && block_given?
    results["result"].each_with_index do |r, i|
      yield r, i
    end
  end
  return results["result"]        
end

#reconcile_all(queries, &block) ⇒ Object

Perform a number of reconciliation queries Submits a single request with a number of queries

Accepts a block to support iteration through the results. Method will yield each result, its index, and the query



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/kasabi/api/reconcile.rb', line 44

def reconcile_all(queries, &block)        
  #TODO add batching
  #TODO make parallel?
  json = {}
  queries.each_with_index do |query, i|
    json["q#{i}"] = query
  end
  response = get( @endpoint, {"queries" => json.to_json } )
  validate_response(response)
  results = JSON.parse( response.content )
  if block_given?
    queries.each_with_index do |query, i|
      if results["q#{i}"]
        yield results["q#{i}"]["result"], i, query
      end
    end
  end
  return results         
end

#reconcile_label(label, &block) ⇒ Object

Simple reconciliation request



17
18
19
# File 'lib/kasabi/api/reconcile.rb', line 17

def reconcile_label(label, &block)
  return reconcile( Client.make_query(label), &block )
end