Class: Embulk::Plugin::SlackApi
- Inherits:
-
Object
- Object
- Embulk::Plugin::SlackApi
- Defined in:
- lib/embulk/input/slack_history.rb
Instance Method Summary collapse
- #get_continuous_param(continuous, filepath, channelid) ⇒ Object
- #get_history(continuous, filepath) ⇒ Object
- #get_history_by_channels(continuous, filepath, channels, isprivate) ⇒ Object
- #get_oldest_filepath(filepath, channelid) ⇒ Object
-
#initialize(token) ⇒ SlackApi
constructor
A new instance of SlackApi.
- #parse_history(message) ⇒ Object
- #pre ⇒ Object
- #update_oldest(continuous, filepath, channelid, newest) ⇒ Object
Constructor Details
#initialize(token) ⇒ SlackApi
Returns a new instance of SlackApi.
11 12 13 14 15 16 |
# File 'lib/embulk/input/slack_history.rb', line 11 def initialize(token) @token = token @members = {} @groups = {} @channels = {} end |
Instance Method Details
#get_continuous_param(continuous, filepath, channelid) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/embulk/input/slack_history.rb', line 79 def get_continuous_param(continuous, filepath, channelid) if !continuous then return "" end param = "" fullpath = get_oldest_filepath(filepath, channelid) if !File.exist?(fullpath) then return "" end line = File.read(fullpath) return line end |
#get_history(continuous, filepath) ⇒ Object
153 154 155 156 157 158 159 160 |
# File 'lib/embulk/input/slack_history.rb', line 153 def get_history(continuous, filepath) res = get_history_by_channels(continuous, filepath, @channels, "no") res.concat(get_history_by_channels(continuous, filepath, @groups, "yes")) return res end |
#get_history_by_channels(continuous, filepath, channels, isprivate) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/embulk/input/slack_history.rb', line 110 def get_history_by_channels(continuous, filepath, channels, isprivate) res = [] i = 0 channels.each{|id, name| api = "channels" if isprivate == "yes" then api = "groups" end oldest = get_continuous_param(continuous, filepath, id) json = RestClient.get('https://slack.com/api/' + api + '.history', {:params => {'token' => @token, 'channel' => id, 'oldest' => oldest, 'inclusive' => 0, 'pretty' => 1}}) result = JSON.parse(json) newest = 0.0 result["messages"].each{|| mes = parse_history() mes["channelid"] = id mes["channelname"] = name mes["private"] = isprivate res[i] = mes i += 1 ts = ['ts'].to_f if newest < ts then newest = ts end } update_oldest(continuous, filepath, id, newest) } return res end |
#get_oldest_filepath(filepath, channelid) ⇒ Object
75 76 77 |
# File 'lib/embulk/input/slack_history.rb', line 75 def get_oldest_filepath(filepath, channelid) return filepath + "/" + channelid + ".oldest" end |
#parse_history(message) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/embulk/input/slack_history.rb', line 62 def parse_history() res = {} res["ts"] = ["ts"] res["username"] = @members[["user"]] res["userid"] = ["user"] res["message"] = ["text"] return res end |
#pre ⇒ Object
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/embulk/input/slack_history.rb', line 18 def pre() # TODO: need implement error handling # HTTP Status Code # RestClient Exception # result is NOT 'ok' json = RestClient.get('https://slack.com/api/channels.list', {:params => {'token' => @token, 'pretty' => 1}}) result = JSON.parse(json) if !result['ok'] then raise SlackApiException end result["channels"].each{|channel| @channels[channel["id"]] = channel["name"] } json = RestClient.get('https://slack.com/api/groups.list', {:params => {'token' => @token, 'pretty' => 1}}) result = JSON.parse(json) if !result['ok'] then raise SlackApiException end result["groups"].each{|group| @groups[group["id"]] = group["name"] } json = RestClient.get('https://slack.com/api/users.list', {:params => {'token' => @token, 'pretty' => 1}}) result = JSON.parse(json) if !result['ok'] then raise SlackApiException end result["members"].each{|member| @members[member["id"]] = member["name"] } return true end |
#update_oldest(continuous, filepath, channelid, newest) ⇒ Object
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/embulk/input/slack_history.rb', line 99 def update_oldest(continuous, filepath, channelid, newest) if !continuous || newest == 0.0 then return end fullpath = get_oldest_filepath(filepath, channelid) File.write(fullpath, newest) end |