Class: Supr::Client

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/supr/client.rb

Instance Method Summary collapse

Methods included from Utils

#attr_define, #create_hash_from_url, #create_url, #get_result, #instance_variablise, #underscore

Constructor Details

#initialize(login, api_key) ⇒ Client

Returns a new instance of Client.



17
18
19
20
# File 'lib/supr/client.rb', line 17

def initialize(,api_key)
  @login = 
  @api_key = api_key
end

Instance Method Details

#expand(input) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/supr/client.rb', line 50

def expand(input)
  if input.is_a? String
    if input.include?('su.pr/') || input.include?('j.mp/')
      hash = create_hash_from_url(input)
      request = create_url "expand", :hash => hash
      result = get_result(request)
      result = { :short_url => input, :hash => hash }.merge result[hash]
    else
      request = create_url "expand", :hash => input
      result = get_result(request)
      result = { :hash => input, :short_url => "http://su.pr/#{input}" }.merge result[input]
    end
    Supr::Url.new(@login,@api_key,result)        
  elsif input.is_a? Array
    request = create_url "expand", :hash => input.join(',')
    result = get_result(request)
    input.map do |hsh|
      new_url = {:hash => hsh, :short_url => "http://su.pr/#{hsh}"}.merge result[hsh]
      hsh = Supr::Url.new(@login,@api_key,new_url)
    end
  else
    raise ArgumentError('Expand requires either a short url, a hash or an array of hashes')
  end
end

#shorten(input, opts = {}) ⇒ Object



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

def shorten(input, opts={})
  if input.is_a? String
    request = create_url("shorten", :longUrl => input, :history => (opts[:history] ? 1 : nil))
    result = get_result(request)
    result = {:long_url => input}.merge result[input]
    Supr::Url.new(@login,@api_key,result)
  elsif input.is_a? Array
    request = create_url("shorten", :history => (opts[:history] ? 1 : nil))
    request.query << "&" + input.map { |long_url| "longUrl=#{CGI.escape(long_url)}" }.join("&") unless input.nil?
    result = get_result(request)
    input.map do |long_url|
      new_url = {:long_url => long_url}.merge result[long_url]
      long_url = Supr::Url.new(@login,@api_key,new_url)
    end
  else
    raise ArgumentError.new("Shorten requires either a url or an array of urls")
  end
end

#validateObject Also known as: valid?



41
42
43
44
45
46
47
# File 'lib/supr/client.rb', line 41

def validate
  @validated ||= begin
    self.shorten("http://google.com").kind_of?(::Supr::Url)
  rescue
    false
  end
end