Class: FreshdeskAPI::Client
- Inherits:
-
Object
- Object
- FreshdeskAPI::Client
- 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
-
#config ⇒ Configuration
readonly
Config instance.
Instance Method Summary collapse
-
#connection ⇒ RestClient::Resouce
Creates a connection if there is none, otherwise returns the existing connection.
- #initialize {|config| ... } ⇒ Client constructor
- #make_request!(path, method, options = {}) ⇒ Object
-
#method_missing(method, *args, &block) ⇒ Collection
Handles resources such as ‘tickets’.
Constructor Details
#initialize {|config| ... } ⇒ Client
Creates a new FreshdeskAPI::Client instance and yields #config.
Requires a block to be given.
Does basic configuration constraints:
-
FreshdeskAPI::Configuration#base_url must be https unless FreshdeskAPI::Configuration#allow_http is set.
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’.
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) = args.last.is_a?(Hash) ? args.pop : {} FreshdeskAPI::Collection.new(self, method_class, ) end |
Instance Attribute Details
#config ⇒ Configuration (readonly)
Returns Config instance.
22 23 24 |
# File 'lib/freshdesk_api/client.rb', line 22 def config @config end |
Instance Method Details
#connection ⇒ RestClient::Resouce
Creates a connection if there is none, otherwise returns the existing connection.
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, = {}) response = nil connection[path].send(method, ) { |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 |