Class: SFRest::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/sfrest/connection.rb

Overview

Generic http methods accessors for all the sub classes.

Constant Summary collapse

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

%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

Instance Method Summary collapse

Constructor Details

#initialize(url, user, password) ⇒ Connection

Returns a new instance of Connection.

Parameters:

  • url (String)

    base url of the SF endpoint e.g. www.sfdev.acsitefactory.com

  • user (String)

    api user

  • password (String)

    api password



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_urlObject

Returns the value of attribute base_url.



7
8
9
# File 'lib/sfrest/connection.rb', line 7

def base_url
  @base_url
end

#passwordObject

Returns the value of attribute password.



7
8
9
# File 'lib/sfrest/connection.rb', line 7

def password
  @password
end

#usernameObject

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

Parameters:

  • data (Object)

    JSON parsed http reponse of the SFApi

  • http_status (int)

    the request’s HTTP status

Returns:

  • (Object)

    the data object if there are no issues

Raises:



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
      sf_err_message = "Status: #{http_status}, Message: #{data['message']}"
      raise SFRest::SFError, sf_err_message
    end
  end
  data
end

#api_response(res, return_status: false) ⇒ Array|Object

Confirm that the result looks adequate

Parameters:

  • res (Excon::Response)
  • return_status (Boolean) (defaults to: false)

    If true returns the integer status with the reponse

Returns:

  • (Array|Object)

    if return_status then [int, Object] else Object



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
  message = "Invalid data, status #{res.status}, body: #{res.body}"
  raise SFRest::InvalidResponse, message
end

#delete(uri, payload = '') ⇒ Object

http request via delete

Parameters:

  • uri (string)
  • payload (string) (defaults to: '')
    • This string will be supplied in the body of the request, similar to POST.

Returns:

  • (Object)

    ruby representation of the json response if the reponse body does not parse, raises a SFRest::InvalidResponse



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

Parameters:

  • uri (string)

Returns:

  • (Object)

    ruby representation of the json response if the reponse body does not parse, raises a SFRest::InvalidResponse



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

Parameters:

  • uri (string)

Returns:

  • (Integer, Object)

    http status and the ruby representation if the reponse body does not parse, raises a SFRest::InvalidResponse



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

#pingObject

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

Parameters:

  • uri (string)

Returns:

  • (Object)

    ruby representation of the json response if the reponse body does not parse, raises a SFRest::InvalidResponse



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

Parameters:

  • uri (string)

Returns:

  • (Object)

    ruby representation of the json response if the reponse body does not parse, raises a SFRest::InvalidResponse



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_responseObject

Pings to retrieve a service response.



152
153
154
# File 'lib/sfrest/connection.rb', line 152

def service_response
  ping
end