slackup 0.0.2 → 0.0.3

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: 217865baf5660d9ee09b17a815f9849c8f714f1e
4
- data.tar.gz: 27907d862fd07bbb93b7aa1cc4bdaa8f7ea4133b
3
+ metadata.gz: 9f775a52592ebb2b334b49c07577edb1d63ae909
4
+ data.tar.gz: c8f07567b097c4138ddccf4d1359c711d24dc6c4
5
5
  SHA512:
6
- metadata.gz: 5e83bd95a1104f5a24eb818f2554163af2ac4218ba595ed876558b7a72f776948b54062b78c054e52dbff76d4b84d2a0f1ec4f32a1d9927d1a6207559578285b
7
- data.tar.gz: 69a6419c81695b8a83d62e239881b7843278181558058fa46405494a8c99cb9a26d90367e1aa30545f0f6209367e2827dda3c90e1ffe9052f9a91011f44bb340
6
+ metadata.gz: a8991c5c8a2b0227974db3c2e0ccb33a3d6a69df8154145a1b9e90bfe9aa17a64fadb158999cf9867119a9b0f152744c9c6555359aec2dc0fd0c532d199ad494
7
+ data.tar.gz: 6dadcf2bc8c6adc5d0bd692800b541176a90664a7f93b251d9786e36e5727b9e09c8ee208c47bca2cef21e45cefa05d2a7da4c945e83638415cf53109fcea8cd
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- slackup (0.0.2)
4
+ slackup (0.0.3)
5
5
  slack-api (~> 1.1, >= 1.1.3)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -45,6 +45,8 @@ This gem is does the bare basics of backup and works for me.
45
45
 
46
46
  It has no tests.
47
47
 
