Class: Frontapp::Client

Inherits:
Object
  • Object
show all
Includes:
Attachments, Channels, Comments, ContactGroups, Contacts, Conversations, Events, Exports, Inboxes, Links, Messages, Rules, Tags, Teammates, Teams, Topics
Defined in:
lib/frontapp/client.rb,
lib/frontapp/client/tags.rb,
lib/frontapp/client/links.rb,
lib/frontapp/client/rules.rb,
lib/frontapp/client/teams.rb,
lib/frontapp/client/events.rb,
lib/frontapp/client/topics.rb,
lib/frontapp/client/exports.rb,
lib/frontapp/client/inboxes.rb,
lib/frontapp/client/channels.rb,
lib/frontapp/client/comments.rb,
lib/frontapp/client/contacts.rb,
lib/frontapp/client/messages.rb,
lib/frontapp/client/teammates.rb,
lib/frontapp/client/attachments.rb,
lib/frontapp/client/conversations.rb,
lib/frontapp/client/contact_groups.rb

Defined Under Namespace

Modules: Attachments, Channels, Comments, ContactGroups, Contacts, Conversations, Events, Exports, Inboxes, Links, Messages, Rules, Tags, Teammates, Teams, Topics

Instance Method Summary collapse

Methods included from Exports

#create_export!, #create_export_for_team!, #exports, #get_export

Methods included from Links

#create_link!, #get_link, #get_link_conversations, #links, #update_link!

Methods included from Topics

#get_topic_conversations

Methods included from Teams

#get_team, #teams

Methods included from Teammates

#get_teammate, #get_teammate_conversations, #get_teammate_inboxes, #teammates, #update_teammate!

Methods included from Tags

#create_tag!, #delete_tag!, #get_tag, #get_tag_conversations, #tags

Methods included from Rules

#get_rule, #rules

Methods included from Messages

#get_message, #get_message_source, #import_message, #receive_custom_message, #send_message, #send_reply

Methods included from Inboxes

#create_inbox!, #get_inbox, #get_inbox_channels, #get_inbox_conversations, #get_inbox_teammates, #inboxes

Methods included from Events

#events, #get_event

Methods included from Conversations

#add_conversation_followers!, #add_conversation_links!, #conversations, #get_conversation, #get_conversation_events, #get_conversation_followers, #get_conversation_inboxes, #get_conversation_messages, #remove_conversation_followers!, #remove_conversation_links!, #update_conversation!

Methods included from Contacts

#add_contact_handle!, #add_contact_note!, #contacts, #create_contact!, #delete_contact!, #delete_contact_handle!, #get_contact, #get_contact_conversations, #get_contact_notes, #update_contact!

Methods included from ContactGroups

#add_contacts_to_contact_group!, #contact_groups, #create_contact_group!, #delete_contact_group!, #get_contact_group_contacts

Methods included from Comments

#create_comment!, #get_comment, #get_comment_mentions, #get_conversation_comments

Methods included from Channels

#channels, #create_channel!, #get_channel, #get_channel_inbox, #update_channel!

Methods included from Attachments

#download_attachment

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



43
44
45
46
47
48
49
50
51
# 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

Instance Method Details

#create(path, body) ⇒ Object



101
102
103
104
105
106
107
108
# 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) ⇒ Object



110
111
112
113
114
115
# 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 = {}) ⇒ Object



124
125
126
127
128
129
# 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) ⇒ Object



75
76
77
78
79
80
81
# 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) ⇒ Object



83
84
85
86
87
88
89
90
# 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) ⇒ Object



92
93
94
95
96
97
98
99
# 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 = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 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) ⇒ Object



117
118
119
120
121
122
# 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