Class: Sheety::Api

Inherits:
Object
  • Object
show all
Includes:
Children
Defined in:
lib/sheety/api.rb

Constant Summary collapse

URI_LIST =
'https://spreadsheets.google.com/feeds/spreadsheets/private/full'
URI_AUTH =
'https://www.googleapis.com/oauth2/v3/token'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Children

#_get_i_val, #_passes_constraint, included

Instance Attribute Details

#max_read_retriesObject

Returns the value of attribute max_read_retries.



58
59
60
# File 'lib/sheety/api.rb', line 58

def max_read_retries
  @max_read_retries
end

Class Method Details

.instObject



29
30
31
32
33
34
# File 'lib/sheety/api.rb', line 29

def self.inst
  if @@instance.nil?
    @@instance = Sheety::Api.new
  end
  return @@instance
end

Instance Method Details

#auth(force = false) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# 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) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
# 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) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# 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

for compatibility with Sheety::Children



23
24
25
# File 'lib/sheety/api.rb', line 23

def link(key) # for compatibility with Sheety::Children
  return key
end

#post_feed(uri, data) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# 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) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
# 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) ⇒ Object Also known as: set_max_read_retries



60
61
62
63
64
65
66
67
68
69
# 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

#with_max_retries(num, &block) ⇒ Object

block so you can have the max retries set only for a small amount of code

Raises:

  • (ArgumentError.new)


49
50
51
52
53
54
55
56
# 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