Sha256: f91e6355b15e5bea019e2c44fa91cf0f3369ac0bded15bd0b7e9ee67238f3eeb

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

require 'net/https'
require 'json'

class Haller
  class HallApiError < StandardError; end
  ROOT_URI = 'https://hall.com'
  ENDPOINT = '/api/1/services/generic/'

  OPTION_DEFAULTS = {
    sender_title: 'Haller',
    sender_icon_url: nil}

  attr_accessor :room_key
  attr_accessor :options

  def initialize(room_key, options = {})
    @options = OPTION_DEFAULTS.merge(options)
    @room_key = room_key
  end

  def send_message(message)
    post(message_json(message))
  end

  protected

  def full_path
    ENDPOINT + room_key
  end

  def uri
    URI.parse(ROOT_URI)
  end

  def post(json)
    req = Net::HTTP::Post.new(full_path, {'Content-Type' => 'application/json'})
    req.body = json

    http  = Net::HTTP.new(uri.host, uri.port)
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    http.use_ssl = true

    begin
      response = http.request(req)
      unless ['200', '201'].include?(response.code)
        raise StandardError, "HTTP Status: #{response.code}"
      end
    rescue StandardError => e
      raise HallApiError, e.message
    end
  end

  def message_json(body)
    { title: options[:sender_title],
      message: body,
      picture: options[:sender_icon_url]}.to_json
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
capistrano-haller-0.0.4 lib/haller.rb