Created
April 14, 2015 23:09
-
-
Save warmwaffles/83a7f61c09dc19e4e92f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Routing | |
| include Rails.application.routes.url_helpers | |
| attr_reader :uri, :host, :port | |
| def initialize(args={}) | |
| if args[:url] | |
| parsed = URI.parse(args[:url]) | |
| configure({ | |
| host: parsed.host, | |
| port: parsed.port, | |
| ssl: !!(parsed.scheme =~ /https/) | |
| }) | |
| else | |
| configure(args) | |
| end | |
| end | |
| def ssl? | |
| !!@ssl | |
| end | |
| def configure(args={}) | |
| @ssl = args.fetch(:ssl, true) | |
| @port = args.fetch(:port, nil) | |
| @host = args.fetch(:host, nil) | |
| if @ssl | |
| @uri = URI::HTTPS.build({ | |
| host: @host, | |
| port: @port | |
| }) | |
| else | |
| @uri = URI::HTTP.build({ | |
| host: @host, | |
| port: @port | |
| }) | |
| end | |
| end | |
| def default_url_options | |
| { | |
| protocol: @ssl ? 'https' : 'http', | |
| host: @host, | |
| port: @port | |
| } | |
| end | |
| def host_with_port | |
| if @port | |
| '%s:%s' % [@host, @port] | |
| else | |
| @host | |
| end | |
| end | |
| def url_for_path(path) | |
| opts = default_url_options | |
| base = "#{opts[:protocol]}://#{host_with_port}" | |
| URI.join(base, path).to_s | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment