Sha256: f9b75634cf9d965c63c6399ab6afb0ca5a48729058add0f16cfea92337dba391

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

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)
    method = options.delete(:method) || :post
    options = options.merge(:basic_auth => {:username => URI.parse(url).user, :password => URI.parse(url).password}) if URI.parse(url).user && URI.parse(url).password
    response = HTTParty.send(method, 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

1 entries across 1 versions & 1 rubygems

Version Path
messenger-0.4.3 lib/messenger/web.rb