class UserEngage::Client

Public Class Methods

new(configuration) click to toggle source

Instance methods ##

# File lib/user_engage/client.rb, line 10
def initialize(configuration)
  @configuration = configuration
end

Public Instance Methods

delete(path) click to toggle source

Public: Calls the base_url with the given path and parameters

# File lib/user_engage/client.rb, line 22
def delete(path)
  request(:delete, path)
end
get(path, parameters = {}) click to toggle source

Public: Calls the base_url with the given path and parameters

# File lib/user_engage/client.rb, line 16
def get(path, parameters = {})
  request(:get, path, parameters)
end
post(path, parameters = {}) click to toggle source

Public: Calls the base_url with the given path and parameters

# File lib/user_engage/client.rb, line 28
def post(path, parameters = {})
  request(:post, path, parameters)
end
put(path, parameters = {}) click to toggle source

Public: Calls the base_url with the given path and parameters

# File lib/user_engage/client.rb, line 34
def put(path, parameters = {})
  request(:put, path, parameters)
end

Private Instance Methods

connection() click to toggle source

Private methods ##

# File lib/user_engage/client.rb, line 43
def connection
  Faraday.new(url: host)
end
host() click to toggle source
# File lib/user_engage/client.rb, line 74
def host
  @configuration.host || 'https://app.userengage.com/'
end
json_body_call(method, path, parameters) click to toggle source
# File lib/user_engage/client.rb, line 65
def json_body_call(method, path, parameters)
  connection.public_send(method, path) do |request|
    request.headers['Authorization'] = "Token #{@configuration.token}"
    request.headers['Content-Type'] = 'application/json'
    request.headers['User-Agent'] = "UserEngage-Ruby/#{UserEngage::VERSION}"
    request.body = parameters.to_json
  end
end
path_params_call(method, path, parameters) click to toggle source
# File lib/user_engage/client.rb, line 57
def path_params_call(method, path, parameters)
  connection.public_send(method, path, parameters) do |request|
    request.headers['Authorization'] = "Token #{@configuration.token}"
    request.headers['Content-Type'] = 'application/json'
    request.headers['User-Agent'] = "UserEngage-Ruby/#{UserEngage::VERSION}"
  end
end
request(method, action_path, parameters = nil) click to toggle source
# File lib/user_engage/client.rb, line 47
def request(method, action_path, parameters = nil)
  path = action_path.match?(/^https?/) ?
    action_path :
    "api/public#{action_path}"

  %i[post put patch].include?(method) ?
    json_body_call(method, path, parameters) :
    path_params_call(method, path, parameters)
end