Sha256: a8cc103b358c1b56d069269a6fcc9afb76c6ca18adcf742da0c64ecd4b188f28

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

require 'multi_json'
require 'excon'

module Minitel
  class Client
    attr_accessor :connection

    def initialize(telex_url)
      unless telex_url.start_with? "https://"
        raise ArgumentError, "Bad Url"
      end
      self.connection = Excon.new(telex_url,
        :headers => {
          "User-Agent" => "minitel/#{Minitel::VERSION} excon/#{Excon::VERSION}"
        }
      )
    end

    def notify_app(args)
      StrictArgs.enforce(args, [:app_uuid, :body, :title], :app_uuid)
      post_message('app', args[:app_uuid], args[:title], args[:body])
    end

    def notify_user(args)
      StrictArgs.enforce(args, [:user_uuid, :body, :title], :user_uuid)
      post_message('user', args[:user_uuid], args[:title], args[:body])
    end

    private

    def post_message(type, id, title, body)
      message = {
        title: title,
        body: body,
        target: {type: type, id: id}
      }

      response = connection.post(
                   path: "/producer/messages",
                   body: MultiJson.dump(message),
                   expects: 201)

      MultiJson.load(response.body)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
minitel-0.2.0 lib/minitel/client.rb