Class: Jbr::Property
Overview
Where the work happens: one address on a client's file.
Constant Summary collapse
- CREATE =
The mutation that adds a property to a client already on file.
<<~GRAPHQL mutation propertyCreateMutation($clientId: EncodedId!, $input: PropertyCreateInput!) { propertyCreate(clientId: $clientId, input: $input) { properties { id } userErrors { message } } } GRAPHQL
- FIELDS =
What Jobber calls each address field, against what a caller passes.
{ street1: :street, city: :city, province: :state, postalCode: :zip }
- COORDINATES =
The two Jobber tucks inside the address and a property carries beside the street.
%i[latitude longitude]
- SELECTION =
The address as Jobber answers it, asked for wherever a property is read.
"#{FIELDS.keys.join ' '} coordinates { #{COORDINATES.join ' '} }"- MATCHED =
The fields a match is made on. City and state are written but never matched: Jobber holds whatever was typed, so "NC" and "North Carolina" -- or "Winston Salem" and "Winston-Salem" -- would read as two homes. The ZIP already places the home.
%i[street1 postalCode]
Instance Attribute Summary
Attributes inherited from Resource
Class Method Summary collapse
-
.address_from(fields = {}) ⇒ Hash
The address as Jobber takes it, from the fields a caller passes.
-
.fields_from(address) ⇒ Hash
The fields a caller reads, from the address as Jobber holds it.
Instance Method Summary collapse
-
#find_or_create_for(client_id:, address:, existing: []) ⇒ String?
Reach the property at an address, adding one when none of the client's matches.
Methods inherited from Resource
Constructor Details
This class inherits a constructor from Jbr::Resource
Class Method Details
.address_from(fields = {}) ⇒ Hash
The address as Jobber takes it, from the fields a caller passes.
31 32 33 |
# File 'lib/jbr/property.rb', line 31 def self.address_from(fields = {}) FIELDS.to_h { |jobber, ours| [ jobber, fields[ours] ] }.compact end |
.fields_from(address) ⇒ Hash
The fields a caller reads, from the address as Jobber holds it.
39 40 41 42 43 44 |
# File 'lib/jbr/property.rb', line 39 def self.fields_from(address) address ||= {} coordinates = address['coordinates'] || {} FIELDS.to_h { |jobber, ours| [ ours, address[jobber.to_s] ] }. merge(COORDINATES.to_h { |ours| [ ours, coordinates[ours.to_s] ] }).compact end |
Instance Method Details
#find_or_create_for(client_id:, address:, existing: []) ⇒ String?
Reach the property at an address, adding one when none of the client's matches.
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/jbr/property.rb', line 51 def find_or_create_for(client_id:, address:, existing: []) wanted = self.class.address_from address match = existing.find { |property| same_address? wanted, property['address'] } return match['id'] if match output = @oauth.query CREATE, variables: { clientId: client_id, input: { properties: [ { address: wanted } ] }, } (output&.dig('propertyCreate', 'properties')&.first || {})['id'] end |