hobostove 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 86c7295b9bbe575c31eb65da98a8fe03550f6208
4
- data.tar.gz: 931f5f3e8b558e871950dda4736c1794ddda0b22
3
+ metadata.gz: 1fb76c2892a743794debc51500b56ca9b8b222db
4
+ data.tar.gz: 12ea293f89da75edfde81e350355a760c544daa0
5
5
  SHA512:
6
- metadata.gz: c604d75ce1366ba30b857da0ca58797625bfb62779db89cf0d940513fdb622fe6d15c0dc3764be4e8686ab1ce03903eefcad2fd098137731ebfea2403839db4c
7
- data.tar.gz: ec9ecffc302cde28fc91eeca3a9459ea296520dcf39dddbc6dabe525c59325e5aae31f5e8af82f7e624fa57df4f08c0d4bfc4e15ba91e48fac636421757962e7
6
+ metadata.gz: e37dafa2929d7ecc9e1c01543649e4a9f8739658e698311b35a6cf91c0f00402d69db36a371e8fbdb97667e7503a885d5e971a5c9331a794b2082271298a8114
7
+ data.tar.gz: 4d2e0255a4777d4793bd3518a7f2c697857d8957f221bd619eb4d78bf14bdabd9414214e70b3fa1167be9fcf28e56cff4ceb24d0605cb4f7c2e8080df3fdaf75
@@ -0,0 +1,96 @@
1
+ module Hobostove
2
+ class Campfire
3
+ attr_reader :room_id
4
+
5
+ def initialize
6
+ load_current_room
7
+
8
+ @users = {}
9
+ end
10
+
11
+ def recent_messages
12
+ messages = connection.get("/room/#{room_id}/recent.json?limit=10").body
13
+ messages = JSON.parse(messages)["messages"]
14
+ messages.map do |message|
15
+ message = Hobostove::Models::Message.new(
16
+ message["id"],
17
+ Time.parse(message["created_at"]),
18
+ message["type"],
19
+ user(message["user_id"]),
20
+ message["body"]
21
+ )
22
+ message = handle_upload(message) if message.type == "UploadMessage"
23
+ message
24
+ end
25
+ end
26
+
27
+ def send_message(message)
28
+ connection.post("/room/#{room_id}/speak.json") do |req|
29
+ req.body = {
30
+ :message => {
31
+ :type => "TextMessage",
32
+ :body => message,
33
+ }
34
+ }.to_json
35
+ end
36
+ end
37
+
38
+ def current_users
39
+ room = connection.get("/room/#{room_id}.json").body
40
+ room = JSON.parse(room)["room"]
41
+ room["users"].map do |user|
42
+ @users[user["id"]] = Hobostove::Models::User.new(user["id"], user["name"])
43
+ end
44
+ end
45
+
46
+ def join
47
+ connection.post("/room/#{room_id}/join.json")
48
+ end
49
+
50
+ def leave
51
+ connection.post("/room/#{room_id}/leave.json")
52
+ end
53
+
54
+ private
55
+
56
+ def base_uri
57
+ "https://#{Configuration.subdomain}.campfirenow.com"
58
+ end
59
+
60
+ def connection
61
+ return @connection if @connection
62
+ @connection = Faraday.new(base_uri) do |faraday|
63
+ faraday.adapter *Faraday.default_adapter
64
+ end
65
+ @connection.basic_auth(Configuration.token, "x")
66
+ @connection
67
+ end
68
+
69
+ def load_current_room
70
+ rooms = connection.get("/rooms.json").body
71
+ rooms = JSON.parse(rooms)["rooms"]
72
+ @room_id = rooms.find do |room|
73
+ room["name"] == Configuration.room
74
+ end["id"]
75
+ end
76
+
77
+ def user(user_id)
78
+ return @users[user_id] if @users[user_id]
79
+ user = connection.get("/users/#{user_id}.json").body
80
+ user = JSON.parse(user)["user"]
81
+ @users[user_id] = Hobostove::Models::User.new(user_id, user["name"])
82
+ end
83
+
84
+ def handle_upload(message)
85
+ upload = connection.get("room/#{room_id}/messages/#{message.id}/upload.json").body
86
+ upload = JSON.parse(upload)["upload"]
87
+ Hobostove::Models::Message.new(
88
+ message.id,
89
+ message.timestamp,
90
+ message.type,
91
+ message.user,
92
+ upload["full_url"],
93
+ )
94
+ end
95
+ end
96
+ end
@@ -1,5 +1,5 @@
1
1
  module Hobostove
