diff --git a/.gitignore b/.gitignore index 8d6a243f..443e046c 100644 --- a/.gitignore +++ b/.gitignore @@ -54,3 +54,4 @@ build-iPhoneSimulator/ # Ignore cassette files /specs/cassettes/ +.DS_Store diff --git a/lib/channel.rb b/lib/channel.rb new file mode 100644 index 00000000..c898a556 --- /dev/null +++ b/lib/channel.rb @@ -0,0 +1,24 @@ +require 'table_print' +require "httparty" +require "dotenv" + +class Channel + attr_reader :name, :id, :topic, :member_count + # @topic = topic + # @member_count = member_count + + def initialize(channel_map) + @id = channel_map['id'] + @topic = channel_map['topic']['value'] + @member_count = channel_map['num_members'] + @name = channel_map['name'] + end + + # def details + # return "slack id: #{@id} #{@topic} member count: #{@member_count}" + # end + + def to_s + "Name:#{@name},Topic:#{@topic},Member count :#{@member_count},Slack ID: #{@id}" + end +end diff --git a/lib/recipient.rb b/lib/recipient.rb new file mode 100644 index 00000000..957e8e79 --- /dev/null +++ b/lib/recipient.rb @@ -0,0 +1,24 @@ +require 'dotenv' +require 'httparty' +Dotenv.load + +class Recipient + @slack_id = slack_id + @name = name + + def send_message(message) + #code here + end + + # def self.get(url, params) + # #code here + # end + + def details + raise 'Please implement me' + end + + def self.list + raise 'Please implement me' + end +end diff --git a/lib/slack-cli.rb b/lib/slack-cli.rb new file mode 100755 index 00000000..8f181021 --- /dev/null +++ b/lib/slack-cli.rb @@ -0,0 +1,60 @@ +require_relative "./workspace" + +class SlackCLI + def initialize(api_token) + @commands = { + 'list users' => self.method(:list_users), + 'list channels' => self.method(:list_channels), + 'select user' => self.method(:select_user), + 'select channel' => self.method(:select_channel), + 'exit' => self.method(:quit), + 'quit' => self.method(:quit), + } + @time_to_go = false + @workspace = Workspace.new(api_token) + end + + def quit + @time_to_go = true + end + + def list_users + puts @workspace.users + end + + def list_channels + puts @workspace.channels + end + + def select_user + print "which user? " + user = gets.chomp + selected_user = @workspace.select_user(user) + if selected_user.nil? + puts "thats not a user try again" + end + end + + def select_channel + print "which channel? " + channel = gets.chomp + selected_channel = @workspace.select_channel(channel) + if selected_channel.nil? + puts "thats not a user try again" + end + end + + def run + while !@time_to_go do + puts "Please enter one of the available commands: #{@commands.keys}" + print 'Command: ' + command = gets.chomp + if @commands.key? command + @commands[command].call + else + puts 'You silly goose! That is no command!' + end + end + puts 'Toodles!' + end +end diff --git a/lib/slack.rb b/lib/slack.rb deleted file mode 100755 index 960cf2f7..00000000 --- a/lib/slack.rb +++ /dev/null @@ -1,11 +0,0 @@ -#!/usr/bin/env ruby - -def main - puts "Welcome to the Ada Slack CLI!" - - # TODO project - - puts "Thank you for using the Ada Slack CLI" -end - -main if __FILE__ == $PROGRAM_NAME \ No newline at end of file diff --git a/lib/user.rb b/lib/user.rb new file mode 100644 index 00000000..8e8bdb8d --- /dev/null +++ b/lib/user.rb @@ -0,0 +1,22 @@ +class User + attr_accessor :username, :real_name, :id + def initialize(user_map) + @username = user_map['name'] + @real_name = user_map['real_name'] + @id = user_map['id'] + # @status_text = user_map['status_text'] + # @status_emoji = user_map['status_emoji'] + end + + # def details do + # #code here + # end + # + # def self.list + # #code here + # end + + def to_s + "Username: #{@username}, Real name: #{@real_name}, Slack ID: #{@id}" + end +end diff --git a/lib/workspace.rb b/lib/workspace.rb new file mode 100644 index 00000000..6961ecb7 --- /dev/null +++ b/lib/workspace.rb @@ -0,0 +1,65 @@ +require 'httparty' +require 'dotenv' +require_relative './user' +require_relative './channel' + +# response = HTTParty.get('https://slack.com/api/channels.list?token=xoxp-my-great-key&pretty=1') +# puts response.body, response.code, response.message, response.headers.inspect + +# https://slack.com/api/channels.list?token=xoxp-my-great-key&pretty=1 +class Workspace + include HTTParty + base_uri 'slack.com/api' + + def initialize(api_token) + @options = { query: { token: api_token, pretty: 1 } } + end + + def channels + response = self.class.get('/channels.list', @options).parsed_response + raise 'Failed to get channels' unless response['ok'] + + channels = response['channels'].map do |channel_map| + Channel.new(channel_map) + end + channels + end + + def users + response = self.class.get('/users.list', @options).parsed_response + raise 'Failed to get users' unless response['ok'] + + users = response['members'].map do |user_map| + User.new(user_map) + end + users + end + + def select_channel(selected_channel) + channels.each do |channel| + if channel.name == selected_channel || channel.id == selected_channel + @selected = channel + return channel + end + end + nil + end + + def select_user(selected_user) + users.each do |user| + if user.username == selected_user || user.id == selected_user + @selected = user + return user + end + end + nil + end + + # def show_details(details) + # #code + # end + + # def send_message + # #code + # end +end diff --git a/slack-cli b/slack-cli new file mode 100755 index 00000000..56087527 --- /dev/null +++ b/slack-cli @@ -0,0 +1,9 @@ +#! /usr/bin/env ruby +#shebang line. +require "dotenv" +require_relative 'lib/slack-cli' + +Dotenv.load('.env') +api_token = ENV['SLACK_API_TOKEN'] +cli = SlackCLI.new(api_token) +cli.run() diff --git a/specs/channel_spec.rb b/specs/channel_spec.rb new file mode 100644 index 00000000..e69de29b diff --git a/specs/recipient_spec.rb b/specs/recipient_spec.rb new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/specs/recipient_spec.rb @@ -0,0 +1 @@ + diff --git a/specs/test_helper.rb b/specs/test_helper.rb index 81ccd06b..e23ea304 100644 --- a/specs/test_helper.rb +++ b/specs/test_helper.rb @@ -12,4 +12,4 @@ VCR.configure do |config| config.cassette_library_dir = "specs/cassettes" config.hook_into :webmock -end \ No newline at end of file +end diff --git a/specs/user_spec.rb b/specs/user_spec.rb new file mode 100644 index 00000000..e69de29b diff --git a/specs/workspace_spec.rb b/specs/workspace_spec.rb new file mode 100644 index 00000000..d0cb7287 --- /dev/null +++ b/specs/workspace_spec.rb @@ -0,0 +1,79 @@ +require "dotenv" + +require_relative './test_helper' +require_relative '../lib/workspace' + +describe "workspace" do + before do + Dotenv.load('.env') + api_token = ENV['SLACK_API_TOKEN'] + @workspace = Workspace.new(api_token) + end + + describe "channels" do + it "must be instance of array" do + VCR.use_cassette("good channels") do + channels = @workspace.channels + expect(channels).must_be_kind_of Array + end + end + end + + describe "select_channel" do + it "finds a chanel by channel name" do + VCR.use_cassette("select channel by name") do + # expect{workspace.select_user}.must_be + # arrange + channel_name = "random" + # act + selected_channel = @workspace.select_channel(channel_name) + # assert + expect(selected_channel.name).must_equal 'random' + end + end + + it "finds a channel by id" do + VCR.use_cassette("select channel by id") do + id = "CH36Q9YKX" + selected_channel = @workspace.select_channel(id) + expect(selected_channel.id).must_equal 'CH36Q9YKX' + end + end + + describe "users" do + it "must be instance of array" do + VCR.use_cassette("good users") do + users = @workspace.users + expect(users).must_be_kind_of Array + end + end + + it "must raise exception for bad response" do + workspace = Workspace.new('badapitoken') + VCR.use_cassette("bad channels") do + expect{workspace.channels}.must_raise RuntimeError + end + end + end + + describe "select_user" do + it "finds a user by username" do + VCR.use_cassette("select user") do + # arrange + user_name = "kateannnichols" + # act + selected_user = @workspace.select_user(user_name) + # assert + expect(selected_user.username).must_equal 'kateannnichols' + end + end + it "finds a user by id" do + VCR.use_cassette("select user") do + id = "UH2SA7YJE" + selected_user = @workspace.select_user(id) + expect(selected_user.id).must_equal 'UH2SA7YJE' + end + end + end + end +end