class Sheety::Api
Constants
- URI_AUTH
- URI_LIST
Attributes
max_read_retries[RW]
Public Class Methods
inst()
click to toggle source
# File lib/sheety/api.rb, line 29 def self.inst if @@instance.nil? @@instance = Sheety::Api.new end return @@instance end
new()
click to toggle source
# File lib/sheety/api.rb, line 160 def initialize @client = Google::APIClient.new(:application_name => 'Sheety', :application_version => "0.1.0") @client.authorization= :google_app_default @auth = @client.authorization @auth.scope = 'https://spreadsheets.google.com/feeds' @max_retries = 0 @max_read_retries = 0 return self end
Public Instance Methods
auth(force=false)
click to toggle source
# File lib/sheety/api.rb, line 36 def auth(force=false) if @access_token.blank? || force data = { :grant_type => 'urn:ietf:params:oauth:grant-type:jwt-bearer', :assertion => @auth.to_jwt.to_s } resp = HTTParty.post(URI_AUTH, { :body => data }) if Net::HTTPOK === resp.response @access_token = resp['access_token'] end end return (@access_token ? self : nil) end
delete_feed(uri)
click to toggle source
# File lib/sheety/api.rb, line 114 def delete_feed(uri) tries = 0 begin return parse_response(HTTParty.delete(uri, headers: delete_headers)) rescue if tries < @max_retries tries += 1 retry end return nil end end
get_feed(uri)
click to toggle source
# File lib/sheety/api.rb, line 72 def get_feed(uri) tries = 0 begin return parse_response(HTTParty.get(uri, headers: get_headers)) rescue if tries < [@max_retries, @max_read_retries].max tries += 1 retry end return nil end end
link(key)
click to toggle source
# File lib/sheety/api.rb, line 23 def link(key) # for compatibility with Sheety::Children return key end
post_feed(uri, data)
click to toggle source
# File lib/sheety/api.rb, line 86 def post_feed(uri, data) tries = 0 begin return parse_response(HTTParty.post(uri, body: data, headers: post_headers)) rescue if tries < @max_retries tries += 1 retry end return nil end end
put_feed(uri, data)
click to toggle source
# File lib/sheety/api.rb, line 100 def put_feed(uri, data) tries = 0 begin return parse_response(HTTParty.put(uri, body: data, headers: put_headers)) rescue if tries < @max_retries tries += 1 retry end return nil end end
with_max_read_retries(num) { || ... }
click to toggle source
# File lib/sheety/api.rb, line 60 def with_max_read_retries(num) if block_given? previous_max = @max_read_retries @max_read_retries = num.to_i yield @max_read_retries = previous_max else @max_read_retries = num.to_i end end
Also aliased as: set_max_read_retries
with_max_retries(num) { || ... }
click to toggle source
block so you can have the max retries set only for a small amount of code
# File lib/sheety/api.rb, line 49 def with_max_retries(num, &block) raise ArgumentError.new, "Must pass a block!" unless block_given? previous_max = @max_retries @max_retries = num.to_i yield @max_retries = previous_max end
Private Instance Methods
auth_headers()
click to toggle source
# File lib/sheety/api.rb, line 154 def auth_headers return { 'Authorization' => "Bearer #{@access_token}" } end
delete_headers()
click to toggle source
# File lib/sheety/api.rb, line 150 def delete_headers return auth_headers end
get_headers()
click to toggle source
# File lib/sheety/api.rb, line 138 def get_headers return auth_headers end
parse_response(resp)
click to toggle source
# File lib/sheety/api.rb, line 130 def parse_response(resp) begin return XmlSimple.xml_in(resp.body, { 'KeyAttr' => 'name', 'keepnamespace' => true }) rescue return resp end end
post_headers()
click to toggle source
# File lib/sheety/api.rb, line 142 def post_headers return put_headers end
put_headers()
click to toggle source
# File lib/sheety/api.rb, line 146 def put_headers return auth_headers.merge('Content-Type' => 'application/atom+xml') end