Class: SwissMatch::TelSearch

Inherits:
DirectoryService show all
Defined in:
lib/swissmatch/telsearch.rb

Overview

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

Constant Summary

NS =
{'t' => 'http://tel.search.ch/api/spec/result/1.0/'}
API_URI =
URI.parse('http://tel.search.ch/api')

Instance Method Summary (collapse)

Constructor Details

- (TelSearch) initialize(key)

Returns a new instance of TelSearch



21
22
23
24
# File 'lib/swissmatch/telsearch.rb', line 21

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

Instance Method Details

- (Object) addresses(params)



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/swissmatch/telsearch.rb', line 33

def addresses(params)
  uri       = @uri.dup
  uri.query = URI.encode_www_form(search_ch_mapping(params).merge('key' => @key))
  feed      = Nokogiri.XML(open(uri, &:read))

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

- (Object) search_ch_mapping(params)



26
27
28
29
30
31
# File 'lib/swissmatch/telsearch.rb', line 26

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