class Slacking::HttpClient

Public Class Methods

new(token, organization) click to toggle source
# File lib/slacking/http_client.rb, line 12
def initialize(token, organization)
  @token = token
  @organization = organization
end

Public Instance Methods

post_to_slack(profile) click to toggle source
# File lib/slacking/http_client.rb, line 17
def post_to_slack(profile)
  @profile = profile
  url = URI.parse(slack_url)

  request = Net::HTTP::Post.new("#{url.path}?#{url.query}")
  request.body = request_body
  request.add_field 'Content-Type', 'application/json'

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true

  handle_response http.request(request)
end

Protected Instance Methods

request_body() click to toggle source
# File lib/slacking/http_client.rb, line 33
def request_body
  {
    text: message,
    channel: "##{@profile[:channel]}".sub('##', '#'),
    icon_url: @profile[:icon_url],
    username: @profile[:username],
  }.to_json
end

Private Instance Methods

handle_response(response) click to toggle source
# File lib/slacking/http_client.rb, line 49
def handle_response(response)
  if response.body == 'No hooks'
    config = get_slack_config('w+')
    @token = config[:token]
    @organization = config[:organization]
  else
    puts response.body
  end
end
message() click to toggle source
# File lib/slacking/http_client.rb, line 44
def message
  puts "What would you like to say to #{@profile[:channel]}, #{@profile[:username]}?"
  get_action
end
slack_url() click to toggle source
# File lib/slacking/http_client.rb, line 59
def slack_url
  "https://#{@organization}.slack.com/services/hooks/incoming-webhook?token=#{@token}"
end