class Embulk::Plugin::SlackApi

Public Class Methods

new(token) click to toggle source
# File lib/embulk/input/slack_history.rb, line 11
def initialize(token)
  @token = token
  @members = {}
  @groups = {}
  @channels = {}
end

Public Instance Methods

get_continuous_param(continuous, filepath, channelid) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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{|message|

      mes = parse_history(message)
      mes["channelid"] = id
      mes["channelname"] = name
      mes["private"] = isprivate
      res[i] = mes
      i += 1

      ts = message['ts'].to_f
      if newest < ts then
        newest = ts
      end

    }

    update_oldest(continuous, filepath, id, newest)

  }

  return res

end
get_oldest_filepath(filepath, channelid) click to toggle source
# File lib/embulk/input/slack_history.rb, line 75
def get_oldest_filepath(filepath, channelid)
  return filepath + "/" + channelid + ".oldest"
end
parse_history(message) click to toggle source
# File lib/embulk/input/slack_history.rb, line 62
def parse_history(message)

  res = {}

  res["ts"] = message["ts"]
  res["username"] = @members[message["user"]]
  res["userid"] = message["user"]
  res["message"] = message["text"]

  return res

end
pre() click to toggle source
# 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) click to toggle source
# 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