-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.rb
51 lines (41 loc) · 1.38 KB
/
helpers.rb
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
# Application Helper Methods
require 'json'
module Helpers
# HipChat API Helpers
class HipChat
include HTTParty
base_uri 'https://api.hipchat.com/v2'
def get_emoticons
# Ensure API key is set and available before proceeding
unless ENV['HIPCHAT_API']
raise 'HipChat API key not accessible under "HIPCHAT_API" key. See README.'
end
# Build GET request to 'emoticon' endpoint of HipChat API v2.0
self.class.get '/emoticon', query: { 'auth_token' => ENV['HIPCHAT_API'], 'type' => 'group' }
end
end
# Refresh cache with new GET request to HipChat
def update(cache)
cache.set('emoticons', new_query)
end
# GET emoticons from HipChat API and return the response body
def new_query
hipchat = HipChat.new.get_emoticons
# Proceed only if GET request is successful
if hipchat.response.code != 200.to_s
raise "Unable to successfully query the HipChat API. Error code: #{hipchat.response.code}"
end
# Return body of GET request from HipChat
emoticons = hipchat.response.body
end
# Check if 'emoticons' key exists in Redis cache
def emoticons_in(cache)
cache.exists('emoticons')
end
# Call 'emoticons' JSON string from cache, parse and return hash of emoticons
def render_emoticons(cache)
string = cache.get('emoticons')
hash = JSON.parse(string)
emoticons = hash['items']
end
end