Module: LimdeskApi

Defined in:
lib/limdesk_api.rb,
lib/limdesk_api/sale.rb,
lib/limdesk_api/client.rb,
lib/limdesk_api/ticket.rb,
lib/limdesk_api/version.rb,
lib/limdesk_api/activity.rb,
lib/limdesk_api/limdesk_object.rb

Overview

LideskAPI Warapper Limdesk.com is a multichannel, web-based customer support solution. This gem lets you integrate your software using LimdeskAPI.

Defined Under Namespace

Classes: Activity, Client, LimdeskObject, Sale, Ticket

Constant Summary collapse

ENDPOINT =

default endpoint

'https://cloud.limdesk.com'
KNOWN_OBJS =
{
  ticket: :tickets,
  activity: :activities,
  client: :clients,
  sale: :sales
}
VERSION =
'0.0.4'

Class Method Summary collapse

Class Method Details

.check_create_response(body, obj) ⇒ Object



133
134
135
136
137
138
# File 'lib/limdesk_api.rb', line 133

def self.check_create_response(body, obj)
  fail 'LimdeskApiError' if !body.is_a?(Hash) ||
                            body['status'].nil? ||
                            body['status'] == 'error' ||
                            body[obj.to_s].nil?
end

.check_get_one_response(body) ⇒ Object



58
59
60
61
# File 'lib/limdesk_api.rb', line 58

def self.check_get_one_response(body)
  fail 'LimdeskApiError' if !body.is_a?(Hash) ||
                            body['status'] != 'ok'
end

.check_get_page_response(body, obj) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/limdesk_api.rb', line 86

def self.check_get_page_response(body, obj)
  fail 'LimdeskApiError' if !body.is_a?(Hash) ||
                            body['status'] != 'ok' ||
                            body['page'].nil? ||
                            body['total_pages'].nil? ||
                            body[obj.to_s].nil?
end

.check_update_resonse(body) ⇒ Object



159
160
161
162
# File 'lib/limdesk_api.rb', line 159

def self.check_update_resonse(body)
  fail 'LimdeskApiError' if !body.is_a?(Hash) ||
                            body['status'].nil?
end

.configure {|_self| ... } ⇒ Object

Examples:

configure API access

LimdeskApi.configure { |lim| lim.key = 'xxx'; lim.version = 1 }

Yields:

  • (_self)

Yield Parameters:

  • _self (LimdeskApi)

    the object that the method was called on



28
29
30
31
32
33
34
35
36
37
# File 'lib/limdesk_api.rb', line 28

def self.configure
  yield self
  @connection = Faraday.new(url: ENDPOINT) do |faraday|
    faraday.response :json, content_type: /\bjson$/
    faraday.adapter Faraday.default_adapter
    faraday.use Faraday::Response::Logger if @debug
  end
  @prefix = "/api/v#{@version}"
  self
end

.create(params) ⇒ Object

create LimdeskAPI object

Parameters:

  • params (Hash)

    new object data



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/limdesk_api.rb', line 143

def self.create(params)
  resp = @connection.post do |req|
    req.url generate_url params
    req.params[:key] = @key
    req.body = params[:params].to_json
  end
  case resp.status
  when 200
    body = resp.body
    check_create_response(body, params[:object])
    body[params[:object].to_s]
  else
    fail 'LimdeskApiErrorFatal'
  end
end

.debug=(debug) ⇒ Object



47
48
49
# File 'lib/limdesk_api.rb', line 47

def self.debug=(debug)
  @debug = debug
end

.delete(params) ⇒ Object



192
193
194
# File 'lib/limdesk_api.rb', line 192

def self.delete(params)
  update(:delete, params)
end

.generate_url(params) ⇒ Object



51
52
53
54
55
56
# File 'lib/limdesk_api.rb', line 51

def self.generate_url(params)
  url = [@prefix, LimdeskApi::KNOWN_OBJS[params[:object]]]
  url.push params[:id] if params[:id]
  url.push params[:action] if params[:action]
  url.join('/')
end

.get_all(object_name) ⇒ Object

get all LimdeskAPI objects of a type

Parameters:

  • object_name (Symbol)

    t one of LimdeskApi::KNOWN_OBJS



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/limdesk_api.rb', line 121

def self.get_all(object_name)
  query_options = { page: 0, object: object_name }
  data = []
  loop do
    query_options[:page] += 1
    results = LimdeskApi.get_page(query_options)
    data += results[:objects]
    break if results[:total_pages] == results[:page] || results[:page] == 0
  end
  data
end

.get_one(params) ⇒ Object

get a single LimdeskAPI object

Parameters:

  • params (Hash)

Options Hash (params):

  • :object (Symbol)

    one of LimdeskApi::KNOWN_OBJS

  • :id (Integer)

    requested object’s id



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/limdesk_api.rb', line 68

def self.get_one(params)
  resp = @connection.get do |req|
    req.url generate_url params
    req.params[:key] = @key
    req.params[:query] = params[:query] if params[:query]
  end
  case resp.status
  when 200
    body = resp.body
    check_get_one_response(body)
    body[params[:object].to_s]
  when 404
    nil
  else
    fail 'LimdeskApiErrorFatal'
  end
end

.get_page(params) ⇒ Object

get a page of LimdeskAPI object

Parameters:

  • params (Hash)

Options Hash (params):

  • :object (Symbol)

    one of LimdeskApi::KNOWN_OBJS

  • :page (Integer)

    requested page



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/limdesk_api.rb', line 99

def self.get_page(params)
  obj = LimdeskApi::KNOWN_OBJS[params[:object]]
  resp = @connection.get do |req|
    req.url generate_url params
    req.params[:key] = @key
    req.params[:page] = params[:page]
  end
  case resp.status
  when 200
    body = resp.body
    check_get_page_response(body, obj)
    {  page: body['page'],
       total_pages: body['total_pages'],
       objects: body[obj.to_s] }
  else
    fail 'LimdeskApiErrorFatal'
  end
end

.key=(key) ⇒ Object



39
40
41
# File 'lib/limdesk_api.rb', line 39

def self.key=(key)
  @key = key
end

.post_simple(params) ⇒ Object



188
189
190
# File 'lib/limdesk_api.rb', line 188

def self.post_simple(params)
  update(:post, params)
end

.put(params) ⇒ Object



184
185
186
# File 'lib/limdesk_api.rb', line 184

def self.put(params)
  update(:put, params)
end

.update(method, params) ⇒ Object

update/delete a LimdeskAPI object

Parameters:

  • method (Symobol)

    a http method, one of :put, :post, :delete

  • params (Hash)

    object data, if required



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/limdesk_api.rb', line 168

def self.update(method, params)
  resp = @connection.send(method) do |req|
    req.url generate_url params
    req.params[:key] = @key
    req.body = params[:params].to_json
  end
  case resp.status
  when 200
    body = resp.body
    check_update_resonse(body)
    body['status'] == 'ok' ? true : false
  else
    fail 'LimdeskApiErrorFatal'
  end
end

.version=(version) ⇒ Object



43
44
45
# File 'lib/limdesk_api.rb', line 43

def self.version=(version)
  @version = version
end