Class: MyJohnDeereApi::Request::Create::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/my_john_deere_api/request/create/base.rb

Direct Known Subclasses

Asset, AssetLocation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, attributes) ⇒ Base

Accepts a valid oAuth AccessToken, and a hash of attributes.



10
11
12
13
14
15
# File 'lib/my_john_deere_api/request/create/base.rb', line 10

def initialize(client, attributes)
  @client = client
  @attributes = attributes

  process_attributes
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



5
6
7
# File 'lib/my_john_deere_api/request/create/base.rb', line 5

def attributes
  @attributes
end

#clientObject (readonly)

Returns the value of attribute client.



5
6
7
# File 'lib/my_john_deere_api/request/create/base.rb', line 5

def client
  @client
end

#responseObject (readonly)

Returns the value of attribute response.



5
6
7
# File 'lib/my_john_deere_api/request/create/base.rb', line 5

def response
  @response
end

Instance Method Details

#accessorObject

client accessor



20
21
22
23
# File 'lib/my_john_deere_api/request/create/base.rb', line 20

def accessor
  return @accessor if defined?(@accessor)
  @accessor = client&.accessor
end

#objectObject

Object created by request



37
38
39
40
41
42
43
# File 'lib/my_john_deere_api/request/create/base.rb', line 37

def object
  return @object if defined?(@object)

  request unless response

  @object = model.new(client, fetch_record)
end

#requestObject

Make the request, if the instance is valid



28
29
30
31
32
# File 'lib/my_john_deere_api/request/create/base.rb', line 28

def request
  validate!

  @response = accessor.post(resource, request_body.to_json, headers)
end

#valid?Boolean

Runs validations, adding to the errors hash as needed. Returns true if the errors hash is still empty after all validations have been run.

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
# File 'lib/my_john_deere_api/request/create/base.rb', line 49

def valid?
  return @is_valid if defined?(@is_valid)

  validate_required
  validate_attributes

  @is_valid = errors.empty?
end

#validate!Object

Raises an error if the record is invalid. Passes the errors hash to the error, in order to build a useful message string.

Raises:



62
63
64
# File 'lib/my_john_deere_api/request/create/base.rb', line 62

def validate!
  raise(InvalidRecordError, errors) unless valid?
end