Class: PostalMethods::Client

Inherits:
Object
  • Object
show all
Extended by:
Savon::Model
Defined in:
lib/postalmethods/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Client



40
41
42
43
44
# File 'lib/postalmethods/client.rb', line 40

def initialize(args)
  self.username = args[:username]
  self.password = args[:password]
  self.api_key  = args[:api_key]
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



26
27
28
# File 'lib/postalmethods/client.rb', line 26

def api_key
  @api_key
end

#passwordObject

Returns the value of attribute password.



26
27
28
# File 'lib/postalmethods/client.rb', line 26

def password
  @password
end

#usernameObject

Returns the value of attribute username.



26
27
28
# File 'lib/postalmethods/client.rb', line 26

def username
  @username
end

Instance Method Details

#cancel_delivery(id) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/postalmethods/client.rb', line 289

def cancel_delivery(id)
  opts = { i_d: id }
  xml = super message: sign!(opts)
  res = xml.body[:cancel_delivery_response][:cancel_delivery_result]
  code = res.to_i

  if code == -3000
    return true
  else
    report_error(code)
  end
end

#delete_uploaded_file(filename) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/postalmethods/client.rb', line 175

def delete_uploaded_file(filename)
  opts = { my_file_name: filename }
  xml = super message: sign!(opts)
  res = xml.body[:delete_uploaded_file_response][:delete_uploaded_file_result]
  code = res.to_i

  if code > 0
    true
  else
    report_error(code)
  end
end

#file_from(data) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/postalmethods/client.rb', line 68

def file_from(data)
  return data if data.is_a?(File)

  raise "Filename is empty" if data.blank?
  raise "File does not exist: #{data}" unless File.exist?(data)
  raise "File is not a regular file: #{data}" unless File.file?(data)

  File.open(data)
end

#get_details(ids) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/postalmethods/client.rb', line 99

def get_details(ids)
  opts = { i_d: one_or_many_ids(ids) }
  xml = super message: sign!(opts)
  res = xml.body[:get_details_response][:get_details_result]
  code = res[:result_code].to_i

  if code == -3000
    [res[:details][:details]].flatten
  else
    report_error(code)
  end
end

#get_details_extended(ids) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/postalmethods/client.rb', line 112

def get_details_extended(ids)
  opts = { i_d: one_or_many_ids(ids) }
  xml = super message: sign!(opts)
  res = xml.body[:get_details_extended_response][:get_details_extended_result]
  code = res[:result_code].to_i

  if code == -3000
    [res[:details][:extended_details]].flatten
  else
    report_error(code)
  end
end

#get_pdf(id) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/postalmethods/client.rb', line 125

def get_pdf(id)
  opts = { i_d: id }
  xml = super message: sign!(opts)
  res = xml.body[:get_pdf_response][:get_pdf_result]
  code = res[:result_code].to_i

  if code == -3000
    true
  else
    report_error(code)
  end
end

#get_status(ids) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/postalmethods/client.rb', line 86

def get_status(ids)
  opts = { i_d: one_or_many_ids(ids) }
  xml = super message: sign!(opts)
  res = xml.body[:get_status_response][:get_status_result]
  code = res[:result_code].to_i

  if code == -3000
    [res[:statuses][:letter_status_and_desc]].flatten
  else
    report_error(code)
  end
end

#get_uploaded_file_detailsObject



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/postalmethods/client.rb', line 138

def get_uploaded_file_details
  opts = { }
  xml = super message: sign!(opts)
  res = xml.body[:get_uploaded_file_details_response][:get_uploaded_file_details_result]
  code = res[:result_code].to_i

  if code == -3000
    [res[:uploaded_files][:file_details]].flatten
  else
    report_error(code)
  end
end

#one_or_many_ids(data) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/postalmethods/client.rb', line 54

def one_or_many_ids(data)
  if data.is_a?(Array)
    data.join(',')
  elsif data.is_a?(Range)
    data.to_s.sub('..','-')
  else
    data.to_s
  end
end

#report_error(code) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/postalmethods/client.rb', line 78

def report_error(code)
  if Error::CODES.keys.include?(code)
    instance_eval("raise APIStatusCode#{code.to_s.strip.sub(/^\-/,'')}Error")
  else
    puts "Unknown result code: #{code}"
  end
