class MyJohnDeereApi::Request::Create::AssetLocation

Public Instance Methods

object() click to toggle source

Object created by request

There is no endpoint to fetch a single location by id, so we have to override the `object` method from the base class.

We have to fetch locations in bulk via the asset, but there could be thousands. We limit the request to just the first record from the location list endpoint, since locations are returned newest to oldest.

# File lib/my_john_deere_api/request/create/asset_location.rb, line 18
def object
  return @object if defined?(@object)

  request unless response

  path = uri_path(response.headers['location']) + '?count=1'
  result = client.get(path)
  record = result['values'].first

  @object = Model::AssetLocation.new(client, record)
end

Private Instance Methods

geometry_from_coordinates() click to toggle source

Convert just coordinates into valid geometry hash

# File lib/my_john_deere_api/request/create/asset_location.rb, line 81
def geometry_from_coordinates
  {
    type: 'Feature',
    geometry: {
      geometries: [
        coordinates: attributes[:coordinates],
        type: 'Point'
      ],
      type: 'GeometryCollection'
    }
  }.to_json
end
process_attributes() click to toggle source

Set defaults and generate some attributes from others. Overridden from parent class.

# File lib/my_john_deere_api/request/create/asset_location.rb, line 36
def process_attributes
  process_timestamp
  process_geometry
end
process_geometry() click to toggle source

Custom-process geometry

# File lib/my_john_deere_api/request/create/asset_location.rb, line 68
def process_geometry
  attributes[:geometry] = if attributes[:geometry]
    attributes[:geometry].is_a?(String) ?
      attributes[:geometry] :
      attributes[:geometry].to_json
  elsif attributes[:coordinates]
    geometry_from_coordinates
  end
end
process_timestamp() click to toggle source

Custom-process timestamp

# File lib/my_john_deere_api/request/create/asset_location.rb, line 57
def process_timestamp
  attributes[:timestamp] ||= Time.now.utc

  attributes[:timestamp] = attributes[:timestamp].is_a?(String) ?
    attributes[:timestamp] :
    attributes[:timestamp].strftime('%Y-%m-%dT%H:%M:%SZ')
end
request_body() click to toggle source

Request body

# File lib/my_john_deere_api/request/create/asset_location.rb, line 44
def request_body
  return @body if defined?(@body)

  @body = [{
    timestamp: attributes[:timestamp],
    geometry: attributes[:geometry],
    measurementData: attributes[:measurement_data]
  }]
end
resource() click to toggle source

Path supplied to API

# File lib/my_john_deere_api/request/create/asset_location.rb, line 97
def resource
  @resource ||= "/platform/assets/#{attributes[:asset_id]}/locations"
end
timestamp_add(timestamp, seconds) click to toggle source

Create a new timestamp adjusted by X minutes

# File lib/my_john_deere_api/request/create/asset_location.rb, line 104
def timestamp_add(timestamp, seconds)
  stamp = DateTime.parse(timestamp).to_time + seconds
  stamp.to_datetime.strftime('%Y-%m-%dT%H:%M:%SZ')
end