48
+ It depends on the ['slack-api' gem](https://github.com/aki017/slack-ruby-gem)
49
+
48
50
  ## Contributing
49
51
 
50
52
  1. Fork it ( https://github.com/[my-github-username]/slackup/fork )
@@ -1,3 +1,3 @@
1
1
  module Slackup
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/slackup.rb CHANGED
@@ -42,12 +42,18 @@ class Slackup
42
42
  def execute
43
43
  SEMAPHORE.synchronize do
44
44
  authorize! &&
45
- Dir.chdir(name) do
46
- channels.each do |channel|
47
- write_channel_messages(channel)
45
+ Dir.chdir(name) do
46
+ channels.each do |channel|
47
+ write_channel_messages(channel)
48
+ end
49
+ write_stars
50
+ write_users
51
+ Dir.chdir(ims_dir) do
52
+ im_list.each do |im|
53
+ write_im_messages(im)
54
+ end
55
+ end
48
56
  end
49
- write_stars
50
- end
51
57
  end
52
58
  end
53
59
 
@@ -67,45 +73,162 @@ class Slackup
67
73
  end
68
74
  end
69
75
 
76
+ User = Struct.new(:user_hash) do
77
+ def id; user_hash["id"]; end
78
+
79
+ def name; user_hash["name"]; end
80
+
81
+ def deleted; user_hash["deleted"]; end
82
+
83
+ def color; user_hash["color"]; end
84
+
85
+ def profile; user_hash["profile"]; end
86
+
87
+ def admin?; user_hash["is_admin"]; end
88
+
89
+ def owner?; user_hash["is_owner"]; end
90
+
91
+ def has_2fa?; user_hash["has_2fa"]; end
92
+
93
+ def has_files?; user_hash["has_files"]; end
94
+
95
+ def to_hash; user_hash; end
96
+ end
97
+ # {
98
+ # "ok": true,
99
+ # "members": [
100
+ # {
101
+ # "id": "U023BECGF",
102
+ # "name": "bobby",
103
+ # "deleted": false,
104
+ # "color": "9f69e7",
105
+ # "profile": {
106
+ # "first_name": "Bobby",
107
+ # "last_name": "Tables",
108
+ # "real_name": "Bobby Tables",
109
+ # "email": "bobby@slack.com",
110
+ # "skype": "my-skype-name",
111
+ # "phone": "+1 (123) 456 7890",
112
+ # "image_24": "https:\/\/...",
113
+ # "image_32": "https:\/\/...",
114
+ # "image_48": "https:\/\/...",
115
+ # "image_72": "https:\/\/...",
116
+ # "image_192": "https:\/\/..."
117
+ # },
118
+ # "is_admin": true,
119
+ # "is_owner": true,
120
+ # "has_2fa": false,
121
+ # "has_files": true
122
+ # },
123
+ # ]
124
+ # }
70
125
  def users
71
- @users ||= Slack.users_list["members"]
126
+ @users ||= Slack.users_list["members"].map { |member| User.new(member) }
72
127
  end
73
128
 
74
129
  def channels
75
130
  @channels ||= Slack.channels_list["channels"]
76
131
  end
77
132
 
133
+ Im = Struct.new(:im_hash) do
134
+ def id; im_hash["id"]; end
135
+
136
+ def user; im_hash["user"]; end
137
+ end
138
+ # @return [Hash]
139
+ # @example
140
+ # {
141
+ # "ok"=>true,
142
+ # "ims"=>[
143
+ # {"id"=>"D1234567890", "is_im"=>true, "user"=>"USLACKBOT", "created"=>1372105335, "is_user_deleted"=>false},
144
+ # ]
145
+ # }
146
+ def im_list
147
+ @im_list ||= Slack.im_list["ims"].map { |im| Im.new(im) }
148
+ end
149
+
150
+ # @param im_id [String] is the 'channel' of the im, e.g. "D1234567890"
151
+ # @return [Hash]
152
+ # @example return
153
+ # {
154
+ # "ok": true,
155
+ # "latest": "1358547726.000003",
156
+ # "messages": [
157
+ # {
158
+ # "type": "message",
159
+ # "ts": "1358546515.000008",
160
+ # "user": "U2147483896",
161
+ # "text": "Hello"
162
+ # },
163
+ # ]
164
+ # "has_more": false
165
+ def im_history(im_id)
166
+ Slack.im_history(channel: im_id)
167
+ end
168
+
78
169
  def write_channel_messages(channel)
79
- messages = Slack.channels_history({channel: channel["id"], count: "1000"})["messages"]
80
- File.open(backup_filename(channel['name']),"w") do |f|
81
- f.write format_channel_messages(messages)
170
+ messages = Slack.channels_history(channel: channel["id"], count: "1000")["messages"]
171
+ File.open(backup_filename(channel["name"]), "w") do |f|
172
+ formatted_messages = format_channel_messages(messages)
173
+ f.write serialize(formatted_messages)
82
174
  end
83
175
  end
84
176
 
85
- def format_channel_messages(messages)
177
+ def format_messages(messages)
86
178
  messages.reverse.map { |msg|
87
- if (msg.has_key?("text") && msg.has_key?("user"))
88
- users.each do |user|
89
- if user["id"] == msg["user"]
90
- break msg["user"] = user["name"]
91
- end
92
- end
179
+ if msg.has_key?("text") && msg.has_key?("user")
180
+ msg["user"] = user_name(msg["user"])
93
181
  msg
94
182
  else
95
183
  nil
96
184
  end
97
- }.compact.to_yaml
185
+ }.compact
186
+ end
187
+
188
+ alias_method :format_channel_messages, :format_messages
189
+ alias_method :format_im_messages, :format_messages
190
+
191
+ # gets user name for an id, if mapping is known, else returns the input
192
+ def user_name(user_id)
193
+ @user_names ||= users.each_with_object({}) {|user, lookup|
194
+ lookup[user.id] = user.name
195
+ }
196
+ @user_names.fetch(user_id) {
197
+ user_id
198
+ }
98
199
  end
99
200
 
100
201
  def write_stars
101
- File.open(backup_filename("stars"),"w") do |f|
202
+ File.open(backup_filename("stars"), "w") do |f|
102
203
  stars = Slack.stars_list(count: "1000", page: "1")
103
- f.write(format_stars(stars))
204
+ f.write(serialize(stars))
205
+ end
206
+ end
207
+
208
+ def write_users
209
+ File.open(backup_filename("users"), "w") do |f|
210
+ f.write(serialize(users.map(&:to_hash)))
211
+ end
212
+ end
213
+
214
+ def serialize(obj)
215
+ obj.to_yaml
216
+ end
217
+
218
+ def write_im_messages(im)
219
+ messages = im_history(im.id)["messages"]
220
+ im_username = user_name(im.user).downcase.gsub(/\s+/, "-")
221
+ formatted_messages = format_im_messages(messages)
222
+ return if formatted_messages.empty?
223
+ File.open(backup_filename(im_username), "w") do |f|
224
+ f.write serialize(formatted_messages)
104
225
  end
105
226
  end
106
227
 
107
- def format_stars(stars)
108
- stars.to_yaml
228
+ def ims_dir
229
+ @ims_dir ||= "ims"
230
+ FileUtils.mkdir_p(@ims_dir)
231
+ @ims_dir
109
232
  end
110
233
 
111
234
  def backup_filename(name)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slackup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin Fleischer
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-05-14 00:00:00.000000000 Z
11
+ date: 2015-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: slack-api