Class: Gist::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/gist/client.rb

Direct Known Subclasses

Conversation

Constant Summary collapse

API_URL =
'https://api.getgist.com'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token) ⇒ Client

Returns a new instance of Client.



9
10
11
# File 'lib/gist/client.rb', line 9

def initialize(access_token)
  @access_token = access_token
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



7
8
9
# File 'lib/gist/client.rb', line 7

def access_token
  @access_token
end

Instance Method Details

#assign_conversation(assignee_id: nil, teammate_id:, id:) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/gist/client.rb', line 57

def assign_conversation(assignee_id: nil, teammate_id: , id:)
	payload = {
		teammate_id: teammate_id,
		assignee_id: assignee_id
	}
	connect(payload: payload, method: :patch, endpoint: "conversations/#{id}/assign")
end

#close_conversation(teammate_id:, id:) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/gist/client.rb', line 82

def close_conversation(teammate_id:, id:)
	payload = {
		teammate_id: teammate_id,
		state: "closed"
	}
	connect(payload: payload, method: :patch, endpoint: "conversations/#{id}")
end

#connect(payload: {}, params: {}, method:, endpoint:) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gist/client.rb', line 13

def connect(payload: {}, params: {}, method:, endpoint:)
	RestClient::Request.execute(
 method: method,
 url: "#{API_URL}/#{endpoint}",
 payload: payload.to_json,
 headers: {
 	content_type: 'application/json',
 	Authorization: "Bearer #{@access_token}",
 	params: params
 }) do |response, request, result|
 case response.code
 when 400
   [ :error, JSON.parse(response.to_str) ]
 when 200
   [ :success, JSON.parse(response.to_str) ]
 else
   fail "Invalid response #{response.to_str} received."
 end
			end

		rescue RestClient::Unauthorized, RestClient::Forbidden => err
JSON.parse(err.response.to_str)
end

#create_conversation(user_id: nil, email: nil, id:, body:, from:) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/gist/client.rb', line 37

def create_conversation(user_id: nil, email: nil, id:, body:, from:)
	payload = {
   from: {
     id: id,
     user_id: user_id,
     email: email
   },
   body: body
	}
	connect(payload: payload, method: :post, endpoint: "conversations")
end

#delete_conversation(id:) ⇒ Object



53
54
55
# File 'lib/gist/client.rb', line 53

def delete_conversation(id:)
	connect(method: :delete, endpoint: "conversations/#{id}")
end

#get_conversation(id:) ⇒ Object



49
50
51
# File 'lib/gist/client.rb', line 49

def get_conversation(id:)
	connect(method: :get, endpoint: "conversations/#{id}")
end

#list_conversations(order: "desc", status: "all", sort: "updated_at") ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/gist/client.rb', line 98

def list_conversations(order: "desc", status: "all", sort: "updated_at")
	params = {
		order: order,
		status: status,
		sort: sort
	}
	connect(params: params, method: :get, endpoint: "conversations")
end

#prioritize_conversation(priority: "priority", teammate_id:, id:) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/gist/client.rb', line 90

def prioritize_conversation(priority: "priority", teammate_id:, id:)
	payload = {
		teammate_id: teammate_id,
		priority: priority
	}
	connect(payload: payload, method: :patch, endpoint: "conversations/#{id}/priority")
end

#reopen_conversation(teammate_id:, id:) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/gist/client.rb', line 74

def reopen_conversation(teammate_id:, id:)
	payload = {
		teammate_id: teammate_id,
		state: "open"
	}
	connect(payload: payload, method: :patch, endpoint: "conversations/#{id}")
end

#reply_to_contact(user_id: nil, email: nil, message_type: "reply", id: nil, body:, conversation_id:) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/gist/client.rb', line 111

def reply_to_contact(user_id: nil, email: nil, message_type: "reply", id: nil, body:, conversation_id:)
	payload = {
		message_type: message_type,
   from: {
     id: id,
     user_id: user_id,
     email: email,
     type: contact
   },
   body: body
	}
	connect(payload: payload, method: :get, endpoint: "conversations/#{conversation_id}/messages")
end

#reply_to_teammate(message_type: "reply", teammate_id:, body:, conversation_id:) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/gist/client.rb', line 125

def reply_to_teammate(message_type: "reply", teammate_id: , body:, conversation_id:)
	payload = {
		message_type: message_type,
   from: {
     type: "teammate",
     teammate_id: teammate_id
   },
   body: body
	}
	connect(payload: payload, method: :get, endpoint: "conversations/#{conversation_id}/messages")
end

#retrieve_conversation_countObject



107
108
109
# File 'lib/gist/client.rb', line 107

def retrieve_conversation_count
	connect(method: :get, endpoint: "conversations/count")
end

#snooze_conversation(snooze_until: nil, teammate_id:, id:) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/gist/client.rb', line 65

def snooze_conversation(snooze_until: nil, teammate_id:, id:)
	payload = {
		teammate_id: teammate_id,
		state: "snoozed",
		snooze_until: snooze_until
	}
	connect(payload: payload, method: :patch, endpoint: "conversations/#{id}")
end