Class: EasyPost::EasyPostObject
- Inherits:
-
Object
- Object
- EasyPost::EasyPostObject
- Includes:
- Enumerable
- Defined in:
- lib/easypost/object.rb
Overview
The EasyPostObject is extended by the EasyPost Resource object.
Direct Known Subclasses
Constant Summary collapse
- @@immutable_values =
rubocop:disable Style/ClassVars
Set.new([:api_key, :id])
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#name ⇒ Object
Returns the value of attribute name.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#unsaved_values ⇒ Object
Returns the value of attribute unsaved_values.
Class Method Summary collapse
-
.construct_from(values, api_key = nil, parent = nil, name = nil) ⇒ Object
Construct an object from values.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Get element of an array.
-
#[]=(key, value) ⇒ Object
Set the element of an array.
-
#as_json(_options = {}) ⇒ Object
Get values as JSON.
-
#deconstruct_keys(_keys) ⇒ Object
Deconstruct the keys of an object.
-
#each(&blk) ⇒ Object
Get each element of values.
-
#id ⇒ Object
Get the ID of an object.
-
#id=(id) ⇒ Object
Set the ID of an object.
-
#initialize(id = nil, api_key = nil, parent = nil, name = nil) ⇒ EasyPostObject
constructor
Initialize an EasyPostObject.
-
#inspect ⇒ Object
Inspect JSON.
-
#keys ⇒ Object
Keys of an object.
-
#refresh_from(values, api_key) ⇒ Object
Refresh an object from the API.
-
#to_hash ⇒ Object
Make values a hash.
-
#to_json(_options = {}) ⇒ Object
Make values JSON.
-
#to_s(*_args) ⇒ Object
Convert to a string.
-
#values ⇒ Object
Values of an object.
Constructor Details
#initialize(id = nil, api_key = nil, parent = nil, name = nil) ⇒ EasyPostObject
Initialize an EasyPostObject.
14 15 16 17 18 19 20 21 22 |
# File 'lib/easypost/object.rb', line 14 def initialize(id = nil, api_key = nil, parent = nil, name = nil) @api_key = api_key @values = {} @unsaved_values = Set.new @transient_values = Set.new @parent = parent @name = name self.id = id if id end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
9 10 11 |
# File 'lib/easypost/object.rb', line 9 def api_key @api_key end |
#name ⇒ Object
Returns the value of attribute name.
9 10 11 |
# File 'lib/easypost/object.rb', line 9 def name @name end |
#parent ⇒ Object
Returns the value of attribute parent.
9 10 11 |
# File 'lib/easypost/object.rb', line 9 def parent @parent end |
#unsaved_values ⇒ Object
Returns the value of attribute unsaved_values.
9 10 11 |
# File 'lib/easypost/object.rb', line 9 def unsaved_values @unsaved_values end |
Class Method Details
.construct_from(values, api_key = nil, parent = nil, name = nil) ⇒ Object
Construct an object from values.
25 26 27 28 29 |
# File 'lib/easypost/object.rb', line 25 def self.construct_from(values, api_key = nil, parent = nil, name = nil) obj = new(values[:id], api_key, parent, name) obj.refresh_from(values, api_key) obj end |
Instance Method Details
#[](key) ⇒ Object
Get element of an array.
67 68 69 |
# File 'lib/easypost/object.rb', line 67 def [](key) @values[key.to_s] end |
#[]=(key, value) ⇒ Object
Set the element of an array.
72 73 74 |
# File 'lib/easypost/object.rb', line 72 def []=(key, value) send(:"#{key}=", value) end |
#as_json(_options = {}) ⇒ Object
Get values as JSON.
92 93 94 |
# File 'lib/easypost/object.rb', line 92 def as_json( = {}) @values.as_json end |
#deconstruct_keys(_keys) ⇒ Object
Deconstruct the keys of an object.
102 103 104 |
# File 'lib/easypost/object.rb', line 102 def deconstruct_keys(_keys) @values.transform_keys(&:to_sym) end |
#each(&blk) ⇒ Object
Get each element of values.
107 108 109 |
# File 'lib/easypost/object.rb', line 107 def each(&blk) @values.each(&blk) end |
#id ⇒ Object
Get the ID of an object.
117 118 119 |
# File 'lib/easypost/object.rb', line 117 def id @values[:id] end |
#id=(id) ⇒ Object
Set the ID of an object.
112 113 114 |
# File 'lib/easypost/object.rb', line 112 def id=(id) @values[:id] = id end |
#inspect ⇒ Object
Inspect JSON.
37 38 39 40 |
# File 'lib/easypost/object.rb', line 37 def inspect id_string = respond_to?(:id) && !id.nil? ? " id=#{id}" : '' "#<#{self.class}:#{id_string}> JSON: " + to_json end |
#keys ⇒ Object
Keys of an object.
77 78 79 |
# File 'lib/easypost/object.rb', line 77 def keys @values.keys end |
#refresh_from(values, api_key) ⇒ Object
Refresh an object from the API.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/easypost/object.rb', line 43 def refresh_from(values, api_key) @api_key = api_key added = Set.new(values.keys - @values.keys) instance_eval do add_accessors(added) end # IDs should never change. But to be safe, only delete if they are different. @values.delete(:id) if values[:id] != @values[:id] values.each do |k, v| @values[k.to_s] = EasyPost::Util.convert_to_easypost_object(v, api_key, self, k) @transient_values.delete(k) @unsaved_values.delete(k) end return if @values[:id] || (values[:id].nil? && values['id'].nil?) @values[:id] = values[:id] || values['id'] end |
#to_hash ⇒ Object
Make values a hash.
97 98 99 |
# File 'lib/easypost/object.rb', line 97 def to_hash @values end |
#to_json(_options = {}) ⇒ Object
Make values JSON.
87 88 89 |
# File 'lib/easypost/object.rb', line 87 def to_json( = {}) JSON.dump(@values) end |
#to_s(*_args) ⇒ Object
Convert to a string.
32 33 34 |
# File 'lib/easypost/object.rb', line 32 def to_s(*_args) JSON.dump(@values) end |
#values ⇒ Object
Values of an object.
82 83 84 |
# File 'lib/easypost/object.rb', line 82 def values @values.values end |