cropio-ruby 0.2 → 0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -0
- data/lib/cropio/connection/proxiable.rb +18 -3
- data/lib/cropio/resource/base.rb +42 -2
- data/lib/cropio/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 99b833cc64617aeef04983e52e3c2295532e1e67
|
4
|
+
data.tar.gz: e90d666c3c8f45fafede98991220aaead1ab3955
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3dd53858623b83b00c6310915cfbd1146047ad0ca3c2ec221f2ae232161ba551873bc8311a031b59f97c604d4ad909374473b82faaff25e134b0a826d73a25a7
|
7
|
+
data.tar.gz: 09abf8825874daf8a305464dbe766ce73fbe7cdc86c1c239aa45b5395695820110c5dcc95e656b0231c9e4ddea61dbc82f2df297abe8ab8bef2a86ae8e142136
|
data/README.md
CHANGED
@@ -79,6 +79,12 @@ crop = Crop.all.last
|
|
79
79
|
crop.destroy
|
80
80
|
```
|
81
81
|
|
82
|
+
- changes
|
83
|
+
```ruby
|
84
|
+
Crop.changes('2012-01-01', '2016-01-01')
|
85
|
+
Crop.changes(Date.new(2012, 1, 1))
|
86
|
+
```
|
87
|
+
|
82
88
|
## Development
|
83
89
|
|
84
90
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -6,7 +6,9 @@ module Cropio
|
|
6
6
|
module Proxiable
|
7
7
|
# Accepts reources name and params to perform HTTPS GET request.
|
8
8
|
def get(resource, query = {})
|
9
|
-
|
9
|
+
rmethod = extract_resource_method!(query)
|
10
|
+
proxy(method: :get, url: url_for(resource, rmethod),
|
11
|
+
headers: { params: query })
|
10
12
|
end
|
11
13
|
|
12
14
|
# Accepts reources name and params to perform HTTPS POST request.
|
@@ -26,13 +28,21 @@ module Cropio
|
|
26
28
|
|
27
29
|
protected
|
28
30
|
|
29
|
-
def url_for(resource)
|
30
|
-
"#{Cropio::Connection::Configurable::BASE_URL}/#{resource}"
|
31
|
+
def url_for(resource, resource_method = nil)
|
32
|
+
url = "#{Cropio::Connection::Configurable::BASE_URL}/#{resource}"
|
33
|
+
url += "/#{resource_method}" if resource_method
|
34
|
+
url
|
35
|
+
end
|
36
|
+
|
37
|
+
def extract_resource_method!(query)
|
38
|
+
fail(ArgumentError) unless query.is_a?(Hash)
|
39
|
+
query.delete(:resource_method)
|
31
40
|
end
|
32
41
|
|
33
42
|
def proxy(options)
|
34
43
|
options[:headers] ||= {}
|
35
44
|
options[:headers].merge!(headers)
|
45
|
+
clear_params_in_options!(options)
|
36
46
|
res = send("proxy_#{options[:method]}", options)
|
37
47
|
options[:method].eql?(:delete) ? res : JSON.parse(res)
|
38
48
|
rescue RestClient::UnprocessableEntity => e
|
@@ -40,6 +50,11 @@ module Cropio
|
|
40
50
|
raise e
|
41
51
|
end
|
42
52
|
|
53
|
+
def clear_params_in_options!(options)
|
54
|
+
return if options[:headers][:params].nil?
|
55
|
+
options[:headers][:params].reject! { |_k, v| v.nil? }
|
56
|
+
end
|
57
|
+
|
43
58
|
def proxy_get(options)
|
44
59
|
RestClient::Request.execute(options)
|
45
60
|
end
|
data/lib/cropio/resource/base.rb
CHANGED
@@ -32,12 +32,18 @@ module Cropio
|
|
32
32
|
all.count
|
33
33
|
end
|
34
34
|
|
35
|
+
def self.changes(from_time = nil, to_time = nil)
|
36
|
+
from_time = to_datetime_if_string(from_time)
|
37
|
+
to_time = to_datetime_if_string(to_time)
|
38
|
+
to_instances(get_all_changes(from_time, to_time))
|
39
|
+
end
|
40
|
+
|
35
41
|
# Returns persistance of the resource.
|
36
42
|
# Resource is persisted if it is saved
|
37
43
|
# and not deleted, if this resource exists
|
38
44
|
# on Cropio servers.
|
39
45
|
def persisted?
|
40
|
-
|
46
|
+
!@persisted.nil? && (@persisted ||= false)
|
41
47
|
end
|
42
48
|
|
43
49
|
# Saves current resource to Cropio.
|
@@ -64,6 +70,14 @@ module Cropio
|
|
64
70
|
|
65
71
|
private
|
66
72
|
|
73
|
+
def self.to_datetime_if_string(param)
|
74
|
+
if param.is_a?(Date) || param.is_a?(DateTime)
|
75
|
+
param.strftime
|
76
|
+
elsif !param.nil?
|
77
|
+
DateTime.parse(param).strftime
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
67
81
|
# Returns pluralized name of own type.
|
68
82
|
def resources_name
|
69
83
|
self.class.resources_name
|
@@ -73,7 +87,7 @@ module Cropio
|
|
73
87
|
def self.get_all_chunks(options = {})
|
74
88
|
response = nil
|
75
89
|
buffer = []
|
76
|
-
limit = options[:limit] || (2**32 - 1)
|
90
|
+
limit = options[:limit] || (2 ** 32 - 1)
|
77
91
|
while data?(response) && limit > 0
|
78
92
|
limit -= limit < LIMIT ? limit : LIMIT
|
79
93
|
response = get_chunk(limit: limit, from_id: offset(buffer))
|
@@ -82,6 +96,24 @@ module Cropio
|
|
82
96
|
buffer
|
83
97
|
end
|
84
98
|
|
99
|
+
def self.get_all_changes(from_time, to_time)
|
100
|
+
response = nil
|
101
|
+
buffer = []
|
102
|
+
limit = 2 ** 32 - 1
|
103
|
+
while data?(response) && limit > 0
|
104
|
+
response = get_changes(limit: limit, from_time: from_time,
|
105
|
+
to_time: to_time)
|
106
|
+
limit -= limit < LIMIT ? limit : LIMIT
|
107
|
+
to_time = last_record_time(response) || from_time
|
108
|
+
buffer += response['data']
|
109
|
+
end
|
110
|
+
buffer
|
111
|
+
end
|
112
|
+
|
113
|
+
def self.last_record_time(response)
|
114
|
+
response['meta']['response']['last_record_time']
|
115
|
+
end
|
116
|
+
|
85
117
|
# Gets offset for next chunk during download.
|
86
118
|
def self.offset(buffer)
|
87
119
|
buffer.any? ? buffer.last['id'] + 1 : 0
|
@@ -103,6 +135,14 @@ module Cropio
|
|
103
135
|
from_id: options[:from_id])
|
104
136
|
end
|
105
137
|
|
138
|
+
def self.get_changes(options)
|
139
|
+
PROXY.get(resources_name,
|
140
|
+
resource_method: :changes,
|
141
|
+
limit: options[:limit],
|
142
|
+
from_time: options[:from_time],
|
143
|
+
to_time: options[:to_time])
|
144
|
+
end
|
145
|
+
|
106
146
|
# Converts each received attribute's hash to resources.
|
107
147
|
def self.to_instances(attr_sets)
|
108
148
|
attr_sets.map do |attr_set|
|
data/lib/cropio/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cropio-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.3'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sergey Vernidub
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-02-
|
11
|
+
date: 2017-02-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|