Sha256: 3fb08669a218b612890cf4637451716fd1cf91af4cba2f9a143f13f191f79ed7

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

require 'httparty'
require 'json'

class Messenger::Notifo

  def self.valid_url?(url)
    !!url.match(/^notifo:\/\/.+$/)
  end

  # URL format:
  #     notifo://username
  #
  # Options:
  #     :notifo_api_username => The service's API username
  #     :notifo_api_secret => The service's API secret
  #     :notifo_title => The notificaiton title
  #     :notifo_url => Open this URL
  def self.deliver(url, message, options={})
    raise Messenger::URLError, "The URL provided is invalid" unless valid_url?(url)
    username = matcher(url)
    response = HTTParty.post("https://#{options[:notifo_api_username]}:#{options[:notifo_api_secret]}@api.notifo.com/v1/send_notification",
                                      :body => { :to => username, :msg => message, :title => options[:notifo_title], :uri => options[:notifo_url] })
    Messenger::Result.new(success?(response), response)
  end

  def self.obfuscate(url)
    raise Messenger::URLError, "The URL provided is invalid" unless valid_url?(url)
    url
  end


private

  def self.matcher(url)
    url.match(/^notifo:\/\/(.+)/)[1]
  end

  def self.success?(response)
    response.code == 200
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
messenger-0.3.3 lib/messenger/notifo.rb
messenger-0.3.1 lib/messenger/notifo.rb