class Shippo::API::ApiObject
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" => "shippotle@goshippo.com", "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" => "shippotle@goshippo.com"
}
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 => "shippotle@goshippo.com"
} “`
Constants
- PREFIX
- PROPS
- PROPS_AS_IS
- PROPS_CATEG
- PROPS_EMAIL
- PROPS_ID
list of allowed properties, of a given type.
- PROPS_TIMED
Public Class Methods
create_object(h)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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
new(*args)
click to toggle source
Calls superclass method
# 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
setup_property(prop, custom = {})
click to toggle source
# File lib/shippo/api/api_object.rb, line 84 def self.setup_property(prop, custom = {}) property prop, self.mk_opts(prop).merge(custom) end
Public Instance Methods
to_s()
click to toggle source
# 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