Class: Jbr::Property
- Includes:
- Cliental
- Defined in:
- lib/jbr/property.rb
Overview
Where the work happens: one address on a client's file.
Direct Known Subclasses
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
-
#address ⇒ Hash
Where the place is, in the fields a caller passes to open one.
-
#city ⇒ String?
The town the work happens in.
-
#find_or_create_for(client_id:, address:, existing: []) ⇒ String?
Reach the property at an address, adding one when none of the client's matches.
-
#street ⇒ String?
The street the work happens on.
-
#zip ⇒ String?
The postal code the work happens in.
Methods included from Cliental
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.
46 47 48 |
# File 'lib/jbr/property.rb', line 46 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.
54 55 56 57 58 59 |
# File 'lib/jbr/property.rb', line 54 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
#address ⇒ Hash
Where the place is, in the fields a caller passes to open one.
27 |
# File 'lib/jbr/property.rb', line 27 def address = Property.fields_from @node['address'] |
#city ⇒ String?
Returns the town the work happens in.
33 |
# File 'lib/jbr/property.rb', line 33 def city = address[:city] |
#find_or_create_for(client_id:, address:, existing: []) ⇒ String?
Reach the property at an address, adding one when none of the client's matches.
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/jbr/property.rb', line 66 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 |
#street ⇒ String?
Returns the street the work happens on.
30 |
# File 'lib/jbr/property.rb', line 30 def street = address[:street] |
#zip ⇒ String?
Returns the postal code the work happens in.
36 |
# File 'lib/jbr/property.rb', line 36 def zip = address[:zip] |