scamp 0.0.4 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,112 +0,0 @@
1
- class Scamp
2
- module Channels
3
- # TextMessage (regular chat message),
4
- # PasteMessage (pre-formatted message, rendered in a fixed-width font),
5
- # SoundMessage (plays a sound as determined by the message, which can be either “rimshot”, “crickets”, or “trombone”),
6
- # TweetMessage (a Twitter status URL to be fetched and inserted into the chat)
7
-
8
- # curl -vvv -H 'Content-Type: application/json' -d '{"message":{"body":"Yeeeeeaaaaaahh", "type":"Textmessage"}}' -u API_KEY:X https://37s.campfirenow.com/room/293788/speak.json
9
- def say(message, channel)
10
- url = "https://#{subdomain}.campfirenow.com/room/#{channel_id(channel)}/speak.json"
11
- http = EventMachine::HttpRequest.new(url).post :head => {'Content-Type' => 'application/json', 'authorization' => [api_key, 'X']}, :body => Yajl::Encoder.encode({:message => {:body => message, :type => "Textmessage"}})
12
-
13
- http.errback { logger.error "Error speaking: '#{message}' to #{channel_id(channel)}" }
14
- end
15
-
16
- def paste(text, channel)
17
- end
18
-
19
- def upload
20
- end
21
-
22
- def join(channel_id)
23
- logger.info "Joining channel #{channel_id}"
24
- url = "https://#{subdomain}.campfirenow.com/room/#{channel_id}/join.json"
25
- http = EventMachine::HttpRequest.new(url).post :head => {'Content-Type' => 'application/json', 'authorization' => [api_key, 'X']}
26
-
27
- http.errback { logger.error "Error joining channel: #{channel_id}" }
28
- http.callback {
29
- yield if block_given?
30
- }
31
- end
32
-
33
- def channel_id(channel_id_or_name)
34
- if channel_id_or_name.is_a? Integer
35
- return channel_id_or_name
36
- else
37
- return channel_id_from_channel_name(channel_id_or_name)
38
- end
39
- end
40
-
41
- def channel_name_for(channel_id)
42
- data = channel_cache_data(channel_id)
43
- return data["name"] if data
44
- channel_id.to_s
45
- end
46
-
47
- private
48
-
49
- def channel_cache_data(channel_id)
50
- return channel_cache[channel_id] if channel_cache.has_key? channel_id
51
- fetch_channel_data(channel_id)
52
- return false
53
- end
54
-
55
- def populate_channel_list
56
- url = "https://#{subdomain}.campfirenow.com/rooms.json"
57
- http = EventMachine::HttpRequest.new(url).get :head => {'authorization' => [api_key, 'X']}
58
- http.errback { logger.error "Error populating the channel list: #{http.status.inspect}" }
59
- http.callback {
60
- new_channels = {}
61
- Yajl::Parser.parse(http.response)['rooms'].each do |c|
62
- new_channels[c["name"]] = c
63
- end
64
- # No idea why using the "channels" accessor here doesn't
65
- # work but accessing the ivar directly does. There's
66
- # Probably a bug.
67
- @channels = new_channels # replace existing channel list
68
- yield if block_given?
69
- }
70
- end
71
-
72
- def fetch_channel_data(channel_id)
73
- logger.debug "Fetching channel data for #{channel_id}"
74
- url = "https://#{subdomain}.campfirenow.com/room/#{channel_id}.json"
75
- http = EventMachine::HttpRequest.new(url).get :head => {'authorization' => [api_key, 'X']}
76
- http.errback { logger.error "Couldn't get data for channel #{channel_id} at url #{url}" }
77
- http.callback {
78
- logger.debug "Fetched channel data for #{channel_id}"
79
- room = Yajl::Parser.parse(http.response)['room']
80
- channel_cache[room["id"]] = room
81
- room['users'].each do |u|
82
- update_user_cache_with(u["id"], u)
83
- end
84
- }
85
- end
86
-
87
- def join_and_stream(id)
88
- join(id) do
89
- logger.info "Joined channel #{id} successfully"
90
- fetch_channel_data(id)
91
- stream(id)
92
- end
93
- end
94
-
95
- def stream(channel_id)
96
- json_parser = Yajl::Parser.new :symbolize_keys => true
97
- json_parser.on_parse_complete = method(:process_message)
98
-
99
- url = "https://streaming.campfirenow.com/room/#{channel_id}/live.json"
100
- # Timeout per https://github.com/igrigorik/em-http-request/wiki/Redirects-and-Timeouts
101
- http = EventMachine::HttpRequest.new(url, :connect_timeout => 20, :inactivity_timeout => 0).get :head => {'authorization' => [api_key, 'X']}
102
- http.errback { logger.error "Couldn't stream channel #{channel_id} at url #{url}" }
103
- http.callback { logger.error "Disconnected from #{url}"; channels_to_join << channel_id}
104
- http.stream {|chunk| json_parser << chunk }
105
- end
106
-
107
- def channel_id_from_channel_name(channel_name)
108
- logger.debug "Looking for channel id for #{channel_name}"
109
- channels[channel_name]["id"]
110
- end
111
- end
112
- end