Skip to content

Instantly share code, notes, and snippets.

@pootsbook
Created October 12, 2013 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pootsbook/6953750 to your computer and use it in GitHub Desktop.
Save pootsbook/6953750 to your computer and use it in GitHub Desktop.
URL
class Url
attr_reader :url
def initialize(url)
raise(ArgumentError, "url not specified") unless url
@url = url
end
def to_uri
URI.parse(url)
end
end
url = Url.new(ARGV[0]) # will raise an ArgumentError if ARGV[0] is nil
@pootsbook
Copy link
Author

Notes:

  • L10 notice I'm using the attr_reader instead of the instance variable. Rationale: depend on behaviour not data (POODR)
  • L5 use an exception to deal with an argument that is required but not passed. Strictly speaking your URL class should have no knowledge of the outside world, i.e., it shouldn't know about command line arguments. Rationale: inject dependencies (POODR)
  • I wasn't sure what the following code was all about, I don't believe it should really be part of this class, but if so, it should pass errors out in a different way. Again a dependency thing, URL shouldn't know about $errors. If you give me a bit of background I can have a think about what might work better.
def to_uri(crit=false)
  $errors.catch(crit) do
    URI.parse(@url) # Requires a valid scheme (http:// , //, etc...)
  end 
end 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment