Class: Slack::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/slack/service.rb

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Service

Returns a new instance of Service.



5
6
7
8
# File 'lib/slack/service.rb', line 5

def initialize(params)
  @channel = params[:channel]
  @webhook = params[:webhook]
end

Instance Method Details

#build_blocks(blocks_object) ⇒ Object (private)



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/slack/service.rb', line 28

def build_blocks(blocks_object)
  blocks = []
  blocks_object.each do |block|
    case block[:block_type]
    when 'divider'
      blocks.push({ type: 'divider' })
    when 'section'
      blocks.push(build_section(block[:text], block[:text_type]))
    else
      continue
    end
  end
  blocks
end

#build_section(text, text_type) ⇒ Object (private)



43
44
45
46
47
48
49
50
51
# File 'lib/slack/service.rb', line 43

def build_section(text, text_type)
  {
    "type": 'section',
    "text": {
      "type": text_type,
      "text": text
    }
  }
end

#notify(header, blocks = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/slack/service.rb', line 10

def notify(header, blocks = nil)
  data = {
    text: header,
    blocks: blocks ? build_blocks(blocks) : nil,
    channel: @channel
  }

  uri = URI.parse(@webhook)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  request = Net::HTTP::Post.new(uri.path, { 'Content-Type' => 'application/json' })
  request.body = data.to_json
  http.request(request)
end