Class: ZendeskAPI::Client
- Inherits:
-
Object
- Object
- ZendeskAPI::Client
- Includes:
- Rescue
- Defined in:
- lib/zendesk_api/client.rb
Instance Attribute Summary collapse
-
#callbacks ⇒ Array
readonly
Custom response callbacks.
-
#config ⇒ Configuration
readonly
Config instance.
Class Method Summary collapse
-
.check_deprecated_namespace_usage(attributes, name) ⇒ Object
show a nice warning for people using the old style api.
Instance Method Summary collapse
-
#build_connection ⇒ Object
protected
Called by #connection to build a connection.
-
#connection ⇒ Object
Creates a connection if there is none, otherwise returns the existing connection.
- #current_account(reload = false) ⇒ Object
-
#current_locale(reload = false) ⇒ Object
Returns the current locale.
-
#current_user(reload = false) ⇒ ZendeskAPI::User
Returns the current user (aka me).
- #initialize {|config| ... } ⇒ Client constructor
-
#insert_callback(&block) ⇒ Object
Pushes a callback onto the stack.
-
#method_missing(method, *args, &block) ⇒ Collection
Handles resources such as ‘tickets’.
Constructor Details
#initialize {|config| ... } ⇒ Client
Creates a new ZendeskAPI::Client instance and yields #config.
Requires a block to be given.
Does basic configuration constraints:
-
ZendeskAPI::Configuration#url must be https unless ZendeskAPI::Configuration#allow_http is set.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/zendesk_api/client.rb', line 64 def initialize raise ArgumentError, "block not given" unless block_given? @config = ZendeskAPI::Configuration.new yield config if !config.allow_http && config.url !~ /^https/ raise ArgumentError, "zendesk_api is ssl only; url must begin with https://" end config.retry = !!config.retry # nil -> false if config.token && !config.password config.password = config.token config.username += "/token" unless config.username.end_with?("/token") end if config.logger.nil? || config.logger == true require 'logger' config.logger = Logger.new($stderr) config.logger.level = Logger::WARN end @callbacks = [] if logger = config.logger insert_callback do |env| if warning = env[:response_headers]["X-Zendesk-API-Warn"] logger.warn "WARNING: #{warning}" end end end 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’. Any options are passed to the underlying collection, except reload which disregards memoization and creates a new Collection instance.
31 32 33 34 35 36 |
# File 'lib/zendesk_api/client.rb', line 31 def method_missing(method, *args, &block) method = method.to_s = args.last.is_a?(Hash) ? args.pop : {} return instance_variable_get("@#{method}") if !.delete(:reload) && instance_variable_defined?("@#{method}") instance_variable_set("@#{method}", ZendeskAPI::Collection.new(self, ZendeskAPI.get_class(Inflection.singular(method)), )) end |
Instance Attribute Details
#callbacks ⇒ Array (readonly)
Returns Custom response callbacks.
26 27 28 |
# File 'lib/zendesk_api/client.rb', line 26 def callbacks @callbacks end |
#config ⇒ Configuration (readonly)
Returns Config instance.
24 25 26 |
# File 'lib/zendesk_api/client.rb', line 24 def config @config end |
Class Method Details
.check_deprecated_namespace_usage(attributes, name) ⇒ Object
show a nice warning for people using the old style api
113 114 115 |
# File 'lib/zendesk_api/client.rb', line 113 def self.check_deprecated_namespace_usage(attributes, name) raise "un-nest '#{name}' from the attributes" if attributes[name].is_a?(Hash) end |
Instance Method Details
#build_connection ⇒ Object (protected)
Called by #connection to build a connection. Can be overwritten in a subclass to add additional middleware and make other configuration changes.
Uses middleware according to configuration options.
Request logger if logger is not nil
Retry middleware if retry is true
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/zendesk_api/client.rb', line 128 def build_connection Faraday.new(config.) do |builder| # response builder.use Faraday::Request::BasicAuthentication, config.username, config.password builder.use Faraday::Response::RaiseError builder.use ZendeskAPI::Middleware::Response::Callback, self builder.use ZendeskAPI::Middleware::Response::Logger, config.logger if config.logger builder.use ZendeskAPI::Middleware::Response::ParseIsoDates builder.response :json, :content_type => 'application/json' builder.use ZendeskAPI::Middleware::Response::Gzip builder.use ZendeskAPI::Middleware::Response::Deflate # request builder.use ZendeskAPI::Middleware::Request::EtagCache, :cache => config.cache builder.use ZendeskAPI::Middleware::Request::Upload builder.request :multipart builder.request :json builder.use ZendeskAPI::Middleware::Request::Retry, :logger => config.logger if config.retry # Should always be first in the stack builder.adapter *config.adapter || Faraday.default_adapter end end |
#connection ⇒ Object
Creates a connection if there is none, otherwise returns the existing connection.
101 102 103 104 |
# File 'lib/zendesk_api/client.rb', line 101 def connection @connection ||= build_connection return @connection end |
#current_account(reload = false) ⇒ Object
45 46 47 48 |
# File 'lib/zendesk_api/client.rb', line 45 def current_account(reload = false) return @current_account if @current_account && !reload @current_account = Hashie::Mash.new(connection.get('account/resolve').body) end |
#current_locale(reload = false) ⇒ Object
Returns the current locale
53 54 55 56 |
# File 'lib/zendesk_api/client.rb', line 53 def current_locale(reload = false) return @locale if @locale && !reload @locale = locales.find(:id => 'current') end |
#current_user(reload = false) ⇒ ZendeskAPI::User
Returns the current user (aka me)
40 41 42 43 |
# File 'lib/zendesk_api/client.rb', line 40 def current_user(reload = false) return @current_user if @current_user && !reload @current_user = users.find(:id => 'me') end |
#insert_callback(&block) ⇒ Object
Pushes a callback onto the stack. Callbacks are executed on responses, last in the Faraday middleware stack.
108 109 110 |
# File 'lib/zendesk_api/client.rb', line 108 def insert_callback(&block) @callbacks << block end |