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) ⇒ 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.
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’.
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) = args.last.is_a?(Hash) ? args.pop : {} FreshdeskAPI::Collection.new(self, method_class, ) end |
Instance Attribute Details
#config ⇒ Configuration (readonly)
Returns Config instance.
24 25 26 |
# File 'lib/freshdesk_api/client.rb', line 24 def config @config end |
Instance Method Details
#connection ⇒ RestClient::Resouce
Creates a connection if there is none, otherwise returns the existing connection.
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, = {}) response = nil connection[path].send(method, ) 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 |