Class: SwissMatch::Directories::TelSearch

Inherits:
Object
  • Object
show all
Defined in:
lib/swissmatch/directories/telsearch.rb

Overview

A Directory service, using tel.search.ch's API. Also see http://tel.search.ch/api/help

You need an API key in order to use this library. You can get one on http://admin.tel.search.ch/api/getkey

See Also:

Constant Summary collapse

NS =

XML Namespaces, used to parse the data returned by tel.search.ch

{'t' => 'http://tel.search.ch/api/spec/result/1.0/'}
API_URI =

The url of the API of tel.search.ch

URI.parse('http://tel.search.ch/api')

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ TelSearch

Returns a new instance of TelSearch

Parameters:


41
42
43
44
# File 'lib/swissmatch/directories/telsearch.rb', line 41

def initialize(key)
  @key = key
  @uri = API_URI
end

Class Method Details

.from_config(config) ⇒ Object

Create a TelSearch instance from the common interface. TelSearch requires the :api_token option to be set.


26
27
28
# File 'lib/swissmatch/directories/telsearch.rb', line 26

def self.from_config(config)
  new(config.fetch(:api_token))
end

Instance Method Details

#addresses(params, options = {}) ⇒ Object

See Also:

  • SwissMatch::Directories#addresses

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/swissmatch/directories/telsearch.rb', line 59

def addresses(params, options = {})
  return [] if params.empty? # short-cut

  # tel.search.ch parameters
  search_params = {'key' => @key}

  # Handle pagination parameters
  per_page = options[:per_page] || 10
  search_params['maxnum'] = per_page

  page = options[:page] || 1
  pos = (page - 1) * per_page + 1
  search_params['pos'] = pos

  # Request
  uri       = @uri.dup
  uri.query = URI.encode_www_form(search_ch_mapping(params).merge(search_params))
  feed      = Nokogiri.XML(open(uri, &:read))

  # Parse result
  feed.css('entry').map { |entry|
    Address.new(
      nil,
      extract(entry, 't|firstname'),
      extract(entry, 't|name'),
      extract(entry, 't|maidenname'),
      extract(entry, 't|street'),
      extract(entry, 't|streetno'),
      extract(entry, 't|zip', &:to_i),
      extract(entry, 't|city'),
    )
  }
end

#search_ch_mapping(params) ⇒ Object

Convert the abstracted APIs options to tel.search.ch's search params

Parameters:

  • params (Hash)

    The abstract API search parameters


51
52
53
54
55
56
# File 'lib/swissmatch/directories/telsearch.rb', line 51

def search_ch_mapping(params)
  {
    :was => params.values_at(:first_name, :family_name, :last_name, :maiden_name, :phone).compact.join(' '),
    :wo  => params.values_at(:street, :street_name, :street_number, :zip_code, :city).compact.join(' '),
  }.reject { |k,v| v.empty? }
end