Class: DeskApi::Client

Inherits:
Object
  • Object
show all
Includes:
Configuration
Defined in:
lib/desk_api/client.rb

Overview

The Client builds and performs the http request using Faraday and the configured adapter. It includes and has full access to the configuration module.

Author:

Instance Attribute Summary

Attributes included from Configuration

#connection_options, #consumer_key, #consumer_secret, #endpoint, #middleware, #password, #subdomain, #token, #token_secret, #username

Instance Method Summary collapse

Methods included from Configuration

#configure, #credentials?, included, keys, register_middleware, #reset!

Constructor Details

#initialize(options = {}) ⇒ DeskApi::Client

Initializes a new client object

Parameters:

  • options (Hash) (defaults to: {})

    optional configuration hash



48
49
50
51
52
53
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, params = {}) {|DeskApi::Resource| ... } ⇒ DeskApi::Resource (private)

Returns a new resource based on the method you're trying to load:

Examples:

request cases

my_cases = client.cases # GET '/api/v2/cases'

Parameters:

  • method (Symbol)

    the method called

  • params (Hash) (defaults to: {})

    additional query params

Yields:

Returns:



87
88
89
90
91
92
93
# 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

Instance Method Details

#by_url(path) ⇒ DeskApi::Resource

Returns a new resource for the given path

Parameters:

  • path (String)

    the url path to the resource

Returns:



73
74
75
# File 'lib/desk_api/client.rb', line 73

def by_url(path)
  DeskApi::Resource.new(self, DeskApi::Resource.build_self_link(path))
end

#method {|Faraday::Response| ... } ⇒ Faraday::Response

Perform an http request

Parameters:

  • path (String)

    the url path to the resource

  • params (Hash)

    optional additional url params

Yields:

  • (Faraday::Response)

    for further request customizations.

Returns:

  • (Faraday::Response)


61
62
63
64
65
66
67
# File 'lib/desk_api/client.rb', line 61

%w(get head delete post patch).each do |method|
  class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{method}(path, params = {}, &block)
      request(:#{method}, path, params, &block)
    end
  RUBY
end