govdelivery-tms 0.8.1 → 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -22,7 +22,7 @@ class GovDelivery::TMS::Connection
22
22
  faraday.use GovDelivery::TMS::Logger, self.logger if self.logger
23
23
  faraday.request :json
24
24
  faraday.headers['X-AUTH-TOKEN'] = auth_token
25
- faraday.headers[:user_agent] = "GovDelivery Ruby TMS::Client #{TMS::VERSION}"
25
+ faraday.headers[:user_agent] = "GovDelivery Ruby GovDelivery::TMS::Client #{GovDelivery::TMS::VERSION}"
26
26
  faraday.response :json, content_type: /\bjson$/
27
27
  faraday.adapter :net_http
28
28
  end
@@ -1,5 +1,5 @@
1
1
  module GovDelivery
2
2
  module TMS #:nodoc:
3
- VERSION = "0.8.1"
3
+ VERSION = "0.8.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: govdelivery-tms
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.8.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -109,7 +109,6 @@ files:
109
109
  - lib/govdelivery/tms/util/core_ext.rb
110
110
  - lib/govdelivery/tms/util/hal_link_parser.rb
111
111
  - lib/govdelivery/tms/version.rb
112
- - lib/govdelivery-tms/instance_resource.rb
113
112
  - lib/govdelivery-tms.rb
114
113
  - spec/client_spec.rb
115
114
  - spec/command_types_spec.rb
