Class: Shippo::API::Resource
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#transformers
included
included
Constructor Details
#initialize(*args) ⇒ Resource
As a Hashie::Mash subclass, Resource can initialize from another hash
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/shippo/api/resource.rb', line 64
def initialize(*args)
if args.first.is_a?(Fixnum) or
(args.first.is_a?(String) && args.first =~ /^[0-9A-Fa-f]+$/)
self.id = args.first
elsif args.first.respond_to?(:keys)
h = Hashie::Mash.new(args.first)
self.deep_merge!(h)
self.object = ApiObject.create_object(self)
transformers.each do |transformer|
transformer.new(self).transform
end
else
super(*args)
end
end
|
Instance Attribute Details
#object ⇒ Object
Returns the value of attribute object.
26
27
28
|
# File 'lib/shippo/api/resource.rb', line 26
def object
@object
end
|
Class Method Details
.from(values) ⇒ Object
Creates a possibly recursive chain (map of lists, etc) of Resource instances based on whether each value is a scalar, array or a hash.
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/shippo/api/resource.rb', line 31
def self.from(values)
if values.is_a?(Array)
values.map { |list| from(list) }
elsif values.respond_to?(:keys) new(values)
else
values
end
end
|
.object_properties ⇒ Object
.short_name(name = self.name) ⇒ Object
42
43
44
|
# File 'lib/shippo/api/resource.rb', line 42
def self.short_name(name = self.name)
name.split('::')[-1]
end
|
Instance Method Details
#inspect ⇒ Object
80
81
82
|
# File 'lib/shippo/api/resource.rb', line 80
def inspect
"#<#{self.class.short_name}:0x#{self.object_id.to_s(16)}#{id.nil? ? '' : "[id=#{id}]"}#{to_hash.inspect}->#{object.inspect}"
end
|
#refresh ⇒ Object
93
94
95
96
97
|
# File 'lib/shippo/api/resource.rb', line 93
def refresh
response = Shippo::API.get(url)
self.from(response)
self
end
|
#success? ⇒ Boolean
99
100
101
|
# File 'lib/shippo/api/resource.rb', line 99
def success?
self.object && self.object.status && self.object.status.eql?(Shippo::API::Category::Status::SUCCESS)
end
|
#to_s ⇒ Object
84
85
86
|
# File 'lib/shippo/api/resource.rb', line 84
def to_s
self.class.short_name + self.to_hash.to_s + '->' + self.object.to_s
end
|
#url ⇒ Object
88
89
90
91
|
# File 'lib/shippo/api/resource.rb', line 88
def url
raise Shippo::Exceptions::MissingDataError.new("#{self.class} has no object_id") unless id
"#{self.class.url}/#{CGI.escape(id)}"
end
|
#valid? ⇒ Boolean
103
104
105
|
# File 'lib/shippo/api/resource.rb', line 103
def valid?
self.object && self.object.state && self.object.state.eql?(Shippo::API::Category::State::VALID)
end
|