salesforceapi-rest 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/salesforceapi-rest.rb +30 -19
- data/lib/salesforceapi-rest/version.rb +1 -1
- metadata +1 -1
data/lib/salesforceapi-rest.rb
CHANGED
@@ -26,28 +26,20 @@ module Salesforceapi
|
|
26
26
|
headers (auth_setting)
|
27
27
|
end
|
28
28
|
|
29
|
-
def initialize(
|
30
|
-
@
|
31
|
-
@
|
29
|
+
def initialize(refresh_token, metadata_uri, client_id, client_secret)
|
30
|
+
@refresh_token = refresh_token
|
31
|
+
@client_id = client_id
|
32
|
+
@client_secret = client_secret
|
32
33
|
@metadata_uri = metadata_uri
|
33
|
-
@api_version =
|
34
|
-
@full_url = instance_uri + "/services/data/#{api_version}/sobjects"
|
34
|
+
@api_version = "v21.0"
|
35
35
|
@ssl_port = 443 # TODO, right SF use port 443 for all HTTPS traffic.
|
36
36
|
|
37
|
-
|
38
|
-
# To be used by HTTParty
|
39
|
-
@auth_header = {
|
40
|
-
"Authorization" => "OAuth " + @oauth_token,
|
41
|
-
"content-Type" => 'application/json'
|
42
|
-
}
|
43
|
-
|
44
|
-
self.class.base_uri @instance_uri
|
45
|
-
|
46
37
|
end
|
47
38
|
|
48
39
|
|
49
40
|
def create(object, attributes)
|
50
41
|
|
42
|
+
config_authorization!
|
51
43
|
path = "/services/data/#{@api_version}/sobjects/#{object}/"
|
52
44
|
target = @instance_uri + path
|
53
45
|
|
@@ -68,6 +60,7 @@ module Salesforceapi
|
|
68
60
|
|
69
61
|
def describe(object)
|
70
62
|
path = "/services/data/#{@api_version}/sobjects/#{object}/describe"
|
63
|
+
config_authorization!
|
71
64
|
target = @instance_uri + path
|
72
65
|
|
73
66
|
self.class.base_uri @instance_uri
|
@@ -83,6 +76,7 @@ module Salesforceapi
|
|
83
76
|
|
84
77
|
def resources
|
85
78
|
path = "/services/data/#{@api_version}"
|
79
|
+
config_authorization!
|
86
80
|
target = @instance_uri + path
|
87
81
|
|
88
82
|
self.class.base_uri @instance_uri
|
@@ -96,25 +90,42 @@ module Salesforceapi
|
|
96
90
|
end
|
97
91
|
end
|
98
92
|
|
93
|
+
def config_authorization!
|
94
|
+
target = target = "https://login.salesforce.com/services/oauth2/token?grant_type=refresh_token&client_id=#{@client_id}&client_secret=#{@client_secret}&refresh_token=#{@refresh_token}"
|
95
|
+
resp = SalesforceApi::Request.do_request("POST", target, {"content-Type" => 'application/json'}, nil)
|
96
|
+
if (resp.code != 200) || !resp.success?
|
97
|
+
message = ActiveSupport::JSON.decode(resp.body)["error_description"]
|
98
|
+
SalesforceApi::Errors::ErrorManager.raise_error(message, 401)
|
99
|
+
else
|
100
|
+
response = ActiveSupport::JSON.decode(resp.body)
|
101
|
+
end
|
102
|
+
@instance_uri = response['instance_url']
|
103
|
+
@access_token = response['access_token']
|
104
|
+
@auth_header = {
|
105
|
+
"Authorization" => "OAuth " + @access_token,
|
106
|
+
"content-Type" => 'application/json'
|
107
|
+
}
|
108
|
+
end
|
109
|
+
|
99
110
|
|
100
111
|
def add_custom_field(attributes)
|
112
|
+
config_authorization!
|
101
113
|
auth_header = {
|
102
|
-
"Authorization" => "OAuth " + @
|
114
|
+
"Authorization" => "OAuth " + @access_token,
|
103
115
|
'Connection' => 'Keep-Alive',
|
104
116
|
'Content-Type' => 'text/xml',
|
105
117
|
'SOAPAction' => '""'
|
106
118
|
}
|
119
|
+
|
107
120
|
self.class.base_uri @metadata_uri
|
108
121
|
|
109
|
-
data = (Envelope % [@
|
122
|
+
data = (Envelope % [@access_token, custom_fields_xml(attributes)])
|
110
123
|
resp = SalesforceApi::Request.do_request("POST", @metadata_uri, auth_header, data.lstrip)
|
111
124
|
|
112
125
|
xml_response = Crack::XML.parse(resp.body)
|
113
126
|
|
114
127
|
if resp.code != 200
|
115
|
-
|
116
|
-
SalesforceApi::Errors::ErrorManager.raise_error("HTTP code " + resp.code.to_s + ": " + message, resp.code)
|
117
|
-
|
128
|
+
SalesforceApi::Errors::ErrorManager.raise_error("HTTP code " + resp.code.to_s + " xml response: " + resp.body, resp.code)
|
118
129
|
else
|
119
130
|
return xml_response
|
120
131
|
end
|