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)


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

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, &block) ⇒ Collection

Handles resources such as ‘tickets’.

Returns:

  • (Collection)

    Collection instance for resource



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

def method_missing(method, *args, &block)
  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:



22
23
24
# File 'lib/freshdesk_api/client.rb', line 22

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



54
55
56
# File 'lib/freshdesk_api/client.rb', line 54

def connection
  @connection ||= build_connection
end

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



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

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