bronto 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/lib/bronto/base.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Bronto
2
2
  class Base
3
- attr_accessor :id, :errors
3
+ attr_accessor :id, :api_key, :errors
4
4
 
5
5
  # Getter/Setter for global API Key.
6
6
  def self.api_key=(api_key)
@@ -22,8 +22,9 @@ module Bronto
22
22
  # If a symbol is passed in, it is converted to "method_plural_class_name" (e.g., :read => read_lists). A string
23
23
  # method is used as-is.
24
24
  # Pass in a block and assign a hash to soap.body with a structure appropriate to the method call.
25
- def self.request(method, refresh_header = false, &_block)
26
- _soap_header = self.soap_header(refresh_header)
25
+ def self.request(method, api_key = nil, refresh_header = false, &_block)
26
+ _soap_header = self.soap_header(api_key, refresh_header)
27
+ api_key = api_key || self.api_key
27
28
 
28
29
  method = "#{method}_#{plural_class_name}" if method.is_a? Symbol
29
30
 
@@ -47,11 +48,11 @@ module Bronto
47
48
 
48
49
  # Helper method to retrieve the session ID and return a SOAP header.
49
50
  # Will return a header with the same initial session ID unless the `refresh` argument is `true`.
50
- def self.soap_header(refresh = false)
51
+ def self.soap_header(api_key, refresh = false)
51
52
  return @soap_header if !refresh and @soap_header.present?
52
53
 
53
54
  resp = api.request(:v4, :login) do
54
- soap.body = { api_token: self.api_key }
55
+ soap.body = { api_token: api_key }
55
56
  end
56
57
 
57
58
  @soap_header = { "v4:sessionHeader" => { session_id: resp.body[:login_response][:return] } }
@@ -61,6 +62,8 @@ module Bronto
61
62
  # Objects without IDs are considered new and are `create`d; objects with IDs are considered existing and are `update`d.
62
63
  def self.save(*objs)
63
64
  objs = objs.flatten
65
+ api_key = objs.first.is_a?(String) ? objs.shift : self.api_key
66
+
64
67
  updates = []
65
68
  creates = []
66
69
 
@@ -72,8 +75,10 @@ module Bronto
72
75
  end
73
76
 
74
77
  # Finds objects matching the `filter` (a Bronto::Filter instance).
75
- def self.find(filter = Bronto::Filter.new, page_number = 1)
76
- resp = request(:read) do
78
+ def self.find(filter = Bronto::Filter.new, page_number = 1, api_key = nil)
79
+ api_key = api_key || self.api_key
80
+
81
+ resp = request(:read, api_key) do
77
82
  soap.body = { filter: filter.to_hash, page_number: page_number }
78
83
  end
79
84
 
@@ -85,10 +90,12 @@ module Bronto
85
90
  #
86
91
  # Returns the same collection of objects that was passed in. Objects whose creation succeeded will be assigned the
87
92
  # ID returned from Bronto.
93
+ # The first element passed in can be a string containing the API key; if none passed, will fall back to the global key.
88
94
  def self.create(*objs)
89
95
  objs = objs.flatten
96
+ api_key = objs.first.is_a?(String) ? objs.shift : self.api_key
90
97
 
91
- resp = request(:add) do
98
+ resp = request(:add, api_key) do
92
99
  soap.body = {
93
100
  plural_class_name => objs.map(&:to_hash)
94
101
  }
@@ -109,10 +116,12 @@ module Bronto
109
116
 
110
117
  # Updates a collection of Bronto::Base objects. The objects should exist on the remote server.
111
118
  # The object should implement `to_hash` to return a hash in the format expected by the SOAP API.
119
+ # The first element passed in can be a string containing the API key; if none passed, will fall back to the global key.
112
120
  def self.update(*objs)
113
121
  objs = objs.flatten
122
+ api_key = objs.first.is_a?(String) ? objs.shift : self.api_key
114
123
 
115
- resp = request(:update) do
124
+ resp = request(:update, api_key) do
116
125
  soap.body = {
117
126
  plural_class_name => objs.map(&:to_hash)
118
127
  }
@@ -126,10 +135,13 @@ module Bronto
126
135
  #
127
136
  # Returns the same collection of objects that was passed in. Objects whose destruction succeeded will
128
137
  # have a nil ID.