end

#send_letter(args = {}) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/postalmethods/client.rb', line 188

def send_letter(args = {})
  description = args[:description].to_s
  mode = valid_or_default(args[:mode], API_VALID_WORK_MODE)
  file = file_from(args[:file])

  opts = {
    my_description: description,
    file_extension: File.extname(file.path),
    file_binary_Data: Base64.encode64(file.read),
    work_mode: mode
  }
  xml = super message: sign!(opts)
  res = xml.body[:send_letter_response][:send_letter_result]
  code = res.to_i

  if code > 0
    code
  else
    report_error(code)
  end
end

#send_letter_and_address(args = {}) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/postalmethods/client.rb', line 210

def send_letter_and_address(args = {})
  description = args[:description].to_s
  mode = valid_or_default(args[:mode], API_VALID_WORK_MODE)
  file = file_from(args[:file])

  opts = {
    my_description: description,
    file_extension: File.extname(file.path),
    file_binary_Data: Base64.encode64(file.read),
    work_mode: mode
  }
  opts.merge!(args[:address])
  xml = super message: sign!(opts)
  res = xml.body[:send_letter_and_address_response][:send_letter_and_address_result]
  code = res.to_i

  if code > 0
    code
  else
    report_error(code)
  end
end

#send_postcard_and_address(args = {}) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/postalmethods/client.rb', line 233

def send_postcard_and_address(args = {})
  description = args[:description].to_s
  front = args[:front].to_s
  back = args[:back].to_s
  size = valid_or_default(args[:size], API_VALID_POSTCARD_SIZE)
  scaling = valid_or_default(args[:scaling], API_VALID_IMAGE_SIDE_SCALING)
  color = valid_or_default(args[:color], API_VALID_PRINT_COLOR)
  priority = valid_or_default(args[:priority], API_VALID_MAILING_PRIORITY)
  mode = valid_or_default(args[:mode], API_VALID_WORK_MODE)
  address = parse_address(args[:address])

  opts = {
    my_description: description,
    image_side_scaling: scaling,
    print_color: color,
    postcard_size: size,
    mailling_priority: priority,
    work_mode: mode,
  }
  opts.merge!(address)

  if front.is_a?(String) and front.start_with?('MyFile:','MyTemplate:')
    opts.merge!({
      image_side_file_type: File.basename(front),
    })
  else
    file = file_from(front)
    opts.merge!({
      image_side_file_type: File.extname(file.path),
      image_side_binary_data: Base64.encode64(file.read),
    })
  end

  if back.is_a?(String) and back.start_with?('MyFile:','MyTemplate:')
    opts.merge!({
      address_side_file_type: File.basename(back),
    })
  else
    file = file_from(back)
    opts.merge!({
      address_side_file_type: File.extname(file.path),
      address_side_binary_data: Base64.encode64(file.read),
    })
  end

  xml = super message: sign!(opts)
  res = xml.body[:send_postcard_and_address_response][:send_postcard_and_address_result]
  code = res.to_i

  if code > 0
    code
  else
    report_error(code)
  end
end

#sign!(data) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/postalmethods/client.rb', line 46

def sign!(data)
  data.merge!({ username: self.username }) unless self.username.blank?
  data.merge!({ password: self.password }) unless self.password.blank?
  data.merge!({ a_p_i_key: self.api_key }) unless self.api_key.blank?

  return data
end

#upload_file(args = {}) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/postalmethods/client.rb', line 151

def upload_file(args = {})
  file = file_from(args[:file])
  description = args[:description]
  permissions = valid_or_default(args[:permissions], API_VALID_PERMISSIONS)
  overwrite = valid_or_default(args[:overwrite], API_VALID_OVERWRITE)

  opts = {
    my_file_name: File.basename(file.path),
    file_binary_data: Base64.encode64(file.read),
    description: description,
    permissions: permissions,
    overwrite: overwrite,
  }
  xml = super message: sign!(opts)
  res = xml.body[:upload_file_response][:upload_file_result]
  code = res.to_i

  if code == -3000
    true
  else
    report_error(code)
  end
end

#valid_or_default(data, stack) ⇒ Object



64
65
66
# File 'lib/postalmethods/client.rb', line 64

def valid_or_default(data, stack)
  (data and stack.include?(data)) ? data : stack.first
end