Class: Yourls::Client
Instance Method Summary collapse
- #expand(short_url, options = {}) ⇒ Object
- #get(action, query = {}) ⇒ Object
-
#initialize(host, api_key, options = {}) ⇒ Client
constructor
A new instance of Client.
- #shorten(long_url, options = {}) ⇒ Object
- #stats(options = {}) ⇒ Object
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, = {}) @host, @api_key = host, api_key @base_uri = File.join(host, 'yourls-api.php') @server_offset = [:offset] || 0.seconds end |
Instance Method Details
#expand(short_url, options = {}) ⇒ Object
18 19 20 |
# File 'lib/yourls/client.rb', line 18 def (short_url, = {}) Yourls::Url.new(get('expand', .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 = (Time.now + @server_offset).to_i signature = Digest::MD5.hexdigest(.to_s + @api_key) query ||= {} query.merge!(: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., response.code) end end |
#shorten(long_url, options = {}) ⇒ Object
22 23 24 |
# File 'lib/yourls/client.rb', line 22 def shorten(long_url, = {}) Yourls::Url.new(get('shorturl', .merge!(:url => long_url)).parsed_response) end |
#stats(options = {}) ⇒ Object
13 14 15 16 |
# File 'lib/yourls/client.rb', line 13 def stats( = {}) response = get('stats', ) response.parsed_response end |