class HelpscoutApi::Client

Constants

API_VERSION
HEADERS

Attributes

api_token[RW]
auth_params[RW]

Public Class Methods

new() click to toggle source
# File lib/helpscout_api/client.rb, line 22
def initialize
  @auth_params = { username: self.class.api_token, password: "X" }
end

Public Instance Methods

conversations() click to toggle source
# File lib/helpscout_api/client.rb, line 45
def conversations
  HelpscoutApi::Conversation.new(self, @auth_params)
end
create(**params) click to toggle source
# File lib/helpscout_api/client.rb, line 36
def create(**params)
  response = self.class.post "#{resource_path}.json", basic_auth: @auth_params, body: params.to_json
  handle response
end
get(id, **params) click to toggle source
# File lib/helpscout_api/client.rb, line 31
def get(id, **params)
  response = self.class.get "#{resource_path}/#{id}.json", basic_auth: @auth_params, query: params
  handle response
end
handle(response) click to toggle source
# File lib/helpscout_api/client.rb, line 62
def handle response
  case response.code
  when 200..299
    response.parsed_response
  when 400..404
    raise HTTParty::Error, response.parsed_response['error']
  when 500..600
    raise HTTParty::Error, response.parsed_response['error']
  else
    raise StandardError, 'Unknown error'
  end
end
list(**params) click to toggle source
# File lib/helpscout_api/client.rb, line 26
def list(**params)
  response = self.class.get "#{resource_path}.json", basic_auth: @auth_params, query: params
  handle response
end
mailboxes() click to toggle source
# File lib/helpscout_api/client.rb, line 41
def mailboxes
  HelpscoutApi::Mailbox.new(self, @auth_params)
end
resource_path() click to toggle source
# File lib/helpscout_api/client.rb, line 49
def resource_path
  klass = self.class.name.split('::').last
  klass[0] = klass[0].chr.downcase

  if klass.end_with?('y')
    "/#{klass.chop}ies"
  elsif klass.end_with?('x')
    "/#{klass}es"
  else
    "/#{klass}s"
  end
end