sf-username-password-authentication-and-rest-api 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -16,3 +16,4 @@ test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
18
  salesforce_config.rb
19
+ .rvmrc
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Purpose
2
2
 
3
- I wanted a simple easy way to interact with the [Salesforce REST API](http://www.salesforce.com/us/developer/docs/api_rest/index_Left.htm) using their [Username-Password OAuth Authentication Flow](http://www.salesforce.com/us/developer/docs/api_rest/Content/intro_understanding_username_password_oauth_flow.htm).
3
+ I wanted a simply easy way to interact with the [Salesforce REST API](http://www.salesforce.com/us/developer/docs/api_rest/index_Left.htm) using their [Username-Password OAuth Authentication Flow](http://www.salesforce.com/us/developer/docs/api_rest/Content/intro_understanding_username_password_oauth_flow.htm).
4
4
 
5
5
  # Goal
6
6
 
@@ -62,8 +62,7 @@ _Note:_ options will be used over ENV variables if both are available.
62
62
  client_id: "client_id",
63
63
  client_secret: "client_secret",
64
64
  host: "test.salesforce.com"
65
-
66
- }
65
+ })
67
66
 
68
67
  ### REST API Examples
69
68
 
@@ -80,7 +79,7 @@ Once you have a SalesforceAPI::Caller instance you can make calls like:
80
79
  caller.query "Select Id, ParentId, OwnerId, Name, Body From Attachment where ParentId = 'a0Ce00000005b9P'"
81
80
 
82
81
  # get a specific attachment as a binary/string
83
- caller.attachment("Attachment", "00Pe0000000IVTyEAO").class
82
+ caller.attachment("Attachment", "00Pe0000000IVTyEAO")
84
83
 
85
84
  Responses are for the most part just JSON which is easily parsed like:
86
85
 
@@ -5,6 +5,13 @@ require "json"
5
5
  require "active_support/core_ext/hash/indifferent_access"
6
6
  require 'salesforce-api/caller'
7
7
 
8
+ # Ruby 1.9.3 introduces "hostname" this provides it for earlier versions of ruby.
9
+ class URI::Generic
10
+ if !URI.respond_to?(:hostname) && URI.respond_to?(:host)
11
+ alias_method :hostname, :host
12
+ end
13
+ end
14
+
8
15
  module SalesforceAPI
9
16
  def self.api_versions host = "na1.salesforce.com"
10
17
  JSON.parse(Net::HTTP.get(host, "/services/data"))
@@ -2,6 +2,7 @@ module SalesforceAPI
2
2
 
3
3
  class ConnectionException < StandardError; end
4
4
  class ErrorReceivedException < StandardError; end
5
+ class Error400 < StandardError; end
5
6
 
6
7
  class Caller
7
8
 
@@ -47,6 +48,23 @@ module SalesforceAPI
47
48
  end
48
49
  end
49
50
 
51
+ def http_patch uri, body, auto_authorize = true
52
+ req = Net::HTTPGenericRequest.new("PATCH", nil, nil, uri.request_uri)
53
+ req["Authorization"] = "Bearer #{access_token}"
54
+ req["Content-Type"] = "application/json"
55
+ req.body = body.to_json
56
+ response = Net::HTTP.start( uri.hostname, uri.port, :use_ssl => true ) do |http|
57
+ http.request( req )
58
+ end
59
+
60
+ if response.instance_of?(Net::HTTPUnauthorized) && auto_authorize
61
+ authorize
62
+ http_patch uri, body, false
63
+ else
64
+ response
65
+ end
66
+ end
67
+
50
68
  def sobject name, id, fields = ""
51
69
  uri = URI("#{instance_url}/services/data/v#{api_version}/sobjects/#{name}/#{id}")
52
70
  uri.query = {:fields => fields}.to_query unless fields.empty?
@@ -69,6 +87,14 @@ module SalesforceAPI
69
87
  }.delete_if{|k,v| v.to_s.empty?} : @token_request_parameters
70
88
  end
71
89
 
90
+ def update name, id, body = {}
91
+ uri = URI("#{instance_url}/services/data/v#{api_version}/sobjects/#{name}/#{id}")
92
+ response = http_patch( uri, body )
93
+ if response.instance_of? Net::HTTPBadRequest
94
+ raise Error400, "The request could not be understood, usually because the JSON or XML body contains an error. Code[#{response.code}]."
95
+ end
96
+ end
97
+
72
98
  private
73
99
 
74
100
  def parse_authorization_response json
@@ -1,3 +1,3 @@
1
1
  module SalesforceAPI
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -79,6 +79,5 @@ describe SalesforceAPI::Caller do
79
79
  response.must_equal mock_response
80
80
  end
81
81
  end
82
-
83
82
  end
84
83
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sf-username-password-authentication-and-rest-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-03 00:00:00.000000000 Z
12
+ date: 2013-01-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &19944440 !ruby/object:Gem::Requirement
16
+ requirement: &23347140 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *19944440
24
+ version_requirements: *23347140
25
25
  description: Easy to use Salesforce REST API caller, using the Username-Password OAuth
26
26
  Authentication Flow.
27
27
  email: