class SFRest::Connection
Generic http methods accessors for all the sub classes.
Constants
- REST_METHODS
define the other class accessor methods. this will instantiate the class with the set creds and make it possible to do
sfa = SFRest.new url, user, password sfa.ping sfa.site.first_site_id
If a new class is added, add the accessor to this list. NOTE: accessor == Class_name.to_lower
Attributes
Public Class Methods
@param [String] url base url of the SF endpoint e.g. www.sfdev.acsitefactory.com @param [String] user api user @param [String] password api password
# File lib/sfrest/connection.rb, line 12 def initialize(url, user, password) @base_url = url @username = user @password = password end
Public Instance Methods
Throws an SFRest
exception for requests that have problems @param [Object] data JSON parsed http reponse of the SFApi @param [int] http_status the request's HTTP status @return [Object] the data object if there are no issues @raise [SFRest::AccessDeniedError] if Authentication fails @raise [SFRest::ActionForbiddenError] if the users role cannot perform the request @raise [SFRest::BadRequestError] if there is something malformed in the request @raise [SFRest::UnprocessableEntity] if there is an unprocessable entity in the request @raise [SFRest::SFError] if the response HTTP status indicates an error but without the above qualifiers
The cyclomatic complexity check is being ignored here because we are collecting all the possible exception raising cases. rubocop: disable Metrics/CyclomaticComplexity
# File lib/sfrest/connection.rb, line 124 def access_check(data, http_status) if data.is_a?(Hash) && !data['message'].nil? case data['message'] when /Access denied|Access Denied/ raise SFRest::AccessDeniedError, data['message'] when /Forbidden: / raise SFRest::ActionForbiddenError, data['message'] when /Bad Request:/ raise SFRest::BadRequestError, data['message'] when /Unprocessible Entity: |Unprocessable Entity: / raise SFRest::UnprocessableEntity, data['message'] end if http_status >= 400 && http_status <= 599 sf_err_message = "Status: #{http_status}, Message: #{data['message']}" raise SFRest::SFError, sf_err_message end end data end
Confirm that the result looks adequate @param [Excon::Response] res @param [Boolean] return_status If true returns the integer status
with the reponse
@return [Array|Object] if return_status then [int, Object]
else Object
# File lib/sfrest/connection.rb, line 103 def api_response(res, return_status: false) data = access_check JSON(res.body), res.status return_status ? [res.status, data] : data rescue JSON::ParserError message = "Invalid data, status #{res.status}, body: #{res.body}" raise SFRest::InvalidResponse, message end
http request via delete @param [string] uri @param [string] payload - This string will be supplied in the body of the request, similar to POST. @return [Object] ruby representation of the json response
if the reponse body does not parse, raises a SFRest::InvalidResponse
# File lib/sfrest/connection.rb, line 86 def delete(uri, payload = '') headers = { 'Content-Type' => 'application/json' } res = Excon.delete(@base_url + uri.to_s, headers: headers, user: username, password: password, ssl_verify_peer: false, body: payload) api_response res end
http request via get @param [string] uri @return [Object] ruby representation of the json response
if the reponse body does not parse, raises a SFRest::InvalidResponse
# File lib/sfrest/connection.rb, line 23 def get(uri) headers = { 'Content-Type' => 'application/json' } res = Excon.get(@base_url + uri.to_s, headers: headers, user: username, password: password, ssl_verify_peer: false) api_response res end
http request via get @param [string] uri @return [Integer, Object] http status and the ruby representation
if the reponse body does not parse, raises a SFRest::InvalidResponse
# File lib/sfrest/connection.rb, line 38 def get_with_status(uri) headers = { 'Content-Type' => 'application/json' } res = Excon.get(@base_url + uri.to_s, headers: headers, user: username, password: password, ssl_verify_peer: false) api_response res, return_status: true end
pings the SF api as an authenticated user responds with a pong
# File lib/sfrest/connection.rb, line 147 def ping get('/api/v1/ping') end
http request via post @param [string] uri @return [Object] ruby representation of the json response
if the reponse body does not parse, raises a SFRest::InvalidResponse
# File lib/sfrest/connection.rb, line 53 def post(uri, payload) headers = { 'Content-Type' => 'application/json' } res = Excon.post(@base_url + uri.to_s, headers: headers, user: username, password: password, ssl_verify_peer: false, body: payload) api_response res end
http request via put @param [string] uri @return [Object] ruby representation of the json response
if the reponse body does not parse, raises a SFRest::InvalidResponse
# File lib/sfrest/connection.rb, line 69 def put(uri, payload) headers = { 'Content-Type' => 'application/json' } res = Excon.put(@base_url + uri.to_s, headers: headers, user: username, password: password, ssl_verify_peer: false, body: payload) api_response res end
Pings to retrieve a service response.
# File lib/sfrest/connection.rb, line 152 def service_response ping end