Class: Messenger::Web

Inherits:
Object
  • Object
show all
Defined in:
lib/messenger/web.rb

Class Method Summary collapse

Class Method Details

.deliver(url, body, options = {}) ⇒ Object

URL format:

http://example.com
https://user:[email protected]

The body of the message is posted as the body of the request, not the query.



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/messenger/web.rb', line 18

def self.deliver(url, body, options={})
  raise Messenger::URLError, "The URL provided is invalid" unless valid_url?(url)
  options      = options.dup
  method       = options.delete(:method) || :post
  uri          = Addressable::URI.parse(url)
  user         = CGI.unescape(uri.user) if uri.user
  password     = CGI.unescape(uri.password) if uri.password
  options      = options.merge(:basic_auth => {:username => user, :password => password}) if user || password
  uri.userinfo = nil if user || password
  response     = HTTParty.send(method, uri.to_s, options.merge(:body => body))
  Messenger::Result.new(success?(response), response)
end

.obfuscate(url) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/messenger/web.rb', line 31

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

.valid_url?(url) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
# File 'lib/messenger/web.rb', line 7

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