Class: Use0MK::Interface
- Inherits:
-
Object
- Object
- Use0MK::Interface
- Defined in:
- lib/use0mk.rb
Overview
This class represents the main interface through which one can operate on the 0.mk API.
Instance Attribute Summary collapse
-
#apikey ⇒ String?
the API key for 0.mk.
-
#username ⇒ String?
the username associated with 0.mk.
Class Method Summary collapse
-
.delete(delete_uri, delete_code) ⇒ true, false
Deletes a shortened URI from 0.mk.
Instance Method Summary collapse
-
#initialize(username = nil, apikey = nil) ⇒ Interface
constructor
Initializes the Interface.
-
#preview(link = {:short_name => nil, :uri => nil}) ⇒ Use0MK::ShortenedURI
Inspects an already shortened URI.
-
#shorten(uri, short_name = nil) ⇒ Use0MK::ShortenedURI
Shortens a URI.
Constructor Details
#initialize(username = nil, apikey = nil) ⇒ Interface
Initializes the Use0MK::Interface.
228 229 230 231 |
# File 'lib/use0mk.rb', line 228 def initialize(username = nil, apikey = nil) @username = username @apikey = apikey end |
Instance Attribute Details
#apikey ⇒ String?
the API key for 0.mk
222 223 224 |
# File 'lib/use0mk.rb', line 222 def apikey @apikey end |
#username ⇒ String?
the username associated with 0.mk
219 220 221 |
# File 'lib/use0mk.rb', line 219 def username @username end |
Class Method Details
.delete(delete_uri, delete_code) ⇒ true, false
Deletes a shortened URI from 0.mk. This method does not raise or handles any exceptions. It also does not handle redirection.
240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/use0mk.rb', line 240 def self.delete(delete_uri, delete_code) delete_uri = URI.parse(delete_uri) response = Net::HTTP.post_form(delete_uri, {'brisiKod' => delete_code}) case response when Net::HTTPSuccess return true # phew, that was hard work, that delete end return false end |
Instance Method Details
#preview(link = {:short_name => nil, :uri => nil}) ⇒ Use0MK::ShortenedURI
Inspects an already shortened URI. This method can raise several exceptions of which most importantly:
-
URI::Error
-
Net::HTTPException
It is a wise idea to watch for any of these.
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/use0mk.rb', line 281 def preview(link = {:short_name => nil, :uri => nil}) uri = nil if !link[:short_name].nil? and !link[:short_name].empty? uri = "http://0.mk/#{link[:short_name]}" elsif !link[:uri].nil? and !link[:uri].empty? uri = link[:uri] if (link[:uri] =~ /http:\/\/0\.mk\/.*/) end if uri.nil? raise ArgumentError, ':uri should be a valid http://0.mk shortened URI.' end _preview(uri) end |
#shorten(uri, short_name = nil) ⇒ Use0MK::ShortenedURI
Shortens a URI. This method can raise several exceptions of which most importantly:
-
URI::Error
-
Net::HTTPException
It is a wise thing to watch for any of those exceptions.
265 266 267 268 269 |
# File 'lib/use0mk.rb', line 265 def shorten(uri, short_name = nil) uri = URI.parse(uri) _shorten(uri, short_name) end |