class Frontapp::Client
Public Class Methods
new(options={})
click to toggle source
# File lib/frontapp/client.rb, line 43 def initialize(options={}) auth_token = options[:auth_token] user_agent = options[:user_agent] || "Frontapp Ruby Gem #{VERSION}" @headers = HTTP.headers({ Accept: "application/json", Authorization: "Bearer #{auth_token}", "User-Agent": user_agent }) end
Public Instance Methods
create(path, body)
click to toggle source
# File lib/frontapp/client.rb, line 101 def create(path, body) res = @headers.post("#{base_url}#{path}", json: body) response = JSON.parse(res.to_s) if !res.status.success? raise Error.from_response(res) end response end
create_without_response(path, body)
click to toggle source
# File lib/frontapp/client.rb, line 110 def create_without_response(path, body) res = @headers.post("#{base_url}#{path}", json: body) if !res.status.success? raise Error.from_response(res) end end
delete(path, body = {})
click to toggle source
# File lib/frontapp/client.rb, line 124 def delete(path, body = {}) res = @headers.delete("#{base_url}#{path}", json: body) if !res.status.success? raise Error.from_response(res) end end
get(path)
click to toggle source
# File lib/frontapp/client.rb, line 75 def get(path) res = @headers.get("#{base_url}#{path}") if !res.status.success? raise Error.from_response(res) end JSON.parse(res.to_s) end
get_plain(path)
click to toggle source
# File lib/frontapp/client.rb, line 83 def get_plain(path) headers_copy = @headers.dup res = @headers.accept("text/plain").get("#{base_url}#{path}") if !res.status.success? raise Error.from_response(res) end res.to_s end
get_raw(path)
click to toggle source
# File lib/frontapp/client.rb, line 92 def get_raw(path) headers_copy = @headers.dup res = @headers.get("#{base_url}#{path}") if !res.status.success? raise Error.from_response(res) end res end
list(path, params = {})
click to toggle source
# File lib/frontapp/client.rb, line 53 def list(path, params = {}) items = [] last_page = false query = format_query(params) url = "#{base_url}#{path}?#{query}" until last_page res = @headers.get(url) if !res.status.success? raise Error.from_response(res) end response = JSON.parse(res.to_s) items.concat(response["_results"]) if response["_results"] pagination = response["_pagination"] if pagination.nil? || pagination["next"].nil? last_page = true else url = pagination["next"] end end items end
update(path, body)
click to toggle source
# File lib/frontapp/client.rb, line 117 def update(path, body) res = @headers.patch("#{base_url}#{path}", json: body) if !res.status.success? raise Error.from_response(res) end end
Private Instance Methods
base_url()
click to toggle source
# File lib/frontapp/client.rb, line 151 def base_url "https://api2.frontapp.com/" end
format_query(params)
click to toggle source
# File lib/frontapp/client.rb, line 132 def format_query(params) res = [] q = params.delete(:q) if q && q.is_a?(Hash) res << q.map do |k, v| case v when Symbol, String "q[#{k}]=#{URI.encode(v)}" when Array then v.map { |c| "q[#{k}][]=#{URI.encode(c.to_s)}" }.join("&") else "q[#{k}]=#{URI.encode(v.to_s)}" end end end res << params.map {|k,v| "#{k}=#{URI.encode(v.to_s)}"} res.join("&") end