Module: GovDelivery::TMS::InstanceResource::InstanceMethods

Defined in:
lib/govdelivery/tms/instance_resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



120
121
122
# File 'lib/govdelivery/tms/instance_resource.rb', line 120

def attributes
  @attributes
end

Returns the value of attribute links.



111
112
113
# File 'lib/govdelivery/tms/instance_resource.rb', line 111

def links
  @links
end

Instance Method Details

#deleteObject



145
146
147
# File 'lib/govdelivery/tms/instance_resource.rb', line 145

def delete
  process_response(client.delete(href), :delete)
end

#delete!Object



149
150
151
# File 'lib/govdelivery/tms/instance_resource.rb', line 149

def delete!
  process_response(client.delete(href), :delete) || fail(GovDelivery::TMS::Errors::InvalidDelete.new(self))
end

#getObject Also known as: get!



122
123
124
125
# File 'lib/govdelivery/tms/instance_resource.rb', line 122

def get
  fail GovDelivery::TMS::Errors::InvalidGet if self.new_record?
  process_response(client.get(href), :get) && self
end

#initialize(client, href = nil, attrs = nil) ⇒ Object



113
114
115
116
117
118
# File 'lib/govdelivery/tms/instance_resource.rb', line 113

def initialize(client, href = nil, attrs = nil)
  super(client, href)
  @attributes = {}
  @links = {}
  set_attributes_from_hash(attrs) if attrs
end

#postObject



128
129
130
131
# File 'lib/govdelivery/tms/instance_resource.rb', line 128

def post
  self.errors = nil
  process_response(client.post(self), :post)
end

#post!Object



133
134
135
# File 'lib/govdelivery/tms/instance_resource.rb', line 133

def post!
  post || fail(GovDelivery::TMS::Errors::InvalidPost.new(self))
end

#putObject



137
138
139
# File 'lib/govdelivery/tms/instance_resource.rb', line 137

def put
  process_response(client.put(self), :put)
end

#put!Object



141
142
143
# File 'lib/govdelivery/tms/instance_resource.rb', line 141

def put!
  process_response(client.put(self), :put) || fail(GovDelivery::TMS::Errors::InvalidPut.new(self))
end

#to_jsonObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/govdelivery/tms/instance_resource.rb', line 157

def to_json
  json_hash = {}
  self.class.writeable_attributes.each do |attr|
    json_hash[attr] = send(attr)
  end
  self.class.collection_attributes.each do |coll|
    json_hash[coll] = send(coll).to_json
  end
  self.class.linkable_attributes.reject { |attr| @links[attr].nil? }.each do |attr|
    json_hash[:_links]       ||= {}
    json_hash[:_links][attr] = @links[attr]
  end
  json_hash.reject { |_, value| value.nil? }
end

#to_sObject



153
154
155
# File 'lib/govdelivery/tms/instance_resource.rb', line 153

def to_s
  "<#{self.class.inspect}#{' href=' + href if href} attributes=#{@attributes.inspect}>"
end