Class: Shippo::API::ApiObject
- Inherits:
-
Hashie::Dash
- Object
- Hashie::Dash
- Shippo::API::ApiObject
- Includes:
- Hashie::Extensions::Dash::PropertyTranslation
- Defined in:
- lib/shippo/api/api_object.rb
Overview
ApiObject
is a class that contains only a set of specific fields that can be used in both requests and responses from Shippo API. Upon return with each response, ApiObject
instance is automatically created and populated with fields that begin with a prefix ‘object_`. The prefix is deleted, and the fields are made available through the non-prefixed accessors.
This class uses Hashie::Dash
under the hood, in order to provide convenient transformations, support required/optional attributes, etc.
Example
“‘ruby response =
"object_state" => "VALID", # not available for address, shipment or rates
"object_created" => "2014-07-16T23:20:31.089Z",
"object_updated" => "2014-07-16T23:20:31.089Z",
"object_id" => "747207de2ba64443b645d08388d0309c",
"object_owner" => "[email protected]",
"name" => "Shawn Ippotle",
"company" => "Shippo",
"street1" => "215 Clayton St.",
"street2" => "",
"city" => "San Francisco",
"state" => "CA",
"zip" => "94117",
"country" => "US",
"phone" => "+1 555 341 9393",
"email" => "[email protected]"
require ‘shippo’ address = Shippo::Address.from(response) address.name # ⤷ Shawn Ippotle require ‘ap’ ap address.object # ⤷
:state => #<Shippo::API::Category::State:0x007fd374b4d0d0 @name=:state, @value=:valid>,
:created => 2014-07-16 23:20:31 UTC,
:updated => 2014-07-16 23:20:31 UTC,
:id => "747207de2ba64443b645d08388d0309c",
:owner => "[email protected]"
“‘
Constant Summary collapse
- PREFIX =
{ id: 'resource_', default: 'object_' }.freeze
- PROPS_ID =
list of allowed properties, of a given type.
%i(id).freeze
- PROPS_CATEG =
%i(state status results).freeze
- PROPS_EMAIL =
%i(owner).freeze
- PROPS_TIMED =
%i(created updated).freeze
- PROPS =
(PROPS_ID + PROPS_EMAIL + PROPS_TIMED + PROPS_CATEG ).flatten.freeze
- PROPS_AS_IS =
(PROPS_EMAIL + PROPS_ID).freeze
Class Method Summary collapse
- .create_object(h) ⇒ Object
- .field_name(property) ⇒ Object
- .matches_prefix?(value) ⇒ Boolean
- .mk_opts(property) ⇒ Object
- .setup_property(prop, custom = {}) ⇒ Object
Instance Method Summary collapse
-
#initialize(*args) ⇒ ApiObject
constructor
A new instance of ApiObject.
- #to_s ⇒ Object
Constructor Details
#initialize(*args) ⇒ ApiObject
Returns a new instance of ApiObject.
110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/shippo/api/api_object.rb', line 110 def initialize(*args) opts = args.first if opts && opts.respond_to?(:keys) Hashie::Extensions::SymbolizeKeys.symbolize_keys!(opts) if opts[:object_id] opts[(PREFIX[:id] + 'id').to_sym] = opts[:object_id] opts.delete(:object_id) end super(opts) else super(args) end end |
Class Method Details
.create_object(h) ⇒ Object
101 102 103 104 105 106 107 108 |
# File 'lib/shippo/api/api_object.rb', line 101 def self.create_object(h) object_keys = h.keys.select { |k| matches_prefix?(k) } h_object = {} object_keys.each { |k| h_object[k.to_s] = h[k] } instance = self.new(h_object) object_keys.each { |k| h.delete(k) if h.key(k) } instance end |
.field_name(property) ⇒ Object
62 63 64 |
# File 'lib/shippo/api/api_object.rb', line 62 def field_name(property) "#{PREFIX[property.to_sym] || PREFIX[:default]}#{property}".to_sym end |
.matches_prefix?(value) ⇒ Boolean
66 67 68 |
# File 'lib/shippo/api/api_object.rb', line 66 def matches_prefix?(value) %r[^(#{PREFIX.values.join('|')})].match(value.to_s) end |
.mk_opts(property) ⇒ Object
70 71 72 |
# File 'lib/shippo/api/api_object.rb', line 70 def mk_opts(property) { with: ->(value) { value }, from: "#{field_name(property)}".to_sym, required: false } end |
.setup_property(prop, custom = {}) ⇒ Object
84 85 86 |
# File 'lib/shippo/api/api_object.rb', line 84 def self.setup_property(prop, custom = {}) property prop, self.mk_opts(prop).merge(custom) end |
Instance Method Details
#to_s ⇒ Object
124 125 126 |
# File 'lib/shippo/api/api_object.rb', line 124 def to_s Shippo::API::Resource.short_name(self.class.name) + self.to_hash.to_s end |