restforce 1.0.6 → 1.1.0

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/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## 1.1.0 (Mar 3, 2013)
2
+
3
+ * Added ability to download attachments easily.
4
+
5
+ Example
6
+
7
+ attachment = client.query('select Id, Name, Body from Attachment').first
8
+ File.open(attachment.Name, 'wb') { |f| f.write(attachment.Body) }
9
+
1
10
  ## 1.0.6 (Feb 16, 2013)
2
11
 
3
12
  * Added `url` method.
data/README.md CHANGED
@@ -275,6 +275,17 @@ _See also: http://www.salesforce.com/us/developer/docs/api_rest/Content/dome_sob
275
275
 
276
276
  * * *
277
277
 
278
+ ### Downloading Attachments
279
+
280
+ Restforce also makes it incredibly easy to download Attachments:
281
+
282
+ ```ruby
283
+ attachment = client.query('select Id, Name, Body from Attachment').first
284
+ File.open(attachment.Name, 'wb') { |f| f.write(attachment.Body) }
285
+ ```
286
+
287
+ * * *
288
+
278
289
  ### Custom Apex REST endpoints
279
290
 
280
291
  You can use Restforce to interact with your custom REST endpoints, by using
data/lib/restforce.rb CHANGED
@@ -9,6 +9,7 @@ module Restforce
9
9
  autoload :SignedRequest, 'restforce/signed_request'
10
10
  autoload :Collection, 'restforce/collection'
11
11
  autoload :Middleware, 'restforce/middleware'
12
+ autoload :Attachment, 'restforce/attachment'
12
13
  autoload :UploadIO, 'restforce/upload_io'
13
14
  autoload :SObject, 'restforce/sobject'
14
15
  autoload :Client, 'restforce/client'
@@ -0,0 +1,23 @@
1
+ module Restforce
2
+ class Attachment < Restforce::SObject
3
+
4
+ # Public: Returns the body of the attachment.
5
+ #
6
+ # Examples
7
+ #
8
+ # attachment = client.query('select Id, Name, Body from Attachment').first
9
+ # File.open(attachment.Name, 'wb') { |f| f.write(attachment.Body) }
10
+ def Body
11
+ ensure_id && ensure_body
12
+ @client.get(super).body
13
+ end
14
+
15
+ private
16
+
17
+ def ensure_body
18
+ return true if self.Body?
19
+ raise 'You need to query the Body for the record first.'
20
+ end
21
+
22
+ end
23
+ end
@@ -33,7 +33,7 @@ module Restforce
33
33
  # Ensures the instance url is set.
34
34
  builder.use Restforce::Middleware::InstanceURL, self, @options
35
35
  # Parses returned JSON response into a hash.
36
- builder.response :json
36
+ builder.response :json, :content_type => /\bjson$/
37
37
  # Caches GET requests.
38
38
  builder.use Restforce::Middleware::Caching, cache, @options if cache
39
39
  # Follows 30x redirects.
@@ -26,9 +26,13 @@ module Restforce
26
26
  # of sobject records.
27
27
  Restforce::Collection
28
28
  elsif val.has_key? 'attributes'
29
- # When the hash contains an attributes key, it should be considered an
30
- # sobject record
31
- Restforce::SObject
29
+ if val['attributes']['type'] == 'Attachment'
30
+ Restforce::Attachment
31
+ else
32
+ # When the hash contains an attributes key, it should be considered an
33
+ # sobject record
34
+ Restforce::SObject
35
+ end
32
36
  else
33
37
  # Fallback to a standard Restforce::Mash for everything else
34
38
  Restforce::Mash
@@ -56,7 +56,8 @@ module Restforce
56
56
  private
57
57
 
58
58
  def ensure_id
59
- raise 'You need to query the Id for the record first.' unless self.Id?
59
+ return true if self.Id?
60
+ raise 'You need to query the Id for the record first.'
60
61
  end
61
62
 
62
63
  end
@@ -1,3 +1,3 @@
1
1
  module Restforce
2
- VERSION = '1.0.6'
2
+ VERSION = '1.1.0'
3
3
  end
data/restforce.gemspec CHANGED
@@ -21,7 +21,7 @@ Gem::Specification.new do |gem|
21
21
  gem.add_dependency 'hashie', '~> 1.2.0'
22
22
 
23
23
  gem.add_development_dependency 'rspec', '~> 2.12.0'
24
- gem.add_development_dependency 'webmock'
25
- gem.add_development_dependency 'simplecov'
24
+ gem.add_development_dependency 'webmock', '~> 1.9.0'
25
+ gem.add_development_dependency 'simplecov', '~> 0.7.1'
26
26
  gem.add_development_dependency 'faye' unless RUBY_PLATFORM == 'java'
27
27
  end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe Restforce::Attachment do
4
+ include_context 'basic client'
5
+
6
+ let(:body_url) { '/services/data/v26.0/sobjects/Attachment/00PG0000006Hll5MAC/Body' }
7
+ let(:hash) { { 'Id' => '1234', 'Body' => body_url } }
8
+ let(:sobject) do
9
+ described_class.new(hash, client)
10
+ end
11
+
12
+ describe '.Body' do
13
+ it 'requests the body' do
14
+ client.should_receive(:get).with(body_url).and_return(double('response').as_null_object)
15
+ sobject.Body
16
+ end
17
+ end
18
+ end
@@ -439,8 +439,8 @@ shared_examples_for 'methods' do
439
439
  before do
