Class: Abrio::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/abrio/client.rb

Overview

Client proxy for Abr.io API.

Constant Summary collapse

DEFAULT_PARAMS =
{:format => "xml", :version => "1.0"}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Use the following options to initialize your client:

[uri] - Default: "abr.io/api/links"
[login] - Your login from Zapt.In. See: http://abr.io/pages/api
[key] - Your key from Zapt.In. See: http://abr.io/pages/api


12
13
14
15
16
# File 'lib/abrio/client.rb', line 12

def initialize(options = {})
  @uri = options[:uri] || "http://abr.io/api/links"
  @login = options[:login]
  @key = options[:key]
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



4
5
6
# File 'lib/abrio/client.rb', line 4

def key
  @key
end

#loginObject

Returns the value of attribute login.



4
5
6
# File 'lib/abrio/client.rb', line 4

def 
  @login
end

#uriObject

Returns the value of attribute uri.



4
5
6
# File 'lib/abrio/client.rb', line 4

def uri
  @uri
end

Instance Method Details

#shorten(long_url, options = {}) ⇒ Object

Shorten an url.

long_url

url you want to shorten.

options

Optional Hash. You can pass the following keys:

uri
  • Default: “abr.io/api/links”

login
key


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/abrio/client.rb', line 24

def shorten(long_url, options = {})
  return nil if long_url.nil? || long_url == ""

  @uri = options.delete(:uri) || @uri
  params = {:login => options.delete(:login) || @login,
            :key   => options.delete(:key) || @key}.merge(DEFAULT_PARAMS)
  params[:longUrl] = long_url

  begin
    response = Abrio::Request.get("#{self.uri}/shorten", params)
    Abrio::Url.parse(response)
  rescue Abrio::Error => e
    Abrio::Url.new(:status_code => "ERROR", :errorMessage => e.message, :error_code => -1)
  end
end