sheety 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 38ade06e385c46a2d91faacf730d0448f6d4dd33
4
- data.tar.gz: 5dfc347ed4b5b4c757c6ed207f3fa1d61c70abd4
3
+ metadata.gz: 0bcb4c0f7b6b0dcf39e89cc9e816cb48ece43ae2
4
+ data.tar.gz: c3b3a6c2f573a421a2f87a24818bca447186373d
5
5
  SHA512:
6
- metadata.gz: ed078b4cf16624b2870f2d5d78ddea2ac3ea863641a3be6942db5434ce2d9b55598758a9da7383c75f10104b29286acd1c4ebf45a485351f2f00fc7055959219
7
- data.tar.gz: 9da2690ecc23258d77af3d2c179bca1bf5cf9dd8bfe35704482dc245d7061f62338b74ff1df0723386f4326c065e0fea88c7e8fe0bbd6d2bf66ad7a2c4a42f83
6
+ metadata.gz: ef9ccea18022a4e0ffa7f6d60941dbeb3d31898a1def8ccdf421879523ef984c3dd528668a7761fad8e25f5cfe0471f85a57c1301b8c75e463563e1faf26d87a
7
+ data.tar.gz: 36aebc88802a69d9d3d7edda08af38ee137ab77d957829a7212061b5edcc96dc61bf0fc1d1fd7aa405032982c1a101e9dc7f6869585305043d266a29c68f73f0
@@ -6,6 +6,12 @@ require_relative 'spreadsheet'
6
6
 
7
7
  module Sheety
8
8
 
9
+ class SheetyError < StandardError
10
+ end
11
+
12
+ class SheetyFetchError < SheetyError
13
+ end
14
+
9
15
  private
10
16
 
11
17
  class Api
@@ -14,7 +20,7 @@ module Sheety
14
20
  URI_LIST = 'https://spreadsheets.google.com/feeds/spreadsheets/private/full'
15
21
  URI_AUTH = 'https://www.googleapis.com/oauth2/v3/token'
16
22
 
17
- def link(key) # for compat with ChildBearing
23
+ def link(key) # for compatibility with Sheety::Children
18
24
  return key
19
25
  end
20
26
 
@@ -39,34 +45,82 @@ module Sheety
39
45
  return (@access_token ? self : nil)
40
46
  end
41
47
 
48
+ # block so you can have the max retries set only for a small amount of code
49
+ def with_max_retries(num, &block)
50
+ raise ArgumentError.new, "Must pass a block!" unless block_given?
51
+
52
+ previous_max = @max_retries
53
+ @max_retries = num.to_i
54
+ yield
55
+ @max_retries = previous_max
56
+ end
57
+
58
+ attr_accessor :max_read_retries
59
+
60
+ def with_max_read_retries(num)
61
+ if block_given?
62
+ previous_max = @max_read_retries
63
+ @max_read_retries = num.to_i
64
+ yield
65
+ @max_read_retries = previous_max
66
+ else
67
+ @max_read_retries = num.to_i
68
+ end
69
+ end
70
+ alias_method :set_max_read_retries, :with_max_read_retries
71
+
42
72
  def get_feed(uri)
73
+ tries = 0
43
74
  begin
44
75
  return parse_response(HTTParty.get(uri, headers: get_headers))
45
76
  rescue
77
+ if tries < [@max_retries, @max_read_retries].max
78
+ tries += 1
79
+ retry
80
+ end
81
+
46
82
  return nil
47
83
  end
48
84
  end
49
85
 
50
86
  def post_feed(uri, data)
87
+ tries = 0
51
88
  begin
52
89
  return parse_response(HTTParty.post(uri, body: data, headers: post_headers))
53
90
  rescue
91
+ if tries < @max_retries
92
+ tries += 1
93
+ retry
94
+ end
95
+
54
96
  return nil
55
97
  end
56
98
  end
57
99
 
58
100
  def put_feed(uri, data)
101
+ tries = 0
59
102
  begin
60
103
  return parse_response(HTTParty.put(uri, body: data, headers: put_headers))
61
104
  rescue
105
+ if tries < @max_retries
106
+ tries += 1
107
+ retry
108
+ end
109
+
62
110
  return nil
63
111
  end
64
112
  end
65
113
 
66
114
  def delete_feed(uri)
115
+ tries = 0
67
116
  begin
68
117
  return parse_response(HTTParty.delete(uri, headers: delete_headers))
69
118
  rescue
119
+ if tries < @max_retries
120
+ tries += 1
121
+ retry
122
+ end
123
+
70
124
  return nil
71
125
  end
72
126
  end
@@ -108,8 +162,10 @@ module Sheety
108
162
  @client.authorization= :google_app_default
109
163
  @auth = @client.authorization
110
164
  @auth.scope = 'https://spreadsheets.google.com/feeds'
165
+ @max_retries = 0
166
+ @max_read_retries = 0
111
167
 
112
168
  return self
113
169
  end
114
170
  end
