Sha256: 4c3fd7beb8a3209ad932193db39c0af146503abc04fe4c4f1a49e7617fd621f8

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

module MyJohnDeere
  class Field < OrganizationOwnedResource
    self.base_jd_resource = "fields"
    self.list_resource_path = "organizations/%{organization_id}/#{self.base_jd_resource}"
    self.retrieve_resource_path = "organizations/%{organization_id}/#{self.base_jd_resource}"
    attributes_to_pull_from_json(:id, :name, :boundaries)

    def initialize(json_object, access_token = nil)
      @boundary = nil
      super(json_object, access_token)
      boundaries = json_object["boundaries"]
      if boundaries && boundaries.length > 0 then
        # If we embed, then we'll need to pass our id
        possible_boundaries = boundaries.map { |b_json| Boundary.new(b_json, access_token, self.id) }
        self.boundary = find_first_active_boundary(possible_boundaries)
      end
    end

    # Will return whether or not the boundary has been set,
    # useful if you're expecting embedded boundaries
    def boundary_unset?
      return @boundary.nil?
    end

    def boundary
      if self.boundary_unset? then
        boundaries = Boundary.list(self.access_token, field_id: self.id, organization_id: self.organization_id)
        @boundary = find_first_active_boundary(boundaries.data)
      end
      return @boundary
    end

    def boundary=(val)
      @boundary = val
    end

    private
      def find_first_active_boundary(possible_boundaries)
        active_boundaries = possible_boundaries.select { |b| b.active && !b.deleted }.
          uniq { |b| b.id }
        if active_boundaries.count > 1 then
          raise MyJohnDeereError.new("There was more than one boundary in the field ID: #{self.id}, this is currently unexpected")
        elsif active_boundaries.count == 1 then
          return active_boundaries.first
        else
          return possible_boundaries.first
        end
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
myjohndeere-0.1.8 lib/myjohndeere/field.rb