-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuffer_client.rb
More file actions
251 lines (191 loc) · 4.9 KB
/
buffer_client.rb
File metadata and controls
251 lines (191 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
require 'cgi'
require_relative "api/auth_api.rb"
require_relative "api/user_api.rb"
require_relative "api/profile_api.rb"
require_relative "api/update_api.rb"
require_relative "api/link_api.rb"
require_relative "api/info_api.rb"
class BufferClient
def initialize(options = {})
@error = nil
# Initialize the API objects we may use
@auth_api = AuthApi.new(options)
@user_api = UserApi.new(options)
@profile_api = ProfileApi.new(options)
@update_api = UpdateApi.new(options)
@link_api = LinkApi.new(options)
@info_api = InfoApi.new(options)
@api_objects = [
@auth_api,
@user_api,
@profile_api,
@update_api,
@link_api,
@info_api
]
end
##################
# HELPER METHODS #
##################
# Reconfigure API objects
def configure(options = {})
@api_objects.each do |api|
api.configure(options)
end
end
def has_error?
@error.present?
end
def get_error
tmp_error = @error
@error = nil
tmp_error
end
def error
@error
end
############
# AUTH API #
############
def get_auth_token
token = @auth_api.get_auth_token
record_err(@auth_api)
token
end
############
# USER API #
############
def get_user_id
user_id = @user_api.get_user_id
record_err(@user_api)
user_id
end
def get_user_json
user_json = @user_api.get_user_json
record_err(@user_api)
user_json
end
def deauthorize
success_json = @user_api.deauthorize
record_err(@user_api)
is_success?(success_json)
end
###############
# PROFILE API #
###############
def get_user_profiles
profiles = @profile_api.get_profiles
record_err(@profile_api)
profiles
end
def get_user_profile(id)
profile = @profile_api.get_profile(id)
record_err(@profile_api)
profile
end
def get_schedule(id)
schedule = @profile_api.get_schedule(id)
record_err(@profile_api)
schedule
end
def update_schedule(id, sched_array)
sched_array.deep_symbolize_keys!
success_json = @profile_api.update_schedule(id, sched_array)
record_err(@profile_api)
is_success?(success_json)
end
##############
# UPDATE API #
##############
def get_update(id)
update_json = @update_api.get_update(id)
record_err(@update_api)
update_json
end
def get_pending_updates(id, options = {})
pending_updates_json = @update_api.get_pending_updates(id, options)
record_err(@update_api)
pending_updates_json
end
def get_sent_updates(id, options = {})
sent_updates_json = @update_api.get_sent_updates(id, options)
record_err(@update_api)
sent_updates_json
end
def get_interactions(id, event, options = {})
# Forces the user to enter a value for event without
# needing to validate the hash
options[:event] = event
interactions_json = @update_api.get_interactions(id, options)
record_err(@update_api)
interactions_json
end
def reorder_updates(id, updates_array, options = {})
new_order_json = @update_api.reorder_updates(id, updates_array, options)
record_err(@update_api)
extract_key(new_order_json, "updates")
end
def shuffle_updates(id, options = {})
shuffle_json = @update_api.shuffle_updates(id, options)
record_err(@update_api)
extract_key(shuffle_json, "updates")
end
def create_update(profile_ids, options = {})
post_json = @update_api.create_update(profile_ids, options)
record_err(@update_api)
post_json
end
def update_status(id, text, options = {})
# Forces the user to enter a value for text without
# needing to validate the hash
options[:text] = text
update_json = @update_api.update_status(id, options)
record_err(@update_api)
update_json
end
def share_update(id)
success_json = @update_api.share_update(id)
record_err(@update_api)
is_success?(success_json)
end
def destroy_update(id)
success_json = @update_api.destroy_update(id)
record_err(@update_api)
is_success?(success_json)
end
def move_to_top(id)
success_json = @update_api.move_to_top(id)
record_err(@update_api)
is_success?(success_json)
end
############
# LINK API #
############
def get_shares(url)
# Encode URL
encoded_url = CGI.escape(url)
share_json = @link_api.get_shares(encoded_url)
record_err(@link_api)
if share_json.present? && share_json["shares"].present?
share_json["shares"]
end
end
############
# INFO API #
############
def get_configuration
info_json = @info_api.get_configuration
record_err(@info_api)
info_json
end
private
def is_success?(success_json)
success_json.present? && success_json.include?("success") && success_json["success"] == true
end
def record_err(api)
@error = api.get_error if api.has_error?
end
def extract_key(json, key)
return json[key] if json.present?
end
end