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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 733e87a91eed5049041559776cfafcdbb61fb3e8
4
- data.tar.gz: 755bc837c4785e22ea5b9bc24fb968334b935668
3
+ metadata.gz: 99b833cc64617aeef04983e52e3c2295532e1e67
4
+ data.tar.gz: e90d666c3c8f45fafede98991220aaead1ab3955
5
5
  SHA512:
6
- metadata.gz: aa71837bacdc4083f1dbc9d1a039c6a689afe3b3283302892a5e886dc748b330130253755c2ac4540134913145e334b73d887e91607f574117cc2484926e87ba
7
- data.tar.gz: b49a74effdc93425877d0ff003882fedf0d6f9c3f31fd68866898deb82e341738f10584e951134b1a73236652c6481868ea1d7e7738315ebe098e9d129e3c4df
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
- proxy(method: :get, url: url_for(resource), headers: { params: query })
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
@@ -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
- @persisted.nil? && (@persisted ||= false)
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|
@@ -1,3 +1,3 @@
1
1
  module Cropio
2
- VERSION = '0.2'
2
+ VERSION = '0.3'
3
3
  end
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.2'
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-24 00:00:00.000000000 Z
11
+ date: 2017-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json