Class: Use0MK::Interface

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username = nil, apikey = nil) ⇒ Interface

Initializes the Use0MK::Interface.

Parameters:

  • username (String) (defaults to: nil)

    0.mk username

  • apikey (String) (defaults to: nil)

    0.mk API key



228
229
230
231
# File 'lib/use0mk.rb', line 228

def initialize(username = nil, apikey = nil)
  @username = username
  @apikey = apikey
end

Instance Attribute Details

#apikeyString?

the API key for 0.mk

Returns:

  • (String, nil)


222
223
224
# File 'lib/use0mk.rb', line 222

def apikey
  @apikey
end

#usernameString?

the username associated with 0.mk

Returns:

  • (String, nil)


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.

Parameters:

  • delete_uri (String)

    the 0.mk URI to delete

  • delete_code (String)

    the 0.mk delete code associated with the 0.mk URI

Returns:

  • (true, false)

    wheter the delete succeeded



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

  • Error

It is a wise idea to watch for any of these.

Parameters:

  • link (Hash) (defaults to: {:short_name => nil, :uri => nil})

    provide :short_name to inspect only the short name of an URI, or :uri to inspect a whole http://0.mk/short_name URI

Returns:



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

  • Error

It is a wise thing to watch for any of those exceptions.

Parameters:

  • uri (String)

    the long URI to shorten

  • short_name (String) (defaults to: nil)

    a custom short name to use when shortening the long URI (http://0.mk/short_name)

Returns:



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