Class: ZaptIn::Client

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

Overview

Client proxy for Zapt.In or 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: "zapt.in/api/links"
[] - Your  from Zapt.In. See: http://zapt.in/pages/api
[key] - Your key from Zapt.In. See: http://zapt.in/pages/api


13
14
15
16
17
# File 'lib/zapt_in/client.rb', line 13

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

Instance Attribute Details

#keyObject

Returns the value of attribute key.



5
6
7
# File 'lib/zapt_in/client.rb', line 5

def key
  @key
end

#loginObject

Returns the value of attribute login.



5
6
7
# File 'lib/zapt_in/client.rb', line 5

def 
  @login
end

#uriObject

Returns the value of attribute uri.



5
6
7
# File 'lib/zapt_in/client.rb', line 5

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: “zapt.in/api/links”

login
key


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

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 = ZaptIn::Request.get("#{self.uri}/shorten", params)
    ZaptIn::Url.parse(response)
  rescue ZaptIn::Error => e
    ZaptIn::Url.new(:status_code => "ERROR", :errorMessage => e.message, :error_code => -1)
  end
end