class GooglApi::Client

Public Class Methods

new(options = {}) click to toggle source
# File lib/googl-api/client.rb, line 17
def initialize(options = {})
  # set api key if provided
  @api_key = { :key => options[:api_key] } if options.has_key?(:api_key)

  # set auth token for client login if provided
  if options[:email] && options[:password] && @token = ClientLogin.authenticate(options[:email], options[:password])
    self.class.headers.merge!("Authorization" => "GoogleLogin auth=#{@token}")
  end
end

Public Instance Methods

analytics(url, projection = "FULL") click to toggle source
# File lib/googl-api/client.rb, line 37
def analytics(url, projection = "FULL")
  raise ArgumentError.new("A URL to check analytics on is required") if url.blank?
  load_respose(self.class.get('/url', :query => @api_key.merge({ :shortUrl => url, :projection => projection })))
end
expand(url) click to toggle source
# File lib/googl-api/client.rb, line 32
def expand(url)
  raise ArgumentError.new("A URL to expand is required") if url.blank?
  load_respose(self.class.get('/url', :query => @api_key.merge({ :shortUrl => url })))
end
history() click to toggle source
# File lib/googl-api/client.rb, line 42
def history
  raise ArgumentError.new("Doing a history search requires a Client Login (or eventually OAuth to be set)") unless @token
  self.class.get('/url/history')
end
shorten(url) click to toggle source
# File lib/googl-api/client.rb, line 27
def shorten(url)
  raise ArgumentError.new("A URL to shorten is required") if url.blank?
  load_respose(self.class.post('/url', :query => @api_key, :body => "{ \"longUrl\": \"#{url}\" }"))
end

Private Instance Methods

load_respose(resp) click to toggle source
# File lib/googl-api/client.rb, line 47
def load_respose(resp)
  raise GooglError.new(resp.message, resp.code) if resp.code != 200
  GooglApi::Response.new(resp.parsed_response)
end