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",
"object_purpose" => "PURCHASE",
"object_source" => "FULLY_ENTERED",
"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>,
:purpose => #<Shippo::API::Category::Purpose:0x007fd373df2070 @name=:purpose, @value=:purchase>,
:source => #<Shippo::API::Category::Source:0x007fd374b4fbf0 @name=:source, @value=:fully_entered>,
: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 purpose source status).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.
114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/shippo/api/api_object.rb', line 114 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
105 106 107 108 109 110 111 112 |
# File 'lib/shippo/api/api_object.rb', line 105 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
66 67 68 |
# File 'lib/shippo/api/api_object.rb', line 66 def field_name(property) "#{PREFIX[property.to_sym] || PREFIX[:default]}#{property}".to_sym end |
.matches_prefix?(value) ⇒ Boolean
70 71 72 |
# File 'lib/shippo/api/api_object.rb', line 70 def matches_prefix?(value) %r[^(#{PREFIX.values.join('|')})].match(value.to_s) end |
.mk_opts(property) ⇒ Object
74 75 76 |
# File 'lib/shippo/api/api_object.rb', line 74 def mk_opts(property) { with: ->(value) { value }, from: "#{field_name(property)}".to_sym, required: false } end |
.setup_property(prop, custom = {}) ⇒ Object
88 89 90 |
# File 'lib/shippo/api/api_object.rb', line 88 def self.setup_property(prop, custom = {}) property prop, self.mk_opts(prop).merge(custom) end |
Instance Method Details
#to_s ⇒ Object
128 129 130 |
# File 'lib/shippo/api/api_object.rb', line 128 def to_s Shippo::API::Resource.short_name(self.class.name) + self.to_hash.to_s end |