Class: Base4R::BaseClient

Inherits:
Object
  • Object
show all
Includes:
HTTPLogger
Defined in:
lib/base_client.rb

Overview

BaseClient handles all communication with the Base API using HTTP

Constant Summary collapse

ITEMS_PATH =
'/base/feeds/items/'
SNIPPETS_PATH =
'/base/feeds/snippets/'
BASE_HOST =
'base.google.com'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HTTPLogger

included, #log, #log_request, #log_response, #verbose?

Constructor Details

#initialize(username, password, api_key, public_feed = true, dry_run = false) ⇒ BaseClient

Construct a BaseClient, which will make API requiest for the Base account belonging to username, authenticating with password and using api_key. Requests will be made against the public feed if public_feed is true, which is the default. The BaseClient can be used for a number of Base API requests.



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/base_client.rb', line 54

def initialize(username, password, api_key, public_feed=true, dry_run=false)

  @auth_key = ClientLogin.new.authenticate(username, password)
  @api_key = api_key

  if public_feed then
    @feed_path = SNIPPETS_PATH
  else
    @feed_path = ITEMS_PATH
  end
  @dry_run = dry_run
end

Instance Attribute Details

#auth_keyObject (readonly)

:nodoc:



45
46
47
# File 'lib/base_client.rb', line 45

def auth_key
  @auth_key
end

#dry_runObject

Returns the value of attribute dry_run.



47
48
49
# File 'lib/base_client.rb', line 47

def dry_run
  @dry_run
end

#feed_pathObject (readonly)

:nodoc:



46
47
48
# File 'lib/base_client.rb', line 46

def feed_path
  @feed_path
end

Instance Method Details

#create_item(item) ⇒ Object

Creates the supplied item as a new Base Item. Throws an Exception if there is a problem creating item.

Raises:



70
71
72
73
74
75
# File 'lib/base_client.rb', line 70

def create_item(item)
  resp = do_request(item.to_xml.to_s, 'POST')
  raise ErrorResponse.new("Error creating base item: #{resp.body}", resp) unless resp.kind_of? Net::HTTPSuccess
  resp['location'] =~ /(\d+)$/
  item.base_id= $1
end

#delete_item(item) ⇒ Object

Delete the supplied Base item. Returns true on success. Throws an Exception if there is a problem deleting item

Raises:



90
91
92
93
94
95
96
97
# File 'lib/base_client.rb', line 90

def delete_item(item)
  base_id = item_base_id item
  raise BaseException.new("base_id is required") if base_id.nil?
  resp = do_request(nil, 'DELETE', :base_id => base_id)
  raise_response_error "Error deleting base item", resp
  raise BaseException.new("Error deleting base item:"+resp.body) unless resp.kind_of? Net::HTTPOK
  true
end

#get_item(base_id) ⇒ Object



99
100
101
# File 'lib/base_client.rb', line 99

def get_item(base_id)
  resp = do_request '', 'GET', :base_id => nil, :url => "http://www.google.com/base/feeds/items/#{base_id}"
end

#update_item(item) ⇒ Object

Update the supplied Base item. Returns true on success. Throws an Exception if there is a problem updating item.

Raises:



80
81
82
83
84
85
86
# File 'lib/base_client.rb', line 80

def update_item(item)
  base_id = item_base_id item
  raise BaseException.new("base_id is required") if base_id.nil?
  resp = do_request(item.to_xml.to_s, 'PUT', :base_id => base_id)
  raise_response_error "Error updating base item", resp
  true
end