2
- class MessageRenderer < Struct.new(:subdomain, :room_id, :window_size)
2
+ class MessageRenderer < Struct.new(:window_size)
3
3
  def render_lines(message)
4
4
  message = render(message)
5
5
  return [] if message.nil?
@@ -19,7 +19,7 @@ module Hobostove
19
19
  end
20
20
 
21
21
  def main
22
- room.join
22
+ campfire.join
23
23
 
24
24
  while @running && (ch = Curses.getch)
25
25
  Hobostove.logger.debug("#{ch} - #{ch.chr.inspect} pressed")
@@ -45,7 +45,7 @@ module Hobostove
45
45
  @message_panel.update_cursor
46
46
  end
47
47
 
48
- room.leave
48
+ campfire.leave
49
49
  end
50
50
 
51
51
  def speak
@@ -54,18 +54,17 @@ module Hobostove
54
54
  return
55
55
  end
56
56
 
57
- room.speak @current_message
57
+ campfire.send_message @current_message
58
58
  @current_message = ""
59
59
  end
60
60
 
61
61
  def stream
62
62
  Thread.new do
63
63
  loop do
64
- recent = room.recent(10)
64
+ recent = campfire.recent_messages
65
65
  recent.each do |message|
66
- next if messages.include?(message[:id])
67
- messages << message[:id]
68
-
66
+ next if messages.include?(message.id)
67
+ messages << message.id
69
68
  handle_message(message)
70
69
  end
71
70
 
@@ -78,7 +77,6 @@ module Hobostove
78
77
 
79
78
  def handle_message(message)
80
79
  Hobostove.logger.debug(message.inspect)
81
- message = Models::Message.convert(message)
82
80
 
83
81
  case message.type
84
82
  when "TextMessage"
@@ -88,15 +86,7 @@ module Hobostove
88
86
  when "LeaveMessage"
89
87
  @users_panel.remove_user(message.user)
90
88
  when "UploadMessage"
91
- upload = room.send(:get, "messages/#{message.id}/upload")
92
- Hobostove.logger.debug(upload.inspect)
93
- message = Models::Message.new(
94
- message.id,
95
- message.timestamp,
96
- message.type,
97
- message.user,
98
- upload.upload.full_url
99
- )
89
+ Notify.notify message.username, message.body
100
90
  end
101
91
 
102
92
  message_renderer.render_lines(message).each do |line|
@@ -106,15 +96,11 @@ module Hobostove
106
96
  Hobostove.logger.fatal(e.inspect)
107
97
  end
108
98
 
109
- def room
110
- @room ||= campfire.find_room_by_name(Configuration.room)
111
- end
112
-
113
99
  private
114
100
 
115
101
  def message_renderer
116
102
  @message_renderer ||=
117
- MessageRenderer.new(Configuration.subdomain, room.id, Curses.cols - 25)
103
+ MessageRenderer.new(Curses.cols - 25)
118
104
  end
119
105
 
120
106
  def start_curses
@@ -138,15 +124,15 @@ module Hobostove
138
124
  end
139
125
 
140
126
  def load_users
141
- room.users.each do |user|
127
+ campfire.current_users.each do |user|
142
128
  Hobostove.logger.info user.name
143
- @users_panel.add_user(Models::User.convert(user), false)
129
+ @users_panel.add_user(user, false)
144
130
  end
145
131
  @users_panel.refresh
146
132
  end
147
133
 
148
134
  def campfire
149
- @campfire ||= Tinder::Campfire.new Configuration.subdomain, :token => Configuration.token
135
+ @campfire ||= Campfire.new
150
136
  end
151
137
  end
152
138
  end
data/lib/hobostove.rb CHANGED
@@ -11,6 +11,7 @@ require 'yaml'
11
11
  require 'hobostove/models/message'
12
12
  require 'hobostove/models/user'
13
13
 
14
+ require 'hobostove/campfire'
14
15
  require 'hobostove/message_renderer'
15
16
 
16
17
  require 'hobostove/cli/setup'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hobostove
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Oestrich
@@ -132,6 +132,7 @@ extra_rdoc_files: []
132
132
  files:
133
133
  - bin/hobostove
134
134
  - lib/hobostove.rb
135
+ - lib/hobostove/campfire.rb
135
136
  - lib/hobostove/cli/room_picker.rb
136
137
  - lib/hobostove/cli/setup.rb
137
138
  - lib/hobostove/cli/upgrade.rb