Class: SlackHookWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Worker
Defined in:
app/workers/slack_hook_worker.rb

Instance Method Summary collapse

Instance Method Details

#attach_project_formaterObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/workers/slack_hook_worker.rb', line 109

def attach_project_formater
  {
    fallback: 'A project wants to join a challenge!',
    color: '#36a64f',
    # pretext: "Optional text that appears above the attachment block",
    author_name: @obj.title,
    author_link: Rails.configuration.frontend_url + '/project/' + @obj.id.to_s,
    # author_icon: "http://flickr.com/icons/bobby.jpg",
    title: 'Creator: ' + @user.first_name + ' ' + @user.last_name,
    title_link: Rails.configuration.frontend_url + '/user/' + @user.id.to_s,
    text: @obj.short_description[0...100] + '[...]',
    fields: [
      {
        title: 'Skills needed',
        value: skills,
        short: true
      }
    ],
    image_url: 'https://app.jogl.io',
    thumb_url: 'https://app.jogl.io',
    footer: 'Just One Giant Lab',
    footer_icon: 'https://app.jogl.io/favicon.ico'
  }
end

#join_user_formaterObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/workers/slack_hook_worker.rb', line 66

def join_user_formater
  {
    fallback: 'Someone joined an object!',
    color: '#36a64f',
    # pretext: "Optional text that appears above the attachment block",
    author_name: 'From:' + @obj2.title,
    author_link: Rails.configuration.frontend_url + '/' + @obj2.class.name.downcase + '/' + @obj.id.to_s,
    # author_icon: "http://flickr.com/icons/bobby.jpg",
    title: @obj.first_name + ' ' + @obj.last_name,
    title_link: Rails.configuration.frontend_url + '/user/' + @obj.id.to_s,
    text: @obj.short_bio.nil? ? 'No bio... :(' : @obj.short_bio[0...100] + '[...]',
    fields: [
      {
        title: 'Skills',
        value: skills,
        short: true
      }
    ],
    image_url: 'https://app.jogl.io',
    thumb_url: 'https://app.jogl.io',
    footer: 'Just One Giant Lab',
    footer_icon: 'https://app.jogl.io/favicon.ico'
  }
end

#new_need_formaterObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/workers/slack_hook_worker.rb', line 41

def new_need_formater
  {
    fallback: 'A new need has been created!',
    color: '#36a64f',
    # pretext: "Optional text that appears above the attachment block",
    author_name: 'From:' + @obj.project.title,
    author_link: Rails.configuration.frontend_url + '/project/' + @obj.project.id.to_s,
    # author_icon: "http://flickr.com/icons/bobby.jpg",
    title: @obj.title,
    title_link: Rails.configuration.frontend_url + '/project/' + @obj.project.id.to_s + '/needs/' + @obj.id.to_s,
    text: @obj.content[0...100] + '[...]',
    fields: [
      {
        title: 'Skills needed',
        value: skills,
        short: true
      }
    ],
    image_url: 'https://app.jogl.io',
    thumb_url: 'https://app.jogl.io',
    footer: 'Just One Giant Lab',
    footer_icon: 'https://app.jogl.io/favicon.ico'
  }
end

#new_post_formaterObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/workers/slack_hook_worker.rb', line 91

def new_post_formater
  {
    fallback: 'Someone posted something new!',
    color: '#36a64f',
    # pretext: "Optional text that appears above the attachment block",
    author_name: 'Go read it!',
    author_link: Rails.configuration.frontend_url + '/post/' + @obj.id.to_s,
    # author_icon: "http://flickr.com/icons/bobby.jpg",
    title: 'From: ' + @user.first_name + ' ' + @user.last_name,
    title_link: Rails.configuration.frontend_url + '/user/' + @user.id.to_s,
    text: @obj.content[0...100] + '[...]',
    image_url: 'https://app.jogl.io',
    thumb_url: 'https://app.jogl.io',
    footer: 'Just One Giant Lab',
    footer_icon: 'https://app.jogl.io/favicon.ico'
  }
end

#perform(slackhook_id, action, object_type, object_id, secondary_object_type = nil, secondary_object_id = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/workers/slack_hook_worker.rb', line 6

def perform(slackhook_id, action, object_type, object_id, secondary_object_type = nil, secondary_object_id = nil)
  @slackhook = Slackhook.find(slackhook_id)

  @obj = object_type.constantize.find(object_id)
  @obj2 = secondary_object_type.constantize.find(secondary_object_id) unless secondary_object_type.nil?

  if object_type == 'Need'
    if action == 'new'
      attachment = new_need_formater
      message = 'A new need has been created !'
    end
  elsif object_type == 'Post'
    @user = User.find(@obj.user_id)
    if action == 'new'
      attachment = new_post_formater
      message = @user.first_name + ' ' + @user.last_name + ' just posted something!'
    end
  elsif object_type == 'Project'
    @user = User.find(@obj.creator_id)
    if action == 'attach'
      attachment = attach_project_formater
      message = @obj.title + ' wants to join the challenge!'
    end
  elsif object_type == 'User'
    if action == 'join'
      attachment = join_user_formater
      message = @obj.first_name + ' ' + @obj.last_name + ' just joined ' + @obj2.title + '!'
    end
  end

  notifier = Slack::Notifier.new @slackhook.hook_url, channel: @slackhook.channel, username: @slackhook.username

  notifier.post text: message, attachments: [attachment]
end

#skillsObject



134
135
136
137
138
139
140
# File 'app/workers/slack_hook_worker.rb', line 134

def skills
  result = []
  @obj.skills.first(5).map do |skill|
    result << skill.skill_name
  end
  result.join(', ')
end