440
440
  @query = stub_api_request('query\?q=SELECT%20some,%20fields%20FROM%20object').
441
441
  with(:headers => { 'Authorization' => "OAuth #{oauth_token}" }).
442
- to_return(:status => 401, :body => fixture('expired_session_response')).then.
443
- to_return(:status => 200, :body => fixture('sobject/query_success_response'))
442
+ to_return(:status => 401, :body => fixture('expired_session_response'), :headers => { 'Content-Type' => 'application/json' }).then.
443
+ to_return(:status => 200, :body => fixture('sobject/query_success_response'), :headers => { 'Content-Type' => 'application/json' })
444
444
 
445
445
  @login = stub_login_request(:with_body => "grant_type=password&client_id=client_id&client_secret=" \
446
446
  "client_secret&username=foo&password=barsecurity_token").
@@ -19,8 +19,13 @@ describe Restforce::Mash do
19
19
  end
20
20
 
21
21
  context 'when the hash has an "attributes" key' do
22
- let(:input) { { 'attributes' => nil } }
22
+ let(:input) { { 'attributes' => { 'type' => 'Account' } } }
23
23
  it { should eq Restforce::SObject }
24
+
25
+ context 'when the sobject type is an Attachment' do
26
+ let(:input) { { 'attributes' => { 'type' => 'Attachment' } } }
27
+ it { should eq Restforce::Attachment }
28
+ end
24
29
  end
25
30
 
26
31
  context 'else' do
@@ -10,7 +10,7 @@ module FixtureHelpers
10
10
 
11
11
  stub = stub_request(options[:method], %r{/services/data/v#{options[:api_version]}/#{endpoint}})
12
12
  stub = stub.with(:body => options[:with_body]) if options[:with_body] && !RUBY_VERSION.match(/^1.8/)
13
- stub = stub.to_return(:status => options[:status], :body => fixture(options[:fixture])) if options[:fixture]
13
+ stub = stub.to_return(:status => options[:status], :body => fixture(options[:fixture]), :headers => { 'Content-Type' => 'application/json'}) if options[:fixture]
14
14
  stub
15
15
  end
16
16
 
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: 1.0.6
4
+ version: 1.1.0
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: 2013-02-17 00:00:00.000000000 Z
12
+ date: 2013-03-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
@@ -96,33 +96,33 @@ dependencies:
96
96
  requirement: !ruby/object:Gem::Requirement
97
97
  none: false
98
98
  requirements:
99
- - - ! '>='
99
+ - - ~>
100
100
  - !ruby/object:Gem::Version
101
- version: '0'
101
+ version: 1.9.0
102
102
  type: :development
103
103
  prerelease: false
104
104
  version_requirements: !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
- - - ! '>='
107
+ - - ~>
108
108
  - !ruby/object:Gem::Version
109
- version: '0'
109
+ version: 1.9.0
110
110
  - !ruby/object:Gem::Dependency
111
111
  name: simplecov
112
112
  requirement: !ruby/object:Gem::Requirement
113
113
  none: false
114
114
  requirements:
115
- - - ! '>='
115
+ - - ~>
116
116
  - !ruby/object:Gem::Version
117
- version: '0'
117
+ version: 0.7.1
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  none: false
122
122
  requirements:
123
- - - ! '>='
123
+ - - ~>
124
124
  - !ruby/object:Gem::Version
125
- version: '0'
125
+ version: 0.7.1
126
126
  - !ruby/object:Gem::Dependency
127
127
  name: faye
128
128
  requirement: !ruby/object:Gem::Requirement
@@ -155,6 +155,7 @@ files:
155
155
  - README.md
156
156
  - Rakefile
157
157
  - lib/restforce.rb
158
+ - lib/restforce/attachment.rb
158
159
  - lib/restforce/client.rb
159
160
  - lib/restforce/client/api.rb
160
161
  - lib/restforce/client/authentication.rb
@@ -215,6 +216,7 @@ files:
215
216
  - spec/fixtures/sobject/upsert_multiple_error_response.json
216
217
  - spec/fixtures/sobject/upsert_updated_success_response.json
217
218
  - spec/fixtures/sobject/write_error_response.json
219
+ - spec/lib/attachment_spec.rb
218
220
  - spec/lib/client_spec.rb
219
221
  - spec/lib/collection_spec.rb
220
222
  - spec/lib/config_spec.rb
@@ -250,7 +252,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
250
252
  version: '0'
251
253
  segments:
252
254
  - 0
253
- hash: -3883981851438487723
255
+ hash: 3130418594951849244
254
256
  required_rubygems_version: !ruby/object:Gem::Requirement
255
257
  none: false
256
258
  requirements:
@@ -259,7 +261,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
259
261
  version: '0'
260
262
  segments:
261
263
  - 0
262
- hash: -3883981851438487723
264
+ hash: 3130418594951849244
263
265
  requirements: []
264
266
  rubyforge_project:
265
267
  rubygems_version: 1.8.23
@@ -298,6 +300,7 @@ test_files:
298
300
  - spec/fixtures/sobject/upsert_multiple_error_response.json
299
301
  - spec/fixtures/sobject/upsert_updated_success_response.json
300
302
  - spec/fixtures/sobject/write_error_response.json
303
+ - spec/lib/attachment_spec.rb
301
304
  - spec/lib/client_spec.rb
302
305
  - spec/lib/collection_spec.rb
303
306
  - spec/lib/config_spec.rb