class Gnfinder::Client

Gnfinder::Client connects to gnfinder server

Public Class Methods

new(host = 'https://gnfinder.globalnames.org', port = '') click to toggle source
# File lib/gnfinder/client.rb, line 6
def initialize(host = 'https://gnfinder.globalnames.org', port = '')
  api_path = '/api/v0'
  url = host + api_path
  url = "#{host}:#{port}#{api_path}" if port.to_s != ''
  @site = RestClient::Resource.new(url, read_timeout: 60)
  @port = port
end

Public Instance Methods

find_file(path, opts = {}) click to toggle source
# File lib/gnfinder/client.rb, line 24
def find_file(path, opts = {})
  params = {}
  update_params(params, opts)
  file = File.new(path, 'rb')
  params = params.merge(file: file)
  resp = @site['find'].post(params)
  prepare_result(resp)
end
find_names(text, opts = {}) click to toggle source
# File lib/gnfinder/client.rb, line 40
def find_names(text, opts = {})
  return to_open_struct({ "names": [] }) if text.to_s.strip == ''

  params = { text: text }
  find(params, opts)
end
find_url(url, opts = {}) click to toggle source
# File lib/gnfinder/client.rb, line 33
def find_url(url, opts = {})
  return to_open_struct({ "names": [] }) if url.to_s.strip == ''

  params = { url: url }
  find(params, opts)
end
gnfinder_version() click to toggle source
# File lib/gnfinder/client.rb, line 14
def gnfinder_version
  resp = @site['/version'].get
  ver = JSON.parse(resp.body, symbolize_names: true)
  OpenStruct.new(ver)
end
ping() click to toggle source
# File lib/gnfinder/client.rb, line 20
def ping
  @site['/ping'].get.body
end

Private Instance Methods

find(params, opts = {}) click to toggle source

rubocop:disable all

# File lib/gnfinder/client.rb, line 50
def find(params, opts = {})
  update_params(params, opts)

  resp = @site['find'].post params.to_json, {content_type: :json, accept: :json}
  prepare_result(resp)
end
prepare_result(response) click to toggle source

rubocop:enable all

# File lib/gnfinder/client.rb, line 58
def prepare_result(response)
  output = JSON.parse(response.body)
  res = output['metadata']
  res['names'] = output['names'] || []
  res = res.deep_transform_keys(&:underscore)
  res['names'] = [] if res['names'].nil?
  to_open_struct(res)
end
to_open_struct(obj) click to toggle source

rubocop:enable all

# File lib/gnfinder/client.rb, line 81
def to_open_struct(obj)
  case obj
  when Hash
    OpenStruct.new(obj.transform_values { |val| to_open_struct(val) })
  when Array
    obj.map { |o| to_open_struct(o) }
  else # Assumed to be a primitive value
    obj
  end
end
update_params(params, opts) click to toggle source

rubocop:disable all

# File lib/gnfinder/client.rb, line 68
def update_params(params, opts)
  params[:noBayes] = true if opts[:no_bayes]
  params[:oddsDetails] = true if opts[:odds_details]
  params[:language] = opts[:language] if opts[:language].to_s.strip != ''

  params[:wordsAround] = opts[:words_around] if opts[:words_around] && opts[:words_around].positive?

  params[:verification] = true if opts[:verification]

  params[:sources] = opts[:sources] if opts[:sources] && !opts[:sources].empty?
end