Class: Lita::Handlers::OnewheelGiphy

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/handlers/onewheel_giphy.rb

Instance Method Summary collapse

Instance Method Details

#call_giphy(uri) ⇒ Object



120
121
122
123
# File 'lib/lita/handlers/onewheel_giphy.rb', line 120

def call_giphy(uri)
  Lita.logger.debug("Calling giphy with #{uri + 'api_key=' + config.api_key}")
  RestClient.get uri + 'api_key=' + config.api_key
end

#get_image(data) ⇒ Object



106
107
108
109
110
111
# File 'lib/lita/handlers/onewheel_giphy.rb', line 106

def get_image(data)
  image_data = JSON.parse(data)
  image = image_data['data']['image_original_url']
  Lita.logger.debug "get_image returning #{image}"
  image
end

#get_random(data) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/lita/handlers/onewheel_giphy.rb', line 90

def get_random(data)
  image_data = JSON.parse(data)
  if image_data['data'].count == 0
    Lita.logger.debug 'No images found.'
    return
  end
  image = image_data['data'][get_random_number(image_data['data'].count)]['images']['original']['url']
  Lita.logger.debug "get_random returning #{image}"
  image
end

#get_random_number(max) ⇒ Object



101
102
103
104
# File 'lib/lita/handlers/onewheel_giphy.rb', line 101

def get_random_number(max)
  and_i = Random.new
  and_i.rand(max)
end

#get_random_uriObject



69
70
71
72
73
74
# File 'lib/lita/handlers/onewheel_giphy.rb', line 69

def get_random_uri()
  # tag - the GIF tag to limit randomness by
  # rating - limit results to those rated (y,g, pg, pg-13 or r).
  # fmt - (optional) return results in html or json format (useful for viewing responses as GIFs to debug/test)
  config.api_uri + 'random?' #?q=' + URI.encode(keywords)
end

#get_search_uri(keywords) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/lita/handlers/onewheel_giphy.rb', line 60

def get_search_uri(keywords)
  # q - search query term or phrase
  # limit - (optional) number of results to return, maximum 100. Default 25.
  # offset - (optional) results offset, defaults to 0.
  # rating - limit results to those rated (y,g, pg, pg-13 or r).
  # fmt - (optional) return results in html or json format (useful for viewing responses as GIFs to debug/test)
  config.api_uri + 'search?q=' + URI.encode(keywords) + '&'
end

#get_translate_image(data) ⇒ Object



113
114
115
116
117
118
# File 'lib/lita/handlers/onewheel_giphy.rb', line 113

def get_translate_image(data)
  image_data = JSON.parse(data)
  image = image_data['data']['images']['original']['url']
  Lita.logger.debug "get_translate_image returning #{image}"
  image
end

#get_translate_uri(keywords) ⇒ Object



83
84
85
86
87
88
# File 'lib/lita/handlers/onewheel_giphy.rb', line 83

def get_translate_uri(keywords)
  # s - term or phrase to translate into a GIF
  # rating - limit results to those rated (y,g, pg, pg-13 or r).
  # fmt - (optional) return results in html or json format (useful for viewing responses as GIFs to debug/test)
  config.api_uri + 'translate?s=' + URI.encode(keywords) + '&'
end


76
77
78
79
80
81
# File 'lib/lita/handlers/onewheel_giphy.rb', line 76

def get_trending_uri()
  # limit (optional) limits the number of results returned. By default returns 25 results.
  # rating - limit results to those rated (y,g, pg, pg-13 or r).
  # fmt - (optional) return results in html or json format (useful for viewing responses as GIFs to debug/test)
  config.api_uri + 'trending?'
end

#random(response) ⇒ Object



38
39
40
41
42
43
# File 'lib/lita/handlers/onewheel_giphy.rb', line 38

def random(response)
  uri = get_random_uri
  giphy_data = call_giphy(uri)
  image = get_image(giphy_data.body)
  response.reply image
end

#search(response) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/lita/handlers/onewheel_giphy.rb', line 29

def search(response)
  keywords = response.matches[0][0]
  Lita.logger.debug "Searching giphy for #{keywords}"
  uri = get_search_uri(keywords)
  giphy_data = call_giphy(uri)
  image = get_random(giphy_data.body)
  response.reply image
end

#translate(response) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/lita/handlers/onewheel_giphy.rb', line 52

def translate(response)
  keywords = response.matches[0][0]
  uri = get_translate_uri(keywords)
  giphy_data = call_giphy(uri)
  image = get_translate_image(giphy_data.body)
  response.reply image
end


45
46
47
48
49
50
# File 'lib/lita/handlers/onewheel_giphy.rb', line 45

def trending(response)
  uri = get_trending_uri
  giphy_data = call_giphy(uri)
  image = get_random(giphy_data.body)
  response.reply image
end