Class: Guillotine::Service
- Inherits:
-
Object
- Object
- Guillotine::Service
- Defined in:
- lib/guillotine/service.rb
Defined Under Namespace
Classes: Options
Constant Summary collapse
- NullChecker =
Deprecated until v2
Guillotine::HostChecker
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#check_host(url) ⇒ Object
Checks to see if the input URL is using a valid host.
-
#create(url, code = nil) ⇒ Object
Public: Maps a URL to a shortened code.
-
#default_url ⇒ Object
Public.
-
#ensure_url(str) ⇒ Object
Ensures that the argument is an Addressable::URI.
-
#get(code) ⇒ Object
Public: Gets the full URL for a shortened code.
-
#initialize(db, value = nil) ⇒ Service
constructor
This is the public API to the Guillotine service.
-
#parse_url(url) ⇒ Object
Internal.
Constructor Details
#initialize(db, value = nil) ⇒ Service
This is the public API to the Guillotine service. Wire this up to Sinatra or whatever. Every public method should return a compatible Rack Response: [Integer Status, Hash headers, String body].
db - A Guillotine::Adapter instance. required_host - Either a String or Regex limiting which domains the
shortened URLs can come from.
54 55 56 57 |
# File 'lib/guillotine/service.rb', line 54 def initialize(db, value = nil) @db = db @options = Options.from(value) end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
44 45 46 |
# File 'lib/guillotine/service.rb', line 44 def db @db end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
44 45 46 |
# File 'lib/guillotine/service.rb', line 44 def @options end |
Instance Method Details
#check_host(url) ⇒ Object
Checks to see if the input URL is using a valid host. You can constrain the hosts with the ‘required_host` argument of the Service constructor.
url - An Addressible::URI instance to check.
Returns a 422 Rack::Response if the host is invalid, or nil.
103 104 105 106 107 108 109 |
# File 'lib/guillotine/service.rb', line 103 def check_host(url) if url.scheme !~ /^https?$/ [422, {}, "Invalid url: #{url}"] else @options.host_checker.call url end end |
#create(url, code = nil) ⇒ Object
Public: Maps a URL to a shortened code.
url - A String or Addressable::URI URL to shorten. code - Optional String code to use. Defaults to a random String.
Returns 201 with the Location pointing to the code, or 422.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/guillotine/service.rb', line 79 def create(url, code = nil) url = ensure_url(url) if resp = check_host(url) return resp end begin if code = @db.add(url.to_s, code, @options) [201, {"Location" => code}, url] else [422, {}, "Unable to shorten #{url}"] end rescue DuplicateCodeError => err [422, {}, err.to_s] end end |
#default_url ⇒ Object
Public
130 131 132 |
# File 'lib/guillotine/service.rb', line 130 def default_url @options.default_url end |
#ensure_url(str) ⇒ Object
Ensures that the argument is an Addressable::URI.
str - A String URL or an Addressable::URI.
Returns an Addressable::URI.
116 117 118 119 120 121 122 |
# File 'lib/guillotine/service.rb', line 116 def ensure_url(str) if str.respond_to?(:scheme) str else parse_url(str.to_s) end end |
#get(code) ⇒ Object
Public: Gets the full URL for a shortened code.
code - A String short code.
Returns 302 with the Location header pointing to the URL on a hit, or 404 on a miss.
65 66 67 68 69 70 71 |
# File 'lib/guillotine/service.rb', line 65 def get(code) if url = @db.find(code) [302, {"Location" => parse_url(url).to_s}] else [404, {}, "No url found for #{code}"] end end |
#parse_url(url) ⇒ Object
Internal
125 126 127 |
# File 'lib/guillotine/service.rb', line 125 def parse_url(url) @db.parse_url(url, @options) end |