Class: SFRest::Connection
- Inherits:
-
Object
- Object
- SFRest::Connection
- Defined in:
- lib/sfrest/connection.rb
Overview
Generic http methods accessors for all the sub classes.
Constant Summary collapse
- REST_METHODS =
%w[audit backup centralized_role_management codebase collection cron domains factory_standard_domain group info profile role security_settings site site_guard site_ownership site_update_priority stage task task_log_settings theme update usage user variable].freeze
Instance Attribute Summary collapse
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#password ⇒ Object
Returns the value of attribute password.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
-
#access_check(data, http_status) ⇒ Object
Throws an SFRest exception for requests that have problems The cyclomatic complexity check is being ignored here because we are collecting all the possible exception raising cases.
-
#api_response(res, return_status: false) ⇒ Array|Object
Confirm that the result looks adequate.
-
#delete(uri, payload = '') ⇒ Object
http request via delete.
-
#get(uri) ⇒ Object
http request via get.
-
#get_with_status(uri) ⇒ Integer, Object
http request via get.
-
#initialize(url, user, password) ⇒ Connection
constructor
A new instance of Connection.
-
#ping ⇒ Object
pings the SF api as an authenticated user responds with a pong.
-
#post(uri, payload) ⇒ Object
http request via post.
-
#put(uri, payload) ⇒ Object
http request via put.
-
#service_response ⇒ Object
Pings to retrieve a service response.
Constructor Details
#initialize(url, user, password) ⇒ Connection
Returns a new instance of Connection.
12 13 14 15 16 |
# File 'lib/sfrest/connection.rb', line 12 def initialize(url, user, password) @base_url = url @username = user @password = password end |
Instance Attribute Details
#base_url ⇒ Object
Returns the value of attribute base_url.
7 8 9 |
# File 'lib/sfrest/connection.rb', line 7 def base_url @base_url end |
#password ⇒ Object
Returns the value of attribute password.
7 8 9 |
# File 'lib/sfrest/connection.rb', line 7 def password @password end |
#username ⇒ Object
Returns the value of attribute username.
7 8 9 |
# File 'lib/sfrest/connection.rb', line 7 def username @username end |
Instance Method Details
#access_check(data, http_status) ⇒ Object
Throws an SFRest exception for requests that have problems The cyclomatic complexity check is being ignored here because we are collecting all the possible exception raising cases. rubocop: disable Metrics/CyclomaticComplexity
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# 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 = "Status: #{http_status}, Message: #{data['message']}" raise SFRest::SFError, end end data end |
#api_response(res, return_status: false) ⇒ Array|Object
Confirm that the result looks adequate
103 104 105 106 107 108 109 |
# 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 = "Invalid data, status #{res.status}, body: #{res.body}" raise SFRest::InvalidResponse, end |
#delete(uri, payload = '') ⇒ Object
http request via delete
86 87 88 89 90 91 92 93 94 95 |
# 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 |
#get(uri) ⇒ Object
http request via get
23 24 25 26 27 28 29 30 31 |
# 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 |
#get_with_status(uri) ⇒ Integer, Object
http request via get
38 39 40 41 42 43 44 45 46 |
# 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 |
#ping ⇒ Object
pings the SF api as an authenticated user responds with a pong
147 148 149 |
# File 'lib/sfrest/connection.rb', line 147 def ping get('/api/v1/ping') end |
#post(uri, payload) ⇒ Object
http request via post
53 54 55 56 57 58 59 60 61 62 |
# 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 |
#put(uri, payload) ⇒ Object
http request via put
69 70 71 72 73 74 75 76 77 78 |
# 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 |
#service_response ⇒ Object
Pings to retrieve a service response.
152 153 154 |
# File 'lib/sfrest/connection.rb', line 152 def service_response ping end |