Class: ZendeskAPI::Client

Inherits:
Object
  • Object
show all
Includes:
Rescue
Defined in:
lib/zendesk_api/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|config| ... } ⇒ Client

Creates a new ZendeskAPI::Client instance and yields #config.

Requires a block to be given.

Does basic configuration constraints:

Yields:

Raises:

  • (ArgumentError)


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
# File 'lib/zendesk_api/client.rb', line 68

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.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.

Returns:

  • (Collection)

    Collection instance for resource



29
30
31
32
33
34
# File 'lib/zendesk_api/client.rb', line 29

def method_missing(method, *args, &block)
  method = method.to_s
  options = args.last.is_a?(Hash) ? args.pop : {}
  return instance_variable_get("@#{method}") if !options.delete(:reload) && instance_variable_defined?("@#{method}")
  instance_variable_set("@#{method}", ZendeskAPI::Collection.new(self, ZendeskAPI.get_class(method.singular), options))
end

Instance Attribute Details

#callbacksArray (readonly)

Returns Custom response callbacks.

Returns:

  • (Array)

    Custom response callbacks



24
25
26
# File 'lib/zendesk_api/client.rb', line 24

def callbacks
  @callbacks
end

#configConfiguration (readonly)

Returns Config instance.

Returns:



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

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



112
113
114
# File 'lib/zendesk_api/client.rb', line 112

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_connectionObject (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



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/zendesk_api/client.rb', line 127

def build_connection
  Faraday.new(config.options) 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 Faraday::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

#connectionObject

Creates a connection if there is none, otherwise returns the existing connection.



100
101
102
103
# File 'lib/zendesk_api/client.rb', line 100

def connection
  @connection ||= build_connection
  return @connection
end

#current_account(reload = false) ⇒ Object



49
50
51
52
# File 'lib/zendesk_api/client.rb', line 49

def (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



57
58
59
60
# File 'lib/zendesk_api/client.rb', line 57

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)

Returns:



44
45
46
47
# File 'lib/zendesk_api/client.rb', line 44

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.

Parameters:

  • block (Proc)

    The block to execute. Takes one parameter, env.



107
108
109
# File 'lib/zendesk_api/client.rb', line 107

def insert_callback(&block)
  @callbacks << block
end

#play(id) ⇒ Object

Plays a view playlist.

Parameters:

  • id (String/Number)

    View id or ‘incoming’



38
39
40
# File 'lib/zendesk_api/client.rb', line 38

def play(id)
  ZendeskAPI::Playlist.new(self, id)
end