115
- end
171
+ end
@@ -27,10 +27,16 @@ module Sheety::Children
27
27
  end
28
28
 
29
29
  module ClassMethods
30
+ # Defines a child relationship with the given name
31
+ # symbol (required): the name of the child
32
+ # options (required): a hash of options
33
+ # - klass (required): the class to use for the child instances
34
+ # - link (required): the url to fetch the children from
35
+ # -
30
36
  def atom_children(symbol, options)
31
37
 
32
38
  if options.blank?
33
- raise ArgumentError.new("blank options #{options} are not valid options for bear_children")
39
+ raise ArgumentError.new("blank options #{options} are not valid options for atom_children")
34
40
  end
35
41
 
36
42
  if options[:klass].blank? || options[:klass].class != Class
@@ -42,11 +48,11 @@ module Sheety::Children
42
48
  end
43
49
 
44
50
  unless method_defined?(:link)
45
- raise TypeError.new("#{self} must respond to :link to bear_children")
51
+ raise TypeError.new("#{self} must respond to :link to use atom_children")
46
52
  end
47
53
 
48
54
  if options[:merge_links] && method_defined?(:add_links) == false
49
- raise TypeError.new("#{self} must respond to :add_links to bear_children with mrege_links=true")
55
+ raise TypeError.new("#{self} must respond to :add_links in atom_children with merge_links=true")
50
56
  end
51
57
 
52
58
  plural = symbol.to_s
@@ -62,14 +68,20 @@ module Sheety::Children
62
68
  except_any_method = :"#{plural}_except_any"
63
69
  find_first_method = :"find_#{singular}"
64
70
 
71
+ # Defines a method that instantiates a new child
65
72
  define_method(new_method) do |entry=nil|
66
73
  options[:klass].new(self, entry)
67
74
  end
68
75
 
76
+ # Defines a method that fetches children from Google
69
77
  define_method(get_method) do |force_refetch=false|
70
78
  if instance_variable_get(inst_var_sym).nil? || force_refetch
71
79
  list = Sheety::Api.inst.get_feed(link(options[:link])) # sort of a cyclic dependency, suboptimal
72
80
 
81
+ if list.nil?
82
+ raise Sheety::SheetyFetchError.new, "Fetching #{plural} failed!"
83
+ end
84
+
73
85
  # TODO: Create a ListFeed Object so the links we get here don't need to be worried about collisions on link ids
74
86
  add_links(list['link']) if !list['link'].blank? && options[:merge_links]
75
87
 
@@ -83,28 +95,45 @@ module Sheety::Children
83
95
  return instance_variable_get(inst_var_sym)
84
96
  end
85
97
 
86
- define_method(enum_method) do |constraints, method|
87
- return method(get_method).call().send(method) do |item|
98
+ # Defines a helper-method that will iterate over children checking that each item passes all constraints given
99
+ define_method(enum_method) do |constraints, enumeration_method|
100
+ children = method(get_method).()
101
+
102
+ if children.nil?
103
+ return []
104
+ end
105
+
106
+ return children.send(enumeration_method) do |item|
88
107
  constraints.all? do |constraint|
89
108
  _passes_constraint(_get_i_val(item, constraint[0], options[:accessor]), constraint[1])
90
109
  end
91
110
  end
92
111
  end
93
112
 
113
+ # Defines a method that selects the children who adhere to all constraints given
94
114
  define_method(where_method) do |constraints|
95
115
  return method(enum_method).call(constraints, :find_all)
96
116
  end
97
117
 
118
+ # Defines a method that selects the first child that passes all constraints given
98
119
  define_method(find_first_method) do |constraints|
99
120
  return method(enum_method).call(constraints, :detect)
100
121
  end
101
122
 
123
+ # Defines a method that selects those children who fail to meet all constraint given
102
124
  define_method(except_method) do |constraints|
103
125
  return method(enum_method).call(constraints, :reject)
104
126
  end
105
127
 
128
+ # Defines a method that selects those children who fail to meet at least one constraint given
106
129
  define_method(except_any_method) do |constraints|
107
- return method(get_method).call().reject do |item|
130
+ children = method(get_method).()
131
+
132
+ if children.nil?
133
+ return []
134
+ end
135
+
136
+ return children.reject do |item|
108
137
  constraints.any? do |constraint|
109
138
  _passes_constraint(_get_i_val(item, constraint[0], options[:accessor]), constraint[1])
110
139
  end
@@ -112,4 +141,4 @@ module Sheety::Children
112
141
  end
113
142
  end
114
143
  end
115
- end
144
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sheety
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Blake Israel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-04 00:00:00.000000000 Z
11
+ date: 2017-01-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: xml-simple
@@ -92,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
92
  version: '0'
93
93
  requirements: []
94
94
  rubyforge_project:
95
- rubygems_version: 2.2.2
95
+ rubygems_version: 2.5.1
96
96
  signing_key:
97
97
  specification_version: 4
98
98
  summary: A Google Spreadsheets Gem