Class: TocDoc::Profile

Inherits:
Resource show all
Defined in:
lib/toc_doc/models/profile.rb,
lib/toc_doc/models/profile/organization.rb,
lib/toc_doc/models/profile/practitioner.rb

Overview

Represents a search profile result (practitioner or organization). Inherits dot-notation attribute access from +TocDoc::Resource+.

Use +Profile.build+ to obtain the correctly typed subclass instance.

Examples:

profile = TocDoc::Profile.build('owner_type' => 'Account', 'name' => 'Dr Smith')
profile.class          #=> TocDoc::Profile::Practitioner
profile.practitioner?  #=> true
profile.name           #=> "Dr Smith"

Direct Known Subclasses

Organization, Practitioner

Defined Under Namespace

Classes: Organization, Practitioner

Constant Summary collapse

PATH =

API path template for a full profile page (+sprintf+-style, requires +identifier+).

Returns:

  • (String)
'/profiles/%<identifier>s.json'

Instance Attribute Summary

Attributes inherited from Resource

#attrs

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#==, #[], #[]=, #attribute_names, #initialize, #inspect, main_attrs, #method_missing, normalize_attrs, #to_h, #to_json

Constructor Details

This class inherits a constructor from TocDoc::Resource

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class TocDoc::Resource

Class Method Details

.build(attrs = {}) ⇒ Profile::Practitioner, Profile::Organization

Factory — returns a +Profile::Practitioner+ or +Profile::Organization+.

Resolves type via +owner_type+ first (autocomplete context), then falls back to the boolean flags present on profile-page responses.

Examples:

Build from an autocomplete result

TocDoc::Profile.build('owner_type' => 'Account', 'name' => 'Dr Smith')

Build from a full profile response

TocDoc::Profile.build('is_practitioner' => true, 'name' => 'Dr Smith')

Parameters:

  • attrs (Hash) (defaults to: {})

    raw attribute hash from the API response

Returns:

Raises:

  • (ArgumentError)

    if +attrs+ contains an unknown +owner_type+ or the profile type cannot be determined from the available flags



36
37
38
39
40
41
42
43
44
# File 'lib/toc_doc/models/profile.rb', line 36

def build(attrs = {})
  attrs = normalize_attrs(attrs)

  return find(attrs['value']) if attrs['force_full_profile']

  build_from_autocomplete(attrs) ||
    build_from_booking_info(attrs) ||
    build_from_full_profile(attrs)
end

.find(identifier) ⇒ Profile::Practitioner, Profile::Organization

Fetches a full profile page by slug or numeric ID.

Examples:

TocDoc::Profile.find('jane-doe-bordeaux')
TocDoc::Profile.find(1542899)

Parameters:

  • identifier (String, Integer)

    profile slug or numeric ID

Returns:

Raises:

  • (ArgumentError)

    if +identifier+ is +nil+



55
56
57
58
59
60
# File 'lib/toc_doc/models/profile.rb', line 55

def find(identifier)
  raise ArgumentError, 'identifier cannot be nil' if identifier.nil?

  data = TocDoc.client.get(format(PATH, identifier: identifier))['data']
  build(profile_attrs(data))
end

Instance Method Details

#idInteger, ...

Returns the profile ID, falling back to the +value+ key used in autocomplete responses when +id+ is absent.

Returns:

  • (Integer, String, nil)


173
174
175
# File 'lib/toc_doc/models/profile.rb', line 173

def id
  self['id'] || self['value']
end

#load_full_profile!self

Replaces this profile's attributes with those from the full profile page, making a network request only when the profile is currently partial. Returns +self+ for chaining.

Returns:

  • (self)


182
183
184
185
186
187
188
189
# File 'lib/toc_doc/models/profile.rb', line 182

def load_full_profile!
  return unless partial

  full_profile = self.class.find(id)
  @attrs = full_profile.instance_variable_get(:@attrs)
  @partial = false
  self
end

#organization?Boolean

Returns true when this profile is an organization.

Examples:

profile.organization?  #=> false

Returns:

  • (Boolean)

    true when this profile is an organization



165
166
167
# File 'lib/toc_doc/models/profile.rb', line 165

def organization?
  is_a?(Organization)
end

#practitioner?Boolean

Returns true when this profile is a practitioner.

Examples:

profile.practitioner?  #=> true

Returns:

  • (Boolean)

    true when this profile is a practitioner



157
158
159
# File 'lib/toc_doc/models/profile.rb', line 157

def practitioner?
  is_a?(Practitioner)
end

#skillsArray<TocDoc::Resource>

Returns all skills across all practices as an array of Resource.

Examples:

profile.skills  #=> [#<TocDoc::Resource ...>, ...]

Returns:



136
137
138
139
# File 'lib/toc_doc/models/profile.rb', line 136

def skills
  hash = self['skills_by_practice'] || {}
  hash.values.flatten.map { |s| TocDoc::Resource.new(s) }
end

#skills_for(practice_id) ⇒ Array<TocDoc::Resource>

Returns skills for a single practice as an array of Resource.

Examples:

profile.skills_for(123)  #=> [#<TocDoc::Resource ...>, ...]

Parameters:

  • practice_id (Integer, String)

    the practice ID

Returns:



148
149
150
151
# File 'lib/toc_doc/models/profile.rb', line 148

def skills_for(practice_id)
  hash = self['skills_by_practice'] || {}
  Array(hash[practice_id.to_s]).map { |s| TocDoc::Resource.new(s) }
end