bronto 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,6 +14,23 @@ require "core_ext/object"
14
14
  require "core_ext/string"
15
15
 
16
16
  module Bronto
17
+ class Error < StandardError
18
+ attr_accessor :code, :message
19
+
20
+ def initialize(code, message)
21
+ self.code = code
22
+ self.message = message
23
+ end
24
+
25
+ def code=(new_code)
26
+ @code = new_code.to_i
27
+ end
28
+
29
+ def to_s
30
+ "#{code}: #{message}"
31
+ end
32
+ end
33
+
17
34
  class Errors
18
35
  attr_accessor :messages
19
36
 
@@ -106,10 +106,10 @@ module Bronto
106
106
  objs.each { |o| o.errors.clear }
107
107
 
108
108
  Array.wrap(resp[:return][:results]).each_with_index do |result, i|
109
- if result[:is_new] and !result[:is_error]
110
- objs[i].id = result[:id]
111
- elsif result[:is_error]
109
+ if result[:is_error]
112
110
  objs[i].errors.add(result[:error_code], result[:error_string])
111
+ else
112
+ objs[i].id = result[:id]
113
113
  end
114
114
  end
115
115
 
@@ -1,6 +1,6 @@
1
1
  module Bronto
2
2
  class Contact < Base
3
- attr_accessor :email, :fields, :lists
3
+ attr_accessor :email, :fields, :list_ids
4
4
 
5
5
  # Finds contacts based on the `filter` (Bronto::Filter object).
6
6
  # * `page_number` is the page of contacts to request. Bronto doesn't specify how many contacts are returned per page,
@@ -9,6 +9,7 @@ module Bronto
9
9
  # * `include_lists` determines whether to include the list IDs each contact belongs to.
10
10
  def self.find(filter = Bronto::Filter.new, page_number = 1, fields = nil, include_lists = false, api_key = nil)
11
11
  body = { filter: filter.to_hash, page_number: page_number }
12
+ api_key = api_key || self.api_key
12
13
 
13
14
  body[:fields] = Array.wrap(fields).map { |f| f.is_a?(Bronto::Field) ? f.id : f } if Array(fields).length > 0
14
15
  body[:include_lists] = include_lists
@@ -33,10 +34,10 @@ module Bronto
33
34
  objs.each { |o| o.errors.clear }
34
35
 
35
36
  Array.wrap(resp[:return][:results]).each_with_index do |result, i|
36
- if result[:is_new] and !result[:is_error]
37
- objs[i].id = result[:id]
38
- elsif result[:is_error]
37
+ if result[:is_error]
39
38
  objs[i].errors.add(result[:error_code], result[:error_string])
39
+ else
40
+ objs[i].id = result[:id]
40
41
  end
41
42
  end
42
43
 
@@ -51,6 +52,25 @@ module Bronto
51
52
  super(options)
52
53
  end
53
54
 
55
+ def reload
56
+ return false if self.email.blank?
57
+
58
+ filter = Bronto::Filter.new
59
+ filter.add_filter("email", "EqualTo", self.email)
60
+
61
+ new_contact = self.class.find(filter, 1, self.fields, true, self.api_key).first
62
+
63
+ self.id = new_contact.id
64
+ self.fields = new_contact.fields
65
+ self.list_ids = new_contact.list_ids
66
+
67
+ true
68
+ end
69
+
70
+ def save
71
+ self.class.save(self)
72
+ end
73
+
54
74
  def to_hash
55
75
  if id.present?
56
76
  { id: id, email: email, fields: fields.values.map(&:to_hash) }
@@ -11,6 +11,7 @@ module Bronto
11
11
  def self.find(filter = Bronto::Filter.new, page_number = 1, include_recipients = false, include_content = false, api_key = nil)
12
12
  body = { filter: filter.to_hash, page_number: page_number, include_recipients: include_recipients,
13
13
  include_content: include_content }
14
+ api_key = api_key || self.class.api_key
14
15
 
15
16
  resp = request(:read, api_key) do
16
17
  soap.body = body
@@ -23,8 +23,16 @@ module Bronto
23
23
  self.active_count ||= 0
24
24
  end
25
25
 
26
- # Adds the given contacts to this list.
27
26
  def add_to_list(*contacts)
27
+ begin
28
+ add_to_list!(contacts)
29
+ rescue Bronto::Error => e
30
+ false
31
+ end
32
+ end
33
+
34
+ # Adds the given contacts to this list.
35
+ def add_to_list!(*contacts)
28
36
  return false if !self.id.present?
29
37
  contacts = contacts.flatten
30
38
 
@@ -38,7 +46,12 @@ module Bronto
38
46
  }
39
47
  end
40
48
 
41
- Array.wrap(resp[:return][:results]).select { |r| r[:is_error] }.count == 0
49
+ errors = Array.wrap(resp[:return][:results]).select { |r| r[:is_error] }
50
+ errors.each do |error|
51
+ raise Bronto::Error.new(error[:error_code], error[:error_string])
52
+ end
53
+
54
+ true
42
55
  end
43
56
 
44
57
  # Removes the given contacts from this list.
@@ -1,3 +1,3 @@
1
1
  module Bronto
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bronto
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: