Class: Messenger::Web
- Inherits:
-
Object
- Object
- Messenger::Web
- Defined in:
- lib/messenger/web.rb
Class Method Summary collapse
-
.deliver(url, body, options = {}) ⇒ Object
URL format: example.com user:[email protected].
- .obfuscate(url) ⇒ Object
- .valid_url?(url) ⇒ Boolean
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, ={}) raise Messenger::URLError, "The URL provided is invalid" unless valid_url?(url) = .dup method = .delete(:method) || :post uri = Addressable::URI.parse(url) user = CGI.unescape(uri.user) if uri.user password = CGI.unescape(uri.password) if uri.password = .merge(:basic_auth => {:username => user, :password => password}) if user || password uri.userinfo = nil if user || password response = HTTParty.send(method, uri.to_s, .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
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 |