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.
63 64 65 |
# File 'lib/easypost/object.rb', line 63 def [](key) @values[key.to_s] end |
#[]=(key, value) ⇒ Object
Set the element of an array.
68 69 70 |
# File 'lib/easypost/object.rb', line 68 def []=(key, value) send(:"#{key}=", value) end |
#as_json(_options = {}) ⇒ Object
Get values as JSON.
88 89 90 |
# File 'lib/easypost/object.rb', line 88 def as_json( = {}) @values.as_json end |
#deconstruct_keys(_keys) ⇒ Object
Deconstruct the keys of an object.
98 99 100 |
# File 'lib/easypost/object.rb', line 98 def deconstruct_keys(_keys) @values.transform_keys(&:to_sym) end |
#each(&blk) ⇒ Object
Get each element of values.
103 104 105 |
# File 'lib/easypost/object.rb', line 103 def each(&blk) @values.each(&blk) end |
#id ⇒ Object
Get the ID of an object.
113 114 115 |
# File 'lib/easypost/object.rb', line 113 def id @values[:id] end |
#id=(id) ⇒ Object
Set the ID of an object.
108 109 110 |
# File 'lib/easypost/object.rb', line 108 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.
73 74 75 |
# File 'lib/easypost/object.rb', line 73 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 |
# 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 don't change, do not update it @values.delete(: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 end |
#to_hash ⇒ Object
Make values a hash.
93 94 95 |
# File 'lib/easypost/object.rb', line 93 def to_hash @values end |
#to_json(_options = {}) ⇒ Object
Make values JSON.
83 84 85 |
# File 'lib/easypost/object.rb', line 83 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.
78 79 80 |
# File 'lib/easypost/object.rb', line 78 def values @values.values end |