Class: Mush::Services::Owly

Inherits:
AuthenticatedService show all
Defined in:
lib/mush/services/owly.rb

Instance Attribute Summary

Attributes inherited from AuthenticatedService

#apikey, #login

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AuthenticatedService

#authorize, #initialize

Methods inherited from Mush::Service

#authorize, #get, #post

Constructor Details

This class inherits a constructor from Mush::AuthenticatedService

Class Method Details

.response_parseObject

Ow.ly api bug, response is in json, but response type is html so force parse



12
13
14
15
16
# File 'lib/mush/services/owly.rb', line 12

def self.response_parse
  Proc.new do |body, format|
    JSON.parse(body)
  end
end

Instance Method Details

#shorten(url) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mush/services/owly.rb', line 19

def shorten(url)
  invalid_uri_msg = "Please provide a valid URI, including http://"
  invalid_auth_msg = "Invalid Authorization Data, please provide apikey"
  
  raise InvalidURI.new invalid_uri_msg if url.empty?
  raise InvalidAuthorizationData.new(invalid_auth_msg) if apikey.empty?
  
  options = {:format => 'json'}
  options[:query] = {:apiKey => self.apikey, :longUrl => url}
  
  response = get('/shorten', options)

  response.code == 200 ? response["results"]["shortUrl"] : response["error"]
end