class DeskApi::Client
The {DeskApi::Client} builds and performs the http request using Faraday and the configured adapter. It includes and has full access to the configuration module.
@author Thomas Stachl <tstachl@salesforce.com> @copyright Copyright © 2013-2016 Salesforce.com @license BSD 3-Clause License
Public Class Methods
Initializes a new client object
@param options [Hash] optional configuration hash @return [DeskApi::Client] the new client
# File lib/desk_api/client.rb, line 48 def initialize(options = {}) DeskApi::Configuration.keys.each do |key| value = options[key] || DeskApi.instance_variable_get(:"@#{key}") instance_variable_set(:"@#{key}", value) end end
Public Instance Methods
Returns a new resource for the given path
@param path [String] the url path to the resource @return [DeskApi::Resource]
# File lib/desk_api/client.rb, line 73 def by_url(path) DeskApi::Resource.new(self, DeskApi::Resource.build_self_link(path)) end
Private Instance Methods
Builds and/or returns the Faraday client. @returns [Faraday::Connection]
# File lib/desk_api/client.rb, line 114 def connection @connection ||= Faraday.new endpoint, connection_options, &middleware end
Returns a new resource based on the method you're trying to load:
@example request cases
my_cases = client.cases # GET '/api/v2/cases'
@param method [Symbol] the method called @param params [Hash] additional query params @yield [DeskApi::Resource] @return [DeskApi::Resource]
# File lib/desk_api/client.rb, line 87 def method_missing(method, params = {}) definition = DeskApi::Resource.build_self_link("/api/v2/#{method}") DeskApi::Resource.new(self, definition).tap do |res| res.query_params = params yield res if block_given? end end
Hands off the request to Faraday for further processing
@param method [Symbol] the http method to call @param path [String] the url path to the resource @param params [Hash] optional additional url params @yield [Faraday::Response] for further request customizations. @return [Faraday::Response] @raises [DeskApi::Error::ClientError] @raises [DeskApi::Error::ParserError]
# File lib/desk_api/client.rb, line 104 def request(method, path, params = {}, &block) connection.send(method, path, params, &block) rescue Faraday::Error::ClientError => err raise DeskApi::Error::ClientError.new(err) rescue JSON::ParserError => err raise DeskApi::Error::ParserError.new(err) end