From 6cb1dda3bff81cdfbbb754a68ac17863dce3858d Mon Sep 17 00:00:00 2001 From: Orta Therox Date: Wed, 16 Jan 2019 08:48:38 -0500 Subject: [PATCH] Migrates to using an enum to indicate what type of status you want, e.g. emoji or not. To allow extra status update types --- .vscode/settings.json | 5 +++ Gemfile | 3 +- Gemfile.lock | 25 ++++++++----- slack-moji/api/endpoints/slack_endpoint.rb | 4 +-- slack-moji/models/user.rb | 41 ++++++++++++---------- spec/api/endpoints/slack_endpoint_spec.rb | 4 +-- spec/models/user_spec.rb | 11 +++--- 7 files changed, 56 insertions(+), 37 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..dbc7e75 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "ruby.useLanguageServer": true, + "ruby.intellisense": "rubyLocate", + "ruby.codeCompletion": "rcodetools" +} \ No newline at end of file diff --git a/Gemfile b/Gemfile index e1405f2..5ded8a6 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,6 @@ source 'http://rubygems.org' -ruby '2.5.3' +ruby '~> 2.5' gem 'emoji_data' gem 'faker' @@ -10,6 +10,7 @@ gem 'hashie' gem 'httparty' gem 'mongoid' gem 'mongoid-scroll' +gem "mongoid-enum" gem 'newrelic_rpm' gem 'nokogiri' gem 'polylines' diff --git a/Gemfile.lock b/Gemfile.lock index 8ce5fd9..d27428d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,12 +1,13 @@ GEM remote: http://rubygems.org/ specs: - activemodel (5.2.1) - activesupport (= 5.2.1) - activesupport (5.2.1) - concurrent-ruby (~> 1.0, >= 1.0.2) - i18n (>= 0.7, < 2) + activemodel (4.2.11) + activesupport (= 4.2.11) + builder (~> 3.1) + activesupport (4.2.11) + i18n (~> 0.7) minitest (~> 5.1) + thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) addressable (2.5.2) public_suffix (>= 2.0.2, < 4.0) @@ -37,7 +38,7 @@ GEM ffi (~> 1.0, >= 1.0.11) coercible (1.0.0) descendants_tracker (~> 0.0.1) - concurrent-ruby (1.0.5) + concurrent-ruby (1.1.4) crack (0.4.3) safe_yaml (~> 1.0.0) dante (0.2.0) @@ -104,7 +105,7 @@ GEM faraday_middleware net-http-digest_auth uri_template - i18n (1.1.0) + i18n (0.9.5) concurrent-ruby (~> 1.0) ice_nine (0.11.2) jaro_winkler (1.5.2) @@ -123,12 +124,16 @@ GEM minitest (5.11.3) mongo (2.6.2) bson (>= 4.3.0, < 5.0.0) - mongoid (7.0.2) - activemodel (>= 5.1, < 6.0.0) + mongoid (5.4.0) + activemodel (~> 4.0) mongo (>= 2.5.1, < 3.0.0) + origin (~> 2.3) + tzinfo (>= 0.3.37) mongoid-compatibility (0.5.1) activesupport mongoid (>= 2.0) + mongoid-enum (0.4.0) + mongoid (~> 5.0) mongoid-scroll (0.3.6) i18n mongoid (>= 3.0) @@ -150,6 +155,7 @@ GEM nokogiri (1.10.0) mini_portile2 (~> 2.4.0) oj (3.6.11) + origin (2.3.1) parallel (1.12.1) parser (2.5.3.0) ast (~> 2.4.0) @@ -287,6 +293,7 @@ DEPENDENCIES httparty hyperclient mongoid + mongoid-enum mongoid-scroll mongoid-shell newrelic_rpm diff --git a/slack-moji/api/endpoints/slack_endpoint.rb b/slack-moji/api/endpoints/slack_endpoint.rb index bd93b53..cc75290 100644 --- a/slack-moji/api/endpoints/slack_endpoint.rb +++ b/slack-moji/api/endpoints/slack_endpoint.rb @@ -70,8 +70,8 @@ class SlackEndpoint < Grape::API case callback_id when 'emoji-count' - emoji_count = payload['actions'].first['value'].to_i - user.update_attributes!(emoji_count: emoji_count, emoji: emoji_count > 0) + new_status = payload['actions'].first['value'] + user.update_attributes!(status: new_status) user.emoji! user.to_slack_emoji_question("Got it, #{user.emoji_text.downcase}.") else diff --git a/slack-moji/models/user.rb b/slack-moji/models/user.rb index 5bdbf8a..35828fd 100644 --- a/slack-moji/models/user.rb +++ b/slack-moji/models/user.rb @@ -1,14 +1,14 @@ class User include Mongoid::Document include Mongoid::Timestamps + include Mongoid::Enum field :user_id, type: String field :user_name, type: String field :access_token, type: String - field :emoji, type: Boolean, default: false - field :emoji_count, type: Integer, default: 0 field :is_bot, type: Boolean, default: false + enum :status, %i[unsubscribed emoji] # , :github] belongs_to :team, index: true validates_presence_of :team @@ -16,7 +16,7 @@ class User index({ user_id: 1, team_id: 1 }, unique: true) index(user_name: 1, team_id: 1) - scope :with_emoji, -> { where(emoji: true, :access_token.ne => nil) } + scope :with_emoji, -> { where(status_type: 'emoji', :access_token.ne => nil) } def slack_mention "<@#{user_id}>" @@ -67,14 +67,19 @@ def to_s "user_id=#{user_id}, user_name=#{user_name}" end - def emoji_count? - !emoji_count.nil? || emoji_count == 0 + def not_updating_status? + status == :unsubscribed + end + + def using_emoji_status? + status == :emoji end def emoji_text - case emoji_count - when 0 then 'No Emoji' - else "#{emoji_count} Emoji" + if using_emoji_status? + 'No Emoji' + else + 'Emoji' end end @@ -100,7 +105,7 @@ def to_slack_auth_request } end - def to_slack_emoji_question(text = 'How much emoji would you like?') + def to_slack_emoji_question(text = 'What type of updates would you like?') { text: text, attachments: [ @@ -110,18 +115,18 @@ def to_slack_emoji_question(text = 'How much emoji would you like?') callback_id: 'emoji-count', actions: [ { - name: 'emoji-count', + name: 'status', text: 'No Emoji', type: 'button', - value: 0, - style: emoji_count == 0 ? 'primary' : 'default' + value: 'unsubscribed', + style: not_updating_status? ? 'primary' : 'default' }, { - name: 'emoji-count', + name: 'status', text: 'Yes Emoji', - type: 'button', + type: 'emoji', value: 1, - style: emoji_count == 1 ? 'primary' : 'default' + style: using_emoji_status? ? 'primary' : 'default' } ] } @@ -137,7 +142,7 @@ def authorize!(code) redirect_uri: moji_authorize_uri ) - update_attributes!(access_token: rc['access_token'], emoji_count: 1, emoji: true) + update_attributes!(access_token: rc['access_token'], status: :emoji) dm!(text: "May the moji be with you!\nTo configure try `/moji me`.") @@ -149,14 +154,14 @@ def slack_client end def emoji! - if emoji_count && emoji_count > 0 + if using_emoji_status? emoji = EmojiData.all[rand(EmojiData.all.count)] logger.info "Emoji :#{emoji.short_name}: #{self}." slack_client.users_profile_set(profile: { status_text: Faker::GreekPhilosophers.quote, status_emoji: ":#{emoji.short_name}:" }.to_json) - elsif emoji_count == 0 + elsif not_updating_status? logger.info "Removing emoji #{self}." slack_client.users_profile_set(profile: { status_text: nil, diff --git a/spec/api/endpoints/slack_endpoint_spec.rb b/spec/api/endpoints/slack_endpoint_spec.rb index 25fe771..5c675ab 100644 --- a/spec/api/endpoints/slack_endpoint_spec.rb +++ b/spec/api/endpoints/slack_endpoint_spec.rb @@ -80,7 +80,7 @@ callback_id: 'emoji-count' }.to_json expect(last_response.status).to eq 201 - expect(user.reload.emoji_count).to eq 0 + expect(user.reload.using_emoji_status?).to eq false expect(last_response.body).to eq( user.to_slack_emoji_question('Got it, no emoji.').to_json ) @@ -96,7 +96,7 @@ callback_id: 'emoji-count' }.to_json expect(last_response.status).to eq 201 - expect(user.reload.emoji_count).to eq 1 + expect(user.reload.using_emoji_status?).to eq true expect(last_response.body).to eq( user.to_slack_emoji_question('Got it, 1 emoji.').to_json ) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 77ee148..afe300e 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -70,9 +70,9 @@ end context '#emoji!' do let!(:user) { Fabricate(:user) } - context 'emoji_count is 1' do + context 'using_emoji_status? is true' do before do - user.update_attributes!(emoji_count: 1) + user.update_attributes!(status: :emoji) end it 'sets user emoji' do expect(user.slack_client).to receive(:users_profile_set) do |arg| @@ -84,14 +84,15 @@ user.emoji! end end - context 'emoji_count is 0' do + context 'using_emoji_status? is false' do before do - user.update_attributes!(emoji_count: 0) + user.update_attributes!(status: :unsubscribed) end it 'unsets user emoji' do expect(user.slack_client).to receive(:users_profile_set).with( profile: { - status_text: nil, status_emoji: nil + status_text: nil, + status_emoji: nil }.to_json ) user.emoji!