138
+ #
139
+ # The first element passed in can be a string containing the API key; if none passed, will fall back to the global key.
129
140
  def self.destroy(*objs)
130
141
  objs = objs.flatten
142
+ api_key = objs.first.is_a?(String) ? objs.shift : self.api_key
131
143
 
132
- resp = request(:delete) do
144
+ resp = request(:delete, api_key) do
133
145
  soap.body = {
134
146
  plural_class_name => objs.map { |o| { id: o.id }}
135
147
  }
@@ -148,6 +160,7 @@ module Bronto
148
160
 
149
161
  # Accepts a hash whose keys should be setters on the object.
150
162
  def initialize(options = {})
163
+ self.api_key = self.class.api_key
151
164
  self.errors = Errors.new
152
165
  options.each { |k,v| send("#{k}=", v) if respond_to?("#{k}=") }
153
166
  end
@@ -159,7 +172,7 @@ module Bronto
159
172
 
160
173
  # Convenience instance method that calls the class `request` method.
161
174
  def request(method, &block)
162
- self.class.request(method, &block)
175
+ self.class.request(method, self.api_key, false, &block)
163
176
  end
164
177
 
165
178
  def reload
@@ -186,18 +199,18 @@ module Bronto
186
199
 
187
200
  # Creates the object. See `Bronto::Base.create` for more info.
188
201
  def create
189
- res = self.class.create(self)
202
+ res = self.class.create(self.api_key, self)
190
203
  res.first
191
204
  end
192
205
 
193
206
  # Updates the object. See `Bronto::Base.update` for more info.
194
207
  def update
195
- self.class.update(self).first
208
+ self.class.update(self.api_key, self).first
196
209
  end
197
210
 
198
211
  # Destroys the object. See `Bronto::Base.destroy` for more info.
199
212
  def destroy
200
- self.class.destroy(self).first
213
+ self.class.destroy(self.api_key, self).first
201
214
  end
202
215
  end
203
216
  end
@@ -7,13 +7,13 @@ module Bronto
7
7
  # only that you should keep increasing the number until no more contacts are returned.
8
8
  # * `fields` can be an array of field IDs or an array of Field objects.
9
9
  # * `include_lists` determines whether to include the list IDs each contact belongs to.
10
- def self.find(filter = Bronto::Filter.new, page_number = 1, fields = nil, include_lists = false)
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
12
 
13
13
  body[:fields] = Array.wrap(fields).map { |f| f.is_a?(Bronto::Field) ? f.id : f } if Array(fields).length > 0
14
14
  body[:include_lists] = include_lists
15
15
 
16
- resp = request(:read) do
16
+ resp = request(:read, api_key) do
17
17
  soap.body = body
18
18
  end
19
19
 
@@ -22,8 +22,9 @@ module Bronto
22
22
 
23
23
  def self.save(*objs)
24
24
  objs = objs.flatten
25
+ api_key = objs.first.is_a?(String) ? objs.shift : self.api_key
25
26
 
26
- resp = request(:add_or_update) do
27
+ resp = request(:add_or_update, api_key) do
27
28
  soap.body = {
28
29
  plural_class_name => objs.map(&:to_hash)
29
30
  }
@@ -8,11 +8,11 @@ module Bronto
8
8
  # only that you should keep increasing the number until no more contacts are returned.
9
9
  # * `fields` can be an array of field IDs or an array of Field objects.
10
10
  # * `include_lists` determines whether to include the list IDs each contact belongs to.
11
- def self.find(filter = Bronto::Filter.new, page_number = 1, include_recipients = false, include_content = false)
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
14
 
15
- resp = request(:read) do
15
+ resp = request(:read, api_key) do
16
16
  soap.body = body
17
17
  end
18
18
 
data/lib/bronto/list.rb CHANGED
@@ -5,8 +5,9 @@ module Bronto
5
5
  # Removes all contacts from the given lists.
6
6
  def self.clear_lists(*lists)
7
7
  lists = lists.flatten
8
+ api_key = lists.first.is_a?(String) ? lists.shift : self.api_key
8
9
 
9
- resp = request(:clear) do
10
+ resp = request(:clear, api_key) do
10
11
  soap.body = {
11
12
  list: lists.map { |l| { id: l.id } }
12
13
  }
@@ -1,3 +1,3 @@
1
1
  module Bronto
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
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.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-29 00:00:00.000000000 Z
12
+ date: 2012-05-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: savon