class Setka::Workflow::Client

Public Class Methods

new(attrs = {}) click to toggle source
# File lib/setka/workflow/client.rb, line 13
def initialize(attrs = {})
  self.configuration.attributes = attrs
end

Public Instance Methods

configuration() click to toggle source
# File lib/setka/workflow/client.rb, line 33
def configuration
  @configuration ||= Configuration.new
end
configure() { |configuration| ... } click to toggle source
# File lib/setka/workflow/client.rb, line 29
def configure
  yield configuration if block_given?
end
delete(path, body = nil, options) click to toggle source
# File lib/setka/workflow/client.rb, line 25
def delete(path, body = nil, options)
  invoke_verb(:delete, uri(path), body, with_auth(options))
end
patch(path, body = nil, options) click to toggle source
# File lib/setka/workflow/client.rb, line 21
def patch(path, body = nil, options)
  invoke_verb(:patch, uri(path), body, with_auth(options))
end
post(path, body, options) click to toggle source
# File lib/setka/workflow/client.rb, line 17
def post(path, body, options)
  invoke_verb(:post, uri(path), body, with_auth(options))
end

Private Instance Methods

invoke_verb(name, uri, body, options) click to toggle source
# File lib/setka/workflow/client.rb, line 39
def invoke_verb(name, uri, body, options)
  configuration.credentials?

  request = Request.new(name, uri, body, options)
  response = request.execute

  return '' unless response

  if response.code.to_i == 401
    Setka::Workflow.logger.error("[401 #{name.to_s.upcase} #{uri}]: "\
      "#{response.body['message']}.")

    raise InvalidAccessToken, response.body[:message]
  end

  if response.code.to_i == 500
    Setka::Workflow.logger.error("[500 #{name.to_s.upcase} #{uri}]: "\
      "#{response.body['message']}.")

    raise InternalServerError, response.body[:message]
  end

  unless [200, 201].include? response.code.to_i
    Setka::Workflow.logger.error(
      "[#{response.code} #{name.to_s.upcase} #{uri}]: "\
        "#{response.body['message']}."
    )

    raise Error, response.body
  end

  response.body
end
uri(path) click to toggle source
# File lib/setka/workflow/client.rb, line 73
def uri(path)
  URI::HTTPS.build(
    host: BASE_ENDPOINT,
    path: "/eapi/v#{API_VERSION}/#{space_name}/#{path}"
  )
end
with_auth(options) click to toggle source
# File lib/setka/workflow/client.rb, line 80
def with_auth(options)
  options.merge(headers: { authorization: "token #{access_token}" })
end