Class: TocDoc::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/toc_doc/models/search.rb,
lib/toc_doc/models/search/result.rb

Overview

Entry point for the autocomplete / search endpoint.

Unlike Availability, +Search+ is not itself a resource — it is a plain service class that wraps the API call and returns a typed result.

Examples:

Fetch everything

result = TocDoc::Search.where(query: 'dentiste')
result          #=> #<TocDoc::Search::Result>
result.profiles #=> [#<TocDoc::Profile::Practitioner>, ...]

Filter by type

TocDoc::Search.where(query: 'dentiste', type: 'practitioner')
#=> [#<TocDoc::Profile::Practitioner>, ...]

Defined Under Namespace

Classes: Result

Constant Summary collapse

PATH =

API path for the autocomplete / searchbar endpoint.

Returns:

  • (String)
'/api/searchbar/autocomplete.json'
VALID_TYPES =

Accepted values for the +type:+ keyword in where.

Returns:

  • (Array<String>)
%w[profile practitioner organization speciality].freeze

Class Method Summary collapse

Class Method Details

.where(query:, type: nil, **options) ⇒ Search::Result, ...

Queries the autocomplete endpoint and returns a Result or a filtered array.

The +type:+ keyword is handled client-side only — it is never forwarded to the API. The full response is always fetched; narrowing happens after.

Examples:

TocDoc::Search.where(query: 'derma', type: 'speciality')
#=> [#<TocDoc::Speciality name="Dermatologue">, ...]

Parameters:

  • query (String)

    the search term

  • type (String, nil) (defaults to: nil)

    optional filter; one of +'profile'+, +'practitioner'+, +'organization'+, +'speciality'+

  • options (Hash)

    additional query params forwarded verbatim to the API

Returns:

Raises:

  • (ArgumentError)

    if +type:+ is not +nil+ and not in VALID_TYPES



48
49
50
51
52
53
54
55
56
57
# File 'lib/toc_doc/models/search.rb', line 48

def where(query:, type: nil, **options)
  if !type.nil? && !VALID_TYPES.include?(type)
    raise ArgumentError, "Invalid type #{type.inspect}. Must be one of: #{VALID_TYPES.join(', ')}"
  end

  data   = TocDoc.client.get(PATH, query: { search: query, **options })
  result = Result.new(data)

  type.nil? ? result : result.filter_by_type(type)
end