Class: SimpleSlack::Getter

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_slack/getter.rb

Instance Method Summary collapse

Constructor Details

#initialize(slack) ⇒ Getter

Returns a new instance of Getter.



3
4
5
# File 'lib/simple_slack/getter.rb', line 3

def initialize(slack)
  @slack = slack
end

Instance Method Details

#channel(key, options = []) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/simple_slack/getter.rb', line 20

def channel(key, options = [])
  key.to_s =~ /\AC.{8}\Z/ ? key_id = key : key_name = key
  channel_list = channels(options)
  if key_id
    channel_list.find{|ch| ch[:id]   == key_id   }
  elsif key_name
    channel_list.find{|ch| ch[:name] == key_name }
  end
end

#channels(options = [], cache: true) ⇒ Object

use options for :is_channel, :creator, :members, :topic, :purpose, :num_members, etc



9
10
11
12
13
14
15
16
17
18
# File 'lib/simple_slack/getter.rb', line 9

def channels(options = [], cache: true)
  return @channels if cache && !@channels.nil?
  channels = @slack.channels_list
  @channels = begin
    channels["channels"].map do |channel|
      add_params = options.empty? ?  {} : options_to_hash(options, channel)
      { id: channel["id"], name: channel["name"] }.merge(add_params)
    end.sort_by {|ch| ch[:name] }
  end
end

#chatObject



108
109
110
# File 'lib/simple_slack/getter.rb', line 108

def chat
  "yet"
end

#chatsObject



104
105
106
# File 'lib/simple_slack/getter.rb', line 104

def chats
  "yet"
end

#im(key, options = []) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/simple_slack/getter.rb', line 94

def im(key, options = [])
  key.to_s =~ /\AU.{8}\Z/ ? key_id = key : key_name = key
  im_list = ims(options)
  if key_id
    im_list.find{|im| im[:user][:id]   == key_id }
  elsif key_name
    im_list.find{|im| im[:user][:name] == key_name }
  end
end

#image(key, options = []) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/simple_slack/getter.rb', line 66

def image(key, options = [])
  key.to_s =~ /\AU.{8}\Z/ ? key_id = key : key_name = key
  image_list = images(options)
  if key_id
    image_list.find{|user| user[:id]   == key_id }
  elsif key_name
    image_list.find{|user| user[:name] == key_name }
  end
end

#images(options = [], cache: true) ⇒ Object

use options for :image_24, :image_32, :image_48, image_72, etc…



55
56
57
58
59
60
61
62
63
64
# File 'lib/simple_slack/getter.rb', line 55

def images(options = [], cache: true)
  return @images if cache && !@images.nil?
  users = @slack.users_list
  @images = begin
    users["members"].map do |user|
      add_params = options.empty? ? {} : options_to_hash(options, user["profile"])
      { id: user["id"], name: user["name"], image: user["profile"]["image_24"] }.merge(add_params)
    end
  end
end

#ims(options = [], cache: true) ⇒ Object

use options for :created, :is_im, :is_org_shared, :is_user_deleted



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/simple_slack/getter.rb', line 78

def ims(options = [], cache: true)
  return @ims if cache && !@ims.nil?
  im_list = @slack.im_list
  @ims = begin
    im_list["ims"].map do |im|
      im_user = if im["user"] == "USLACKBOT"
                  { id: im["user"], name: "slackbot" }
                else
                  user(im["user"])
                end
      add_params = options.empty? ? {} : options_to_hash(options, im)
      { id: im["id"], user: im_user }.merge(add_params)
    end
  end
end

#user(key, options = []) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/simple_slack/getter.rb', line 43

def user(key, options = [])
  key.to_s =~ /\AU.{8}\Z/ ? key_id = key : key_name = key
  user_list = users(options)
  if key_id
    user_list.find{|user| user[:id]   == key_id }
  elsif key_name
    user_list.find{|user| user[:name] == key_name }
  end
end

#users(options = [], cache: true) ⇒ Object

use options for :real_name, :is_admin, :is_bot, etc…



32
33
34
35
36
37
38
39
40
41
# File 'lib/simple_slack/getter.rb', line 32

def users(options = [], cache: true)
  return @users if cache && !@users.nil?
  users = @slack.users_list
  @users = begin
    users["members"].map do |user|
      add_params = options.empty? ? {} : options_to_hash(options, user)
      { id: user["id"], name: user["name"] }.merge(add_params)
    end.sort_by {|ch| ch[:name] }
  end
end