Module: Slack::API::Users
Instance Method Summary collapse
- #get_list ⇒ Object
- #get_profile(id) ⇒ Object
- #info(id) ⇒ Object
- #search(term, what = 'username', regex = false) ⇒ Object
- #set_profile(opts = {}) ⇒ Object
Instance Method Details
#get_list ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/slack-wrapper/api/users.rb', line 27 def get_list() if Slack::API::Auth uri = URI.parse('https://slack.com/api/users.list') http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE req = Net::HTTP::Post.new("#{uri.to_s}?token=#{Slack::Config.token}&presence=false") resp = http.request(req) false unless resp.code == 200 if JSON.parse(resp.body)['ok'] JSON.parse(resp.body)['members'] else Slack::Errors.new(JSON.parse(resp.body)) end else Slack::Errors.new({"error" => "not_authed"}) end end |
#get_profile(id) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/slack-wrapper/api/users.rb', line 64 def get_profile(id) opts = {} if Slack::API::Auth opts['user'] = id opts['token'] = Slack::Config.token opts['include_labels'] = true uri = URI.parse('https://slack.com/api/users.profile.get') opts['token'] = Slack::Config.token req = Net::HTTP::Post::Multipart.new(uri.path, opts) res = Net::HTTP::new(uri.host, uri.port) res.use_ssl = true res.verify_mode = OpenSSL::SSL::VERIFY_NONE resp = res.start do |http| http.request(req) end false unless resp.code == 200 if JSON.parse(resp.body)['ok'] JSON.parse(resp.body)['profile'] else Slack::Errors.new(JSON.parse(resp.body)) end else Slack::Errors.new({"error" => "not_authed"}) end end |
#info(id) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/slack-wrapper/api/users.rb', line 9 def info(id) if Slack::API::Auth uri = URI.parse('https://slack.com/api/users.info') http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE req = Net::HTTP::Post.new("#{uri.to_s}?token=#{Slack::Config.token}&user=#{id}") resp = http.request(req) false unless resp.code == 200 if JSON.parse(resp.body)['ok'] JSON.parse(resp.body)['user'] else Slack::Errors.new(JSON.parse(resp.body)) end else Slack::Errors.new({"error" => "not_authed"}) end end |
#search(term, what = 'username', regex = false) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/slack-wrapper/api/users.rb', line 45 def search(term, what='username', regex=false) users = get_list() out = nil case what when 'email' out = users.select{|u| u['profile']['email'] == term} unless regex out = users.select{|u| u['profile']['email'] =~ /#{term}/}.first if regex when 'username' out = users.select{|u| u['name'] == term}.first unless regex out = users.select{|u| u['name'] =~ /#{term}/} if regex when 'name' out = users.select{|u| u['profile']['real_name'] == term}.first unless regex out = users.select{|u| u['profile']['real_name'] =~ /#{term}/} if regex else Slack::Errors.new({"error" => "invalid_search_term", "detail" => "You can only search by username, name, or email"}) end out end |
#set_profile(opts = {}) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/slack-wrapper/api/users.rb', line 89 def set_profile(opts = {}) if Slack::API::Auth opts['token'] = Slack::Config.token uri = URI.parse('https://slack.com/api/users.profile.set') opts['token'] = Slack::Config.token req = Net::HTTP::Post::Multipart.new(uri.path, opts) res = Net::HTTP::new(uri.host, uri.port) res.use_ssl = true res.verify_mode = OpenSSL::SSL::VERIFY_NONE resp = res.start do |http| http.request(req) end false unless resp.code == 200 if JSON.parse(resp.body)['ok'] JSON.parse(resp.body)['profile'] else Slack::Errors.new(JSON.parse(resp.body)) end else Slack::Errors.new({"error" => "not_authed"}) end end |