Class: Yourls::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/yourls/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, api_key, options = {}) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
# File 'lib/yourls/client.rb', line 7

def initialize(host, api_key, options = {})
  @host, @api_key = host, api_key
  @base_uri = File.join(host, 'yourls-api.php')
  @server_offset = options[:offset] || 0.seconds
end

Instance Method Details

#expand(short_url, options = {}) ⇒ Object



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

def expand(short_url, options = {})
  Yourls::Url.new(get('expand', options.merge!(:shorturl => short_url)).parsed_response)
end

#get(action, query = {}) ⇒ Object



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

def get(action, query = {})
  # Prepare the signature
  timestamp = (Time.now + @server_offset).to_i
  signature = Digest::MD5.hexdigest(timestamp.to_s + @api_key)

  query ||= {}
  query.merge!(:timestamp => timestamp, :signature => signature, :action => action, :format => 'json') 

  response = self.class.get(@base_uri, :query => query)

  if response.code == 200
    return response
  else
    raise YourlsError.new(response.message, response.code)
  end
end

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



22
23
24
# File 'lib/yourls/client.rb', line 22

def shorten(long_url, options = {})
  Yourls::Url.new(get('shorturl', options.merge!(:url => long_url)).parsed_response)
end

#stats(options = {}) ⇒ Object



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

def stats(options = {})
  response = get('stats', options)
  response.parsed_response
end