restforce 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of restforce might be problematic. Click here for more details.
- data/README.md +17 -3
- data/lib/restforce/client.rb +8 -1
- data/lib/restforce/middleware/authentication.rb +1 -1
- data/lib/restforce/middleware/logger.rb +31 -7
- data/lib/restforce/version.rb +1 -1
- data/spec/lib/client_spec.rb +25 -1
- data/spec/lib/middleware/logger_spec.rb +22 -0
- metadata +4 -2
data/README.md
CHANGED
@@ -150,6 +150,8 @@ _See also: http://www.salesforce.com/us/developer/docs/api_rest/Content/dome_sea
|
|
150
150
|
|
151
151
|
### create(sobject, attrs)
|
152
152
|
|
153
|
+
_Alias: insert_
|
154
|
+
|
153
155
|
Takes an sobject name and a hash of attributes to create a record. Returns the
|
154
156
|
Id of the newly created reocrd if the record was successfully created.
|
155
157
|
|
@@ -195,6 +197,8 @@ _See also: http://www.salesforce.com/us/developer/docs/api_rest/Content/dome_ups
|
|
195
197
|
|
196
198
|
### destroy(sobject, id)
|
197
199
|
|
200
|
+
_Alias: delete_
|
201
|
+
|
198
202
|
Takes an sobject name and an Id and deletes the record. Returns true if the
|
199
203
|
record was successfully deleted.
|
200
204
|
|
@@ -257,14 +261,24 @@ Restforce supports the [Streaming API](http://wiki.developerforce.com/page/Getti
|
|
257
261
|
pub/sub with Salesforce a trivial task:
|
258
262
|
|
259
263
|
```ruby
|
260
|
-
# Initialize a client with your username/password/oauth token/etc
|
264
|
+
# Initialize a client with your username/password/oauth token/etc.
|
261
265
|
client = Restforce.new
|
262
266
|
|
263
|
-
# Force an authentication request
|
267
|
+
# Force an authentication request.
|
264
268
|
client.authenticate!
|
265
269
|
|
270
|
+
# Create a PushTopic for subscribing to Account changes.
|
271
|
+
client.create! 'PushTopic', {
|
272
|
+
ApiVersion: '23.0',
|
273
|
+
Name: 'AllAccounts',
|
274
|
+
Description: 'All account records',
|
275
|
+
NotifyForOperations: 'All',
|
276
|
+
NotifyForFields: 'All',
|
277
|
+
Query: "select Id from Account"
|
278
|
+
}
|
279
|
+
|
266
280
|
EM.run {
|
267
|
-
#
|
281
|
+
# Subscribe to the PushTopic.
|
268
282
|
client.subscribe 'AllAccounts' do |message|
|
269
283
|
puts message.inspect
|
270
284
|
end
|
data/lib/restforce/client.rb
CHANGED
@@ -162,6 +162,7 @@ module Restforce
|
|
162
162
|
rescue *exceptions
|
163
163
|
false
|
164
164
|
end
|
165
|
+
alias_method :insert, :create
|
165
166
|
|
166
167
|
# See .create
|
167
168
|
#
|
@@ -171,6 +172,7 @@ module Restforce
|
|
171
172
|
response = api_post "sobjects/#{sobject}", attrs
|
172
173
|
response.body['id']
|
173
174
|
end
|
175
|
+
alias_method :insert!, :create!
|
174
176
|
|
175
177
|
# Public: Update a record.
|
176
178
|
#
|
@@ -192,6 +194,7 @@ module Restforce
|
|
192
194
|
# otherwise.
|
193
195
|
def update!(sobject, attrs)
|
194
196
|
id = attrs.has_key?(:Id) ? attrs.delete(:Id) : attrs.delete('Id')
|
197
|
+
raise 'Id field missing.' unless id
|
195
198
|
api_patch "sobjects/#{sobject}/#{id}", attrs
|
196
199
|
true
|
197
200
|
end
|
@@ -240,6 +243,7 @@ module Restforce
|
|
240
243
|
rescue *exceptions
|
241
244
|
false
|
242
245
|
end
|
246
|
+
alias_method :delete, :destroy
|
243
247
|
|
244
248
|
# See .destroy
|
245
249
|
#
|
@@ -249,6 +253,7 @@ module Restforce
|
|
249
253
|
api_delete "sobjects/#{sobject}/#{id}"
|
250
254
|
true
|
251
255
|
end
|
256
|
+
alias_method :delete!, :destroy!
|
252
257
|
|
253
258
|
# Public: Runs the block with caching disabled.
|
254
259
|
#
|
@@ -355,7 +360,7 @@ module Restforce
|
|
355
360
|
builder.use Restforce::Middleware::RaiseError
|
356
361
|
builder.response :json
|
357
362
|
builder.use Restforce::Middleware::Caching, cache, @options if cache
|
358
|
-
builder.use Restforce::Middleware::Logger, Restforce.configuration.logger if Restforce.log?
|
363
|
+
builder.use Restforce::Middleware::Logger, Restforce.configuration.logger, @options if Restforce.log?
|
359
364
|
builder.adapter Faraday.default_adapter
|
360
365
|
end
|
361
366
|
@connection
|
@@ -406,7 +411,9 @@ module Restforce
|
|
406
411
|
|
407
412
|
# Internal: Faye client to use for subscribing to PushTopics
|
408
413
|
def faye
|
414
|
+
raise 'Instance URL missing. Call .authenticate! first.' unless @options[:instance_url]
|
409
415
|
@faye ||= Faye::Client.new("#{@options[:instance_url]}/cometd/#{@options[:api_version]}").tap do |client|
|
416
|
+
raise 'OAuth token missing. Call .authenticate! first.' unless @options[:oauth_token]
|
410
417
|
client.set_header 'Authorization', "OAuth #{@options[:oauth_token]}"
|
411
418
|
client.bind 'transport:down' do
|
412
419
|
Restforce.log "[COMETD DOWN]"
|
@@ -32,7 +32,7 @@ module Restforce
|
|
32
32
|
def connection
|
33
33
|
@connection ||= Faraday.new(:url => "https://#{@options[:host]}") do |builder|
|
34
34
|
builder.response :json
|
35
|
-
builder.use Restforce::Middleware::Logger, Restforce.configuration.logger if Restforce.log?
|
35
|
+
builder.use Restforce::Middleware::Logger, Restforce.configuration.logger, @options if Restforce.log?
|
36
36
|
builder.adapter Faraday.default_adapter
|
37
37
|
end
|
38
38
|
end
|
@@ -1,16 +1,40 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
|
1
3
|
module Restforce
|
2
|
-
class Middleware::Logger < Faraday::Response::
|
4
|
+
class Middleware::Logger < Faraday::Response::Middleware
|
5
|
+
extend Forwardable
|
6
|
+
|
7
|
+
def initialize(app, logger = nil, options)
|
8
|
+
super(app)
|
9
|
+
@options = options
|
10
|
+
@logger = logger || begin
|
11
|
+
require 'logger'
|
12
|
+
::Logger.new(STDOUT)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def_delegators :@logger, :debug, :info, :warn, :error, :fatal
|
17
|
+
|
3
18
|
def call(env)
|
4
|
-
|
5
|
-
|
6
|
-
|
19
|
+
debug('request') do
|
20
|
+
dump url: env[:url].to_s,
|
21
|
+
method: env[:method],
|
22
|
+
headers: env[:request_headers],
|
23
|
+
body: env[:body]
|
24
|
+
end
|
7
25
|
super
|
8
26
|
end
|
9
27
|
|
10
28
|
def on_complete(env)
|
11
|
-
|
12
|
-
|
13
|
-
|
29
|
+
debug('response') do
|
30
|
+
dump status: env[:status].to_s,
|
31
|
+
headers: env[:response_headers],
|
32
|
+
body: env[:body]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def dump(hash)
|
37
|
+
"\n" + hash.map { |k, v| " #{k}: #{v.inspect}" }.join("\n")
|
14
38
|
end
|
15
39
|
end
|
16
40
|
end
|
data/lib/restforce/version.rb
CHANGED
data/spec/lib/client_spec.rb
CHANGED
@@ -179,7 +179,10 @@ shared_examples_for 'methods' do
|
|
179
179
|
end
|
180
180
|
|
181
181
|
describe '.update' do
|
182
|
-
|
182
|
+
context 'with missing Id' do
|
183
|
+
subject { client.update('Account', Name: 'Foobar') }
|
184
|
+
specify { expect { subject }.to raise_error RuntimeError, 'Id field missing.' }
|
185
|
+
end
|
183
186
|
|
184
187
|
context 'with invalid Id' do
|
185
188
|
before do
|
@@ -371,6 +374,27 @@ shared_examples_for 'methods' do
|
|
371
374
|
subject { client.without_caching { client.query('SELECT some, fields FROM object') } }
|
372
375
|
it { should be_an Array }
|
373
376
|
end
|
377
|
+
|
378
|
+
describe '.faye' do
|
379
|
+
subject { client.send(:faye) }
|
380
|
+
|
381
|
+
context 'with missing oauth token' do
|
382
|
+
let(:instance_url) { 'foobar' }
|
383
|
+
let(:oauth_token) { nil }
|
384
|
+
specify { expect { subject }.to raise_error RuntimeError, 'OAuth token missing. Call .authenticate! first.' }
|
385
|
+
end
|
386
|
+
|
387
|
+
context 'with missing instance url' do
|
388
|
+
let(:instance_url) { nil }
|
389
|
+
specify { expect { subject }.to raise_error RuntimeError, 'Instance URL missing. Call .authenticate! first.' }
|
390
|
+
end
|
391
|
+
|
392
|
+
context 'with oauth token and instance url' do
|
393
|
+
let(:instance_url) { 'foo' }
|
394
|
+
let(:oauth_token) { 'bar' }
|
395
|
+
specify { expect { subject }.to_not raise_error }
|
396
|
+
end
|
397
|
+
end
|
374
398
|
end
|
375
399
|
|
376
400
|
describe 'with mashify middleware' do
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Restforce::Middleware::Logger do
|
4
|
+
let(:app) { double('app') }
|
5
|
+
let(:env) { { } }
|
6
|
+
let(:logger) { double('logger') }
|
7
|
+
let(:options) { { host: 'login.salesforce.com', client_secret: 'foo', password: 'bar' } }
|
8
|
+
let(:middleware) { described_class.new app, logger, options }
|
9
|
+
|
10
|
+
describe 'logging' do
|
11
|
+
before do
|
12
|
+
app.should_receive(:call).once.and_return(app)
|
13
|
+
app.should_receive(:on_complete).once { middleware.on_complete(env) }
|
14
|
+
logger.should_receive(:debug).with('request')
|
15
|
+
logger.should_receive(:debug).with('response')
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'logs the request and response' do
|
19
|
+
middleware.call(env)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: restforce
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
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-09-
|
12
|
+
date: 2012-09-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -229,6 +229,7 @@ files:
|
|
229
229
|
- spec/lib/middleware/authentication_spec.rb
|
230
230
|
- spec/lib/middleware/authorization_spec.rb
|
231
231
|
- spec/lib/middleware/instance_url_spec.rb
|
232
|
+
- spec/lib/middleware/logger_spec.rb
|
232
233
|
- spec/lib/middleware/mashify_spec.rb
|
233
234
|
- spec/lib/middleware/raise_error_spec.rb
|
234
235
|
- spec/lib/sobject_spec.rb
|
@@ -301,6 +302,7 @@ test_files:
|
|
301
302
|
- spec/lib/middleware/authentication_spec.rb
|
302
303
|
- spec/lib/middleware/authorization_spec.rb
|
303
304
|
- spec/lib/middleware/instance_url_spec.rb
|
305
|
+
- spec/lib/middleware/logger_spec.rb
|
304
306
|
- spec/lib/middleware/mashify_spec.rb
|
305
307
|
- spec/lib/middleware/raise_error_spec.rb
|
306
308
|
- spec/lib/sobject_spec.rb
|