Class: Jbr::Visit

Inherits:
Resource show all
Defined in:
lib/jbr/visit.rb

Overview

One stop at a property: when the work on a job is scheduled to happen.

Direct Known Subclasses

Mock::Visit

Constant Summary collapse

CLIENT_FIELDS =

What Jobber calls each field of the client the work is for, against what a caller reads.

{ id: :id, firstName: :first_name, lastName: :last_name, email: :email }
UPCOMING =

The query that reads a page of visits starting after a moment, oldest first. Forty a page, not a hundred: Jobber prices a query by its page size and refuses the wider one.

<<~GRAPHQL
  query($after: String, $from: ISO8601DateTime!) {
    visits(first: 40, after: $after, filter: { startAt: { after: $from } }) {
      nodes {
        id title startAt endAt allDay clientConfirmed
        job { id }
        client { #{CLIENT_FIELDS.keys.join ' '} #{Phone::SELECTION} }
        property { id address { #{Property::SELECTION} } }
      }
      pageInfo { hasNextPage endCursor }
    }
  }
GRAPHQL

Instance Method Summary collapse

Constructor Details

#initialize(oauth:, node: {}) ⇒ Visit

Returns a new instance of Visit.

Parameters:

  • oauth (OAuth)

    the credentials to reach Jobber with.

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

    the visit as Jobber answered it.



25
26
27
28
# File 'lib/jbr/visit.rb', line 25

def initialize(oauth:, node: {})
  super oauth: oauth
  @node = node
end

Instance Method Details

#addressHash

Where the work happens, in the fields Property carries.

Returns:

  • (Hash)

    any of :street, :city, :state, :zip, :latitude and :longitude.



66
# File 'lib/jbr/visit.rb', line 66

def address = Property.fields_from @node.dig('property', 'address')

#all_day?Boolean?

Returns whether the visit takes the whole day rather than an hour of it.

Returns:

  • (Boolean, nil)

    whether the visit takes the whole day rather than an hour of it.



49
# File 'lib/jbr/visit.rb', line 49

def all_day? = @node['allDay']

#clientHash

Who the work is for, in the fields a client is created with.

Returns:

  • (Hash)

    any of :id, :first_name, :last_name, :phone and :email.



59
60
61
62
# File 'lib/jbr/visit.rb', line 59

def client
  fields = CLIENT_FIELDS.to_h { |jobber, ours| [ ours, @node.dig('client', jobber.to_s) ] }
  fields.merge(phone: Phone.from(@node.dig('client', 'phones'))).compact
end

#client_confirmed?Boolean?

Returns whether the client has confirmed the visit.

Returns:

  • (Boolean, nil)

    whether the client has confirmed the visit.



52
# File 'lib/jbr/visit.rb', line 52

def client_confirmed? = @node['clientConfirmed']

#ends_atTime?

Returns the visit end time.

Returns:

  • (Time, nil)

    the visit end time



74
75
76
# File 'lib/jbr/visit.rb', line 74

def ends_at
  Time.iso8601(@node['endAt']) if @node['endAt']
end

#idString?

Returns the Jobber ID of the visit.

Returns:

  • (String, nil)

    the Jobber ID of the visit.



40
# File 'lib/jbr/visit.rb', line 40

def id = @node['id']

#job_idString?

Returns the ID of the job the visit belongs to.

Returns:

  • (String, nil)

    the ID of the job the visit belongs to.



46
# File 'lib/jbr/visit.rb', line 46

def job_id = @node.dig 'job', 'id'

#property_idString?

Returns the ID of the property the work happens at.

Returns:

  • (String, nil)

    the ID of the property the work happens at.



55
# File 'lib/jbr/visit.rb', line 55

def property_id = @node.dig 'property', 'id'

#starts_atTime?

Returns the visit start time.

Returns:

  • (Time, nil)

    the visit start time



69
70
71
# File 'lib/jbr/visit.rb', line 69

def starts_at
  Time.iso8601(@node['startAt']) if @node['startAt']
end

#titleString?

Returns what the visit is called.

Returns:

  • (String, nil)

    what the visit is called.



43
# File 'lib/jbr/visit.rb', line 43

def title = @node['title']

#upcomingEnumerator<Visit>

The visits scheduled from now on, oldest first. Nothing is read until the enumerator is walked, and a page is read only once the one before it runs out.

Returns:

  • (Enumerator<Visit>)

    the account's upcoming visits.



33
34
35
36
37
# File 'lib/jbr/visit.rb', line 33

def upcoming
  Enumerator.new do |yielder|
    nodes.each { |node| yielder << self.class.new(oauth: @oauth, node: node) }
  end
end