Class: Supportbee::Client

Inherits:
Object
  • Object
show all
Includes:
Agents, Groups, Tickets
Defined in:
lib/supportbee/client.rb,
lib/supportbee/client/agents.rb,
lib/supportbee/client/groups.rb,
lib/supportbee/client/labels.rb,
lib/supportbee/client/tickets.rb,
lib/supportbee/client/snippets.rb,
lib/supportbee/client/web_hooks.rb

Defined Under Namespace

Modules: Agents, Groups, Labels, Snippets, Tickets, WebHooks

Instance Method Summary collapse

Methods included from Groups

#groups

Methods included from Agents

#agent, #agents

Methods included from Tickets

#ticket, #tickets

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/supportbee/client.rb', line 13

def initialize(options={})
  @company = options[:company]
  @host = "https://#{@company}.supportbee.com"
  @auth_token = options[:auth_token]
  @conn = Faraday.new(:url => @host) do |faraday|
    faraday.params[:auth_token] = @auth_token
    faraday.request  :url_encoded             # form-encode POST params
    faraday.response :logger                  # log requests to STDOUT
    faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
  end
end

Instance Method Details

#index(model, extra_parameters = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/supportbee/client.rb', line 31

def index(model, extra_parameters = {})
  items = []
  current_page = 1
  last_page = false
  until last_page
    extra_parameters['page'] = current_page
    raw_response = @conn.get do |req|
      req.url "/#{model}.json"
      req.params.merge!(extra_parameters)
    end
    response = JSON.parse(raw_response.body)
    items.concat(response[model])
    if response['current_page'].to_i >= response['total_pages'].to_i
      last_page = true
    else
      current_page = current_page + 1
    end
  end
  items
end

#request(url, options = {}) ⇒ Object



25
26
27
28
29
# File 'lib/supportbee/client.rb', line 25

def request(url, options = {})
  method = options[:method].nil? ? :get : options[:method]
  response = @conn.run_request(method, url, options[:body], options[:headers])
  JSON.parse(response.body) 
end