Class: MyJohnDeereApi::Request::Create::Base
- Inherits:
-
Object
- Object
- MyJohnDeereApi::Request::Create::Base
- Defined in:
- lib/my_john_deere_api/request/create/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
-
#accessor ⇒ Object
client accessor.
-
#initialize(client, attributes) ⇒ Base
constructor
Accepts a valid oAuth AccessToken, and a hash of attributes.
-
#object ⇒ Object
Object created by request.
-
#request ⇒ Object
Make the request, if the instance is valid.
-
#valid? ⇒ Boolean
Runs validations, adding to the errors hash as needed.
-
#validate! ⇒ Object
Raises an error if the record is invalid.
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
#attributes ⇒ Object (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 |
#client ⇒ Object (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 |
#response ⇒ Object (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
#accessor ⇒ Object
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 |
#object ⇒ Object
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 |
#request ⇒ Object
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.
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.
62 63 64 |
# File 'lib/my_john_deere_api/request/create/base.rb', line 62 def validate! raise(InvalidRecordError, errors) unless valid? end |