Class: FreshdeskAPI::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/freshdesk_api/client.rb

Overview

The top-level class that handles configuration and connection to the Freshdesk API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|config| ... } ⇒ Client

Creates a new FreshdeskAPI::Client instance and yields #config.

Requires a block to be given.

Does basic configuration constraints:

Yields:

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
51
# File 'lib/freshdesk_api/client.rb', line 42

def initialize
  raise ArgumentError, 'block not given' unless block_given?

  @config = FreshdeskAPI::Configuration.new

  yield config

  check_url
  set_default_logger
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Collection

Handles resources such as ‘tickets’.

Returns:

  • (Collection)

    Collection instance for resource



28
29
30
31
32
33
34
# File 'lib/freshdesk_api/client.rb', line 28

def method_missing(method, *args)
  method = method.to_s
  method_class = method_as_class(method)
  options = args.last.is_a?(Hash) ? args.pop : {}

  FreshdeskAPI::Collection.new(self, method_class, options)
end

Instance Attribute Details

#configConfiguration (readonly)

Returns Config instance.

Returns:



24
25
26
# File 'lib/freshdesk_api/client.rb', line 24

def config
  @config
end

Instance Method Details

#connectionRestClient::Resouce

Creates a connection if there is none, otherwise returns the existing connection.

Returns:

  • (RestClient::Resouce)

    RestClient connection for the client



56
57
58
# File 'lib/freshdesk_api/client.rb', line 56

def connection
  @connection ||= build_connection
end

#make_request!(path, method, options = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/freshdesk_api/client.rb', line 60

def make_request!(path, method, options = {})
  response = nil
  connection[path].send(method, options) do |resp, _req, _result|
    case resp.code
    when 302
      # Connection to the server failed. Please check username/password
      raise Error::NotAcceptable
    when 404
      raise Error::ResourceNotFound
    when 406
      raise Error::NotAcceptable
    when 400...600
      raise Error::NetworkError
    end
    response = resp
  end
  response
rescue StandardError => e
  raise Error::ClientError, e
end