@@ -1,219 +0,0 @@
1
- module GovDelivery::TMS::InstanceResource
2
- def self.included(base)
3
- base.send(:include, TMS::Base)
4
- base.extend(ClassMethods)
5
- base.send(:include, InstanceMethods)
6
- end
7
-
8
- attr_accessor :response
9
-
10
- module ClassMethods
11
- ##
12
- # Writeable attributes are sent on POST/PUT.
13
- #
14
- def writeable_attributes(*attrs)
15
- @writeable_attributes ||= []
16
- if attrs.any?
17
- @writeable_attributes.map!(&:to_sym).concat(attrs).uniq! if attrs.any?
18
- setup_attributes(@writeable_attributes, false)
19
- end
20
- @writeable_attributes
21
- end
22
-
23
- ##
24
- # Linkable attributes are sent on POST/PUT.
25
- #
26
- def linkable_attributes(*attrs)
27
- @linkable_attributes ||= []
28
- if attrs.any?
29
- @linkable_attributes.map!(&:to_sym).concat(attrs).uniq! if attrs.any?
30
- end
31
- @linkable_attributes
32
- end
33
-
34
- ##
35
- # Readonly attributes don't get POSTed.
36
- # (timestamps are included by default)
37
- #
38
- def readonly_attributes(*attrs)
39
- @readonly_attributes ||= [:created_at, :updated_at, :completed_at]
40
- if attrs.any?
41
- @readonly_attributes.map!(&:to_sym).concat(attrs).uniq!
42
- setup_attributes(@readonly_attributes, true)
43
- end
44
- @readonly_attributes
45
- end
46
-
47
- ##
48
- # For collections that are represented as attributes (i.e. inline, no href)
49
- #
50
- # @example
51
- # collection_attributes :recipients
52
- #
53
- def collection_attributes(*attrs)
54
- @collection_attributes ||= []
55
- if attrs.any?
56
- @collection_attributes.map!(&:to_sym).concat(attrs).uniq!
57
- @collection_attributes.each { |a| setup_collection(a) }
58
- end
59
- @collection_attributes
60
- end
61
-
62
- def custom_class_names
63
- @custom_class_names ||= {}
64
- end
65
-
66
- ##
67
- # For collections that are represented as attributes (i.e. inline, no href)
68
- # and that have a class name other than the one we would infer.
69
- #
70
- # @example
71
- # collection_attributes :recipients, 'EmailRecipient'
72
- #
73
- def collection_attribute(attr, tms_class)
74
- @collection_attributes ||= []
75
- @collection_attributes.push(attr).uniq!
76
- setup_collection(attr, GovDelivery::TMS.const_get(tms_class))
77
- end
78
-
79
- ##
80
- # Read-only collection attributes don't get POSTed.
81
- # Use this for collections that are represented as attributes, but cannot be modified.
82
- #
83
- # @example
84
- # readonly_collection_attribute :opens
85
- #
86
- def readonly_collection_attribute(attr, tms_class)
87
- @readonly_collection_attributes ||= []
88
- @readonly_collection_attributes.push(attr).uniq!
89
- setup_collection(attr, GovDelivery::TMS.const_get(tms_class))
90
- end
91
-
92
- def setup_attributes(attrs, readonly=false)
93
- attrs.map(&:to_sym).each do |property|
94
- self.send :define_method, :"#{property}=", &lambda { |v| @attributes[property] = v } unless readonly
95
- self.send :define_method, property.to_sym, &lambda { @attributes[property] }
96
- end
97
- end
98
-
99
- def setup_collection(property, klass=nil)
100
- if klass
101
- custom_class_names[property] = klass
102
- else
103
- klass ||= GovDelivery::TMS.const_get(property.to_s.capitalize)
104
- end
105
-
106
- self.send :define_method, property.to_sym, &lambda { @attributes[property] ||= klass.new(self.client, nil, nil) }
107
- end
108
- end
109
-
110
- module InstanceMethods
111
- attr_reader :links
112
-
113
- def initialize(client, href=nil, attrs=nil)
114
- super(client, href)
115
- @attributes = {}
116
- @links = {}
117
- set_attributes_from_hash(attrs) if attrs
118
- end
119
-
120
- def attributes
121
- @attributes
122
- end
123
-
124
- def get
125
- raise TMS::Errors::InvalidGet if self.new_record?
126
- process_response(client.get(self.href), :get) && self
127
- end
128
- alias_method :get!, :get
129
-
130
- def post
131
- self.errors = nil
132
- process_response(client.post(self), :post)
133
- end
134
-
135
- def post!
136
- self.post or raise TMS::Errors::InvalidPost.new(self)
137
- end
138
-
139
- def put
140
- process_response(client.put(self), :put)
141
- end
142
-
143
- def put!
144
- process_response(client.put(self), :put) or raise TMS::Errors::InvalidPut.new(self)
145
- end
146
-
147
- def delete
148
- process_response(client.delete(self.href), :delete)
149
- end
150
-
151
- def delete!
152
- process_response(client.delete(self.href), :delete) or raise TMS::Errors::InvalidDelete.new(self)
153
- end
154
-
155
- def to_s
156
- "<#{self.class.inspect}#{' href=' + self.href if self.href} attributes=#{@attributes.inspect}>"
157
- end
158
-
159
- def to_json
160
- json_hash = {}
161
- self.class.writeable_attributes.each do |attr|
162
- json_hash[attr] = self.send(attr)
163
- end
164
- self.class.collection_attributes.each do |coll|
165
- json_hash[coll] = self.send(coll).to_json
166
- end
167
- self.class.linkable_attributes.each do |attr|
168
- json_hash[:_links] ||= {}
169
- json_hash[:_links][attr] = @links[attr]
170
- end
171
- json_hash
172
- end
173
-
174
- protected
175
-
176
- def relation_class(rel)
177
- self.class.custom_class_names[rel.to_sym] || super
178
- end
179
-
180
- def process_response(response, method)
181
- self.response = response
182
- error_class = TMS::Errors.const_get("Invalid#{method.to_s.capitalize}")
183
- case response.status
184
- when 204
185
- return true
186
- when 200..299
187
- set_attributes_from_hash(response.body) if response.body.is_a?(Hash)
188
- @links = {}
189
- self.new_record=false
190
- return true
191
- when 401
192
- raise error_class.new("401 Not Authorized")
193
- when 404
194
- raise(error_class.new("Can't POST to #{self.href}"))
195
- when 500..599
196
- raise(TMS::Errors::ServerError.new(response))
197
- else # 422?
198
- if response.body['errors']
199
- self.errors = response.body['errors']
200
- end
201
- end
202
- return false
203
- end
204
-
205
- def set_attributes_from_hash(hash)
206
- hash.reject { |k, _| k=~/^_/ }.each do |property, value|
207
- if self.class.collection_attributes.include?(property.to_sym)
208
- klass = self.class.custom_class_names[property] || GovDelivery::TMS.const_get(property.to_s.capitalize)
209
- @attributes[property.to_sym] = klass.new(client, nil, value)
210
- else
211
- @attributes[property.to_sym] = value
212
- end
213
- end
214
- self.errors = hash['errors']
215
- parse_links(hash['_links'])
216
- end
217
-
218
- end
219
- end