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.



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

def initialize(slack)
  @slack = slack
end

Instance Method Details

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



16
17
18
19
20
21
22
23
24
# File 'lib/simple_slack/getter.rb', line 16

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 = []) ⇒ Object

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



8
9
10
11
12
13
14
# File 'lib/simple_slack/getter.rb', line 8

def channels(options = [])
  channels = @slack.channels_list
  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

#chatObject



95
96
97
# File 'lib/simple_slack/getter.rb', line 95

def chat
  "yet"
end

#chatsObject



91
92
93
# File 'lib/simple_slack/getter.rb', line 91

def chats
  "yet"
end

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



81
82
83
84
85
86
87
88
89
# File 'lib/simple_slack/getter.rb', line 81

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



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

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 = []) ⇒ Object

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



48
49
50
51
52
53
54
# File 'lib/simple_slack/getter.rb', line 48

def images(options = [])
  users = @slack.users_list
  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

#ims(options = []) ⇒ Object

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



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/simple_slack/getter.rb', line 68

def ims(options = [])
  im_list = @slack.im_list
  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

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



36
37
38
39
40
41
42
43
44
# File 'lib/simple_slack/getter.rb', line 36

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 = []) ⇒ Object

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



28
29
30
31
32
33
34
# File 'lib/simple_slack/getter.rb', line 28

def users(options = [])
  users = @slack.users_list
  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