Sha256: ad71162a44b7eb1d0bd5882cc0b6f302abf5513b96d1d6953f7894d5da14f7c7

Contents?: true

Size: 916 Bytes

Versions: 3

Compression:

Stored size: 916 Bytes

Contents

require 'httparty'

class Messenger::Web

  def self.valid_url?(url)
    !!URI.parse(url)
  rescue URI::InvalidURIError
    false
  end

  # URL format:
  #     http://example.com
  #     https://user:pass@example.com
  #
  # The body of the message is posted as the body of the request, not the query.
  def self.deliver(url, body, options={})
    raise Messenger::URLError, "The URL provided is invalid" unless valid_url?(url)
    response = HTTParty.post(url, options.merge(:body => body))
    Messenger::Result.new(success?(response), response)
  end

  def self.obfuscate(url)
    raise Messenger::URLError, "The URL provided is invalid" unless valid_url?(url)
    path = URI.parse(url)
    if path.password
      url.sub(/#{path.password}/, 'xxxx')
    else
      url
    end
  end


private

  def self.success?(response)
    case response.code
    when 200, 201: true
    else
      false
    end
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
messenger-0.3.4 lib/messenger/web.rb
messenger-0.3.3 lib/messenger/web.rb
messenger-0.3.1 lib/messenger/web.rb