Class: SwissMatch::Directories::TelSearch
- Inherits:
-
Object
- Object
- SwissMatch::Directories::TelSearch
- 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
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
-
.from_config(config) ⇒ Object
Create a TelSearch instance from the common interface.
Instance Method Summary collapse
- #addresses(params, options = {}) ⇒ Object
-
#initialize(key) ⇒ TelSearch
constructor
A new instance of TelSearch.
-
#search_ch_mapping(params) ⇒ Object
Convert the abstracted APIs options to tel.search.ch's search params.
Constructor Details
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
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, = {}) return [] if params.empty? # short-cut # tel.search.ch parameters search_params = {'key' => @key} # Handle pagination parameters per_page = [:per_page] || 10 search_params['maxnum'] = per_page page = [: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
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 |