-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitch_helper.rb
44 lines (38 loc) · 1.28 KB
/
twitch_helper.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
module TwitchHelper
def self.get_streams_api(game)
media = "https://api.twitch.tv/kraken/search/streams?" +
(URI.encode_www_form "q" => game)
HTTParty.get media
end
def self.stream_hash(game, stream_data)
{
network: "twitch",
game: stream_data["game"],
viewer_count: stream_data["viewers"],
channel_name: stream_data["channel"]["status"],
channel_thumb: stream_data["preview"]["medium"],#consider template wxh
streamer_name: stream_data["channel"]["display_name"],#used to get stream
streamer_logo: stream_data["channel"]["logo"]
}
end
def self.streams(game)
streams_data = get_streams_api(game)
if streams_data.has_key? 'streams'
streams_data['streams'].map { |stream_data| stream_hash(game, stream_data) }
else
streams_data #if there's an error it'll return the error hash
end
end
def self.get_stream(user)
data = HTTParty.get "https://api.twitch.tv/kraken/streams/#{user}"
if data.has_key? 'error'
data # gives the error hash back to the view to be displayed, can't do it here
else
{
channel_embed_url: "#{data["stream"]["channel"]["url"]}/embed",
embed_height: 378,
embed_width: 620
}
end
end
end