fieldview 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 05858ad578d5fc9f1a42d4f15c8d9ee0b7299f01
4
- data.tar.gz: be48bc245a2c2a38c6b850b51f735ca4d8db193a
3
+ metadata.gz: 42ce3fc3ca592d38465e8625ab4ce76a19204ef3
4
+ data.tar.gz: 73bcdb8be3ae5e3f188f14e37233f9012536608a
5
5
  SHA512:
6
- metadata.gz: d1893ef1af5bdcc6a9ca2790e9806d58828d24a859ad944109b611a1af556688a45761290f0694821a5690c44c3b3e86d59a69135178eec56bcad0598890997c
7
- data.tar.gz: cad12cb23d33a5569cdd3583429bab951ede0d58c5fcc4ca6ea80e27266fff1554717d49edc4b28c76a4c5dfbaf2c7fc72507c5585e380d18e832f3f43866341
6
+ metadata.gz: 79d8559523948541f50ea3e826d163875013e30b3447285f1c948eefcf32a4371ff8e77689bf1137e45fe97e62f232af9913b1b24cc972cf2071268b3b2a7f05
7
+ data.tar.gz: 90b3f2a25cfdcaed05e9cb28a53737a24e2dab9b2f93eb9db159497472bf594d6783bcc21a87a84ce18f2b9f4adaa5fd6fdb1dfbc4c1e85f3fed9ceb69bca123
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fieldview (0.0.0)
4
+ fieldview (0.0.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -48,7 +48,7 @@ auth_token = FieldView::AuthToken.new(refresh_token: <RTOKEN>)
48
48
 
49
49
  # Then you can do something like this:
50
50
 
51
- fields = FieldView::Fields.list(auth_token)
51
+ fields = FieldView::Field.list(auth_token)
52
52
 
53
53
  fields.each do |field|
54
54
  puts field.boundary
@@ -58,6 +58,30 @@ fields.has_more?
58
58
 
59
59
  ```
60
60
 
61
+ ## Uploading a File in Chunks
62
+
63
+ Assuming you've acquired an AuthToken, then you can do something like the following:
64
+
65
+ ``` ruby
66
+ file = <PATH TO SOME FILE>
67
+ upload = FieldView::Upload.create(auth_token,
68
+ Digest::MD5.file(file).to_s, File.size(file),
69
+ <SOME FILE TYPE>)
70
+
71
+ start_bytes = 0
72
+ File.open(file, "rb") do|f|
73
+ response = nil
74
+ until f.eof?
75
+ # since it's 0 based
76
+ bytes = f.read(FieldView::Upload::REQUIRED_CHUNK_SIZE)
77
+ end_bytes = start_bytes + bytes.bytesize - 1
78
+ response = upload.upload_chunk(start_bytes, end_bytes, bytes)
79
+ puts "Uploaded #{start_bytes}-#{end_bytes}/#{File.size(file)}"
80
+ start_bytes = end_bytes + 1
81
+ end
82
+ end
83
+ ```
84
+
61
85
  ## Development
62
86
 
63
87
  Run all tests:
@@ -70,7 +94,7 @@ Run a single test suite:
70
94
 
71
95
  Run a single test:
72
96
 
73
- bundle exec ruby -Ilib/ test/field_view_test.rb -n /client.id/
97
+ bundle exec ruby -Ilib/ test/field_view_test.rb -n /requires_redirect_uri/
74
98
 
75
99
  ## TODOs
76
100
 
@@ -12,13 +12,21 @@ module FieldView
12
12
  # When the access token expires we'll need to refresh,
13
13
  # can be specified as part of the object, 14399 is what is being
14
14
  # returned at the time of writing this (2017-04-11)
15
- self.access_token_expiration_at = params[:access_token_expiration_at] || (now + (params[:expires_in]||14399))
15
+ if params[:access_token_expiration_at].is_a?(String) then
16
+ self.access_token_expiration_at = Time.parse(params[:access_token_expiration_at])
17
+ else
18
+ self.access_token_expiration_at = params[:access_token_expiration_at] || (now + (params[:expires_in]||14399))
19
+ end
16
20
 
17
21
  # Refresh token isn't required, but can be initialized with this
18
22
  self.refresh_token = params[:refresh_token]
19
23
 
20
24
  # Refresh token technically expires in 30 days
21
- self.refresh_token_expiration_at = params[:access_token_expiration_at] || (now + (30)*24*60*60)
25
+ if params[:refresh_token_expiration_at].is_a?(String) then
26
+ self.refresh_token_expiration_at = Time.parse(params[:refresh_token_expiration_at])
27
+ else
28
+ self.refresh_token_expiration_at = params[:refresh_token_expiration_at] || (now + (30)*24*60*60)
29
+ end
22
30
  end
23
31
 
24
32
  def access_token_expired?
@@ -106,7 +114,7 @@ module FieldView
106
114
  def self.new_auth_token_with_code_from_redirect_code(code, redirect_uri: nil)
107
115
  http, request = build_token_request([
108
116
  ["grant_type", "authorization_code"],
109
- ["redirect_uri", FieldView.redirect_uri || redirect_uri],
117
+ ["redirect_uri", redirect_uri || FieldView.redirect_uri],
110
118
  ["code", code]
111
119
  ])
112
120
  response = http.request(request)
@@ -1,20 +1,4 @@
1
1
  module FieldView
2
- class Feature
3
- attr_accessor :type, :coordinates
4
- def initialize(json_feature_object)
5
- # Use RGEO??
6
- self.type = json_feature_object[:type]
7
- self.coordinates = json_feature_object[:coordinates]
8
- end
9
-
10
- def point?()
11
- return !!(self.type =~ /\Apoint\z/i)
12
- end
13
-
14
- def multi_polygon?()
15
- return !!(self.type =~ /\Amultipolygon\z/i)
16
- end
17
- end
18
2
  class Boundary
19
3
  attr_accessor :id, :units, :area, :centroid, :geometry
20
4
  def initialize(json_object)
@@ -0,0 +1,24 @@
1
+ module FieldView
2
+ class Feature
3
+ attr_accessor :type, :coordinates, :data
4
+
5
+ def initialize(json_feature_object)
6
+ # Use RGEO??
7
+ self.type = json_feature_object[:type]
8
+ self.coordinates = json_feature_object[:coordinates]
9
+ self.data = json_feature_object
10
+ end
11
+
12
+ def point?()
13
+ return !!(self.type =~ /\Apoint\z/i)
14
+ end
15
+
16
+ def multi_polygon?()
17
+ return !!(self.type =~ /\Amultipolygon\z/i)
18
+ end
19
+
20
+ def polygon?()
21
+ return !!(self.type =~ /\Apolygon\z/i)
22
+ end
23
+ end
24
+ end
@@ -1,5 +1,6 @@
1
1
  module FieldView
2
2
  class Field < Requestable
3
+ PATH = "fields"
3
4
  attr_accessor :id
4
5
  attr_accessor :name
5
6
  attr_accessor :boundary_id
@@ -10,6 +11,45 @@ module FieldView
10
11
  super(auth_token)
11
12
  end
12
13
 
14
+ def self.retrieve(auth_token, id)
15
+ response = auth_token.execute_request!(:get, "#{PATH}/#{id}")
16
+
17
+ Util.verify_response_with_code("Field retrieve", response, 200)
18
+
19
+ return new(response.data, auth_token)
20
+ end
21
+
22
+ def self.list(auth_token, limit: nil, next_token: nil)
23
+ limit ||= FieldView.default_page_limit
24
+ response = auth_token.execute_request!(:get, PATH,
25
+ headers: {
26
+ FieldView::NEXT_TOKEN_HEADER_KEY => next_token,
27
+ FieldView::PAGE_LIMIT_HEADER_KEY => limit
28
+ })
29
+ next_token = response.http_headers[FieldView::NEXT_TOKEN_HEADER_KEY]
30
+
31
+ if (response.http_status == 200 || response.http_status == 206) then
32
+ # 206: Partial result, will have more data
33
+ # 200: When all the results were in the list
34
+ return_data = response.data[:results]
35
+ elsif (response.http_status == 304)
36
+ # 304: Nothing modified since last request
37
+ return_data = []
38
+ else
39
+ # This should never happen
40
+ raise UnexpectedResponseError.new("Fields list expects 200,206, or 304 for codes",
41
+ fieldview_response: response)
42
+ end
43
+
44
+ return ListObject.new(
45
+ self,
46
+ auth_token,
47
+ return_data.collect { |i| Field.new(i, auth_token) },
48
+ response.http_status,
49
+ next_token: next_token,
50
+ limit: limit)
51
+ end
52
+
13
53
  def boundary
14
54
  @boundary ||= nil
15
55
  if @boundary.nil?
@@ -20,9 +20,8 @@ module FieldView
20
20
  def next_page!()
21
21
  return if !self.more_pages?()
22
22
  new_list = @listable.list(auth_token, limit: self.limit, next_token: self.next_token)
23
- @data = new_list.data
24
- @last_http_status = new_list.last_http_status
25
- @auth_token = new_list.auth_token
23
+ initialize(new_list.listable, new_list.auth_token,
24
+ new_list.data, new_list.last_http_status, next_token: new_list.next_token, limit: new_list.limit)
26
25
  end
27
26
 
28
27
  # alias for more_pages
@@ -1,7 +1,7 @@
1
1
  module FieldView
2
2
  class Util
3
3
  def self.http_status_is_more_in_list?(http_status)
4
- return http_status.to_i == 206
4
+ return http_status.nil? || http_status.to_i == 206
5
5
  end
6
6
 
7
7
  def self.verify_response_with_code(message, fieldview_response, *acceptable_response_codes)
@@ -1,3 +1,3 @@
1
1
  module FieldView
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
data/lib/fieldview.rb CHANGED
@@ -8,9 +8,9 @@ require 'fieldview/version'
8
8
 
9
9
  # API Support Classes
10
10
  require 'fieldview/errors'
11
+ require 'fieldview/feature'
11
12
  require 'fieldview/requestable'
12
13
  require 'fieldview/auth_token'
13
- require 'fieldview/fields'
14
14
  require 'fieldview/util'
15
15
  require 'fieldview/fieldview_response'
16
16
  require 'fieldview/field'
@@ -21,6 +21,20 @@ class TestAuthToken < Minitest::Test
21
21
  teardown_for_api_request
22
22
  end
23
23
 
24
+ def test_expiration_at_initialization
25
+ five_seconds_from_now = Time.now + 5
26
+ ten_seconds_from_now = Time.now + 10
27
+ token = FieldView::AuthToken.new(access_token: "yyy", refresh_access_token: "xxx",
28
+ access_token_expiration_at: five_seconds_from_now, refresh_token_expiration_at: ten_seconds_from_now)
29
+ assert_equal five_seconds_from_now, token.access_token_expiration_at
30
+ assert_equal ten_seconds_from_now, token.refresh_token_expiration_at
31
+
32
+ token = FieldView::AuthToken.new(access_token: "yyy", refresh_access_token: "xxx",
33
+ access_token_expiration_at: "2017-05-05T18:44:03.520-06:00", refresh_token_expiration_at: "2017-05-05T18:44:04.520-06:00")
34
+ assert_equal Time.parse("2017-05-05T18:44:03.520-06:00"), token.access_token_expiration_at
35
+ assert_equal Time.parse("2017-05-05T18:44:04.520-06:00"), token.refresh_token_expiration_at
36
+ end
37
+
24
38
  def test_build_token_request
25
39
  http, request = FieldView::AuthToken.build_token_request([["dont", "care"]])
26
40
  assert_equal "/api/oauth/token?dont=care", request.path
data/test/test_field.rb CHANGED
@@ -2,15 +2,85 @@ require File.expand_path('../test_helper', __FILE__)
2
2
 
3
3
  class TestField < Minitest::Test
4
4
  FIXTURE = API_FIXTURES.fetch(:single_field_list)[:results][0]
5
- def test_initialization
6
- field = FieldView::Field.new(FIXTURE, new_auth_token)
5
+ LIST_FIXTURE = API_FIXTURES.fetch(:single_field_list)
6
+
7
+ def setup
8
+ setup_for_api_requests
9
+ end
7
10
 
11
+ def teardown
12
+ teardown_for_api_request
13
+ end
14
+
15
+ def test_retrieve()
16
+ stub_request(:get, /fields/).
17
+ to_return(status: 200, body: FIXTURE.to_json)
18
+
19
+ field = FieldView::Field.retrieve(new_auth_token, FIXTURE[:id])
20
+
8
21
  assert_equal FIXTURE[:id], field.id
9
22
  assert_equal FIXTURE[:name], field.name
10
23
  assert_equal FIXTURE[:boundaryId], field.boundary_id
11
24
  assert_equal new_auth_token.access_token, field.auth_token.access_token
12
25
  end
26
+
27
+ def test_list_with_one_page()
28
+ next_token = "AZXJKLA123"
29
+ stub_request(:get, /fields/).
30
+ with(headers: { FieldView::PAGE_LIMIT_HEADER_KEY => FieldView.default_page_limit.to_s}).
31
+ to_return(status: 200, body: LIST_FIXTURE.to_json(),
32
+ headers: next_token_headers)
33
+ fields = FieldView::Field.list(new_auth_token)
34
+
35
+ assert_equal LIST_FIXTURE[:results].length, fields.data.length
36
+ assert_equal LIST_FIXTURE[:results][0][:id], fields.data[0].id
37
+ assert_equal next_token, fields.next_token
38
+ assert_equal FieldView::Field, fields.listable
39
+ end
40
+
41
+ def test_list_with_no_more_data
42
+ stub_request(:get, /fields/).
43
+ with(headers: { FieldView::PAGE_LIMIT_HEADER_KEY => FieldView.default_page_limit.to_s}).
44
+ to_return(status: 304, body: {}.to_json(),
45
+ headers: next_token_headers)
46
+ fields = FieldView::Field.list(new_auth_token)
47
+
48
+ assert_equal 0, fields.data.length
49
+ end
50
+
51
+ def test_with_more_data
52
+ next_token = "AZXJKLA123"
53
+ stub_request(:get, /fields/).
54
+ with(headers: { FieldView::PAGE_LIMIT_HEADER_KEY => FieldView.default_page_limit.to_s}).
55
+ to_return(status: 200, body: LIST_FIXTURE.to_json(),
56
+ headers: next_token_headers)
57
+ fields = FieldView::Field.list(new_auth_token)
58
+
59
+ assert_equal LIST_FIXTURE[:results].length, fields.data.length
60
+ assert_equal LIST_FIXTURE[:results][0][:id], fields.data[0].id
61
+ assert_equal next_token, fields.next_token
62
+ end
63
+
64
+ def test_with_limit
65
+ stub_request(:get, /fields/).
66
+ with(headers: { FieldView::PAGE_LIMIT_HEADER_KEY => 1}).
67
+ to_return(status: 200, body: LIST_FIXTURE.to_json(),
68
+ headers: next_token_headers)
69
+ fields = FieldView::Field.list(new_auth_token, limit: 1)
13
70
 
71
+ assert_equal 1, fields.data.length
72
+ end
73
+
74
+ def test_with_next_token
75
+ next_token = "AZXJKLA123"
76
+ stub_request(:get, /fields/).
77
+ with(headers: { FieldView::NEXT_TOKEN_HEADER_KEY => next_token }).
78
+ to_return(status: 200, body: LIST_FIXTURE.to_json())
79
+
80
+ fields = FieldView::Field.list(new_auth_token, next_token: next_token)
81
+ assert_equal 1, fields.data.length
82
+ end
83
+
14
84
  def test_get_boundary
15
85
  field = FieldView::Field.new(FIXTURE, new_auth_token)
16
86
 
@@ -2,19 +2,43 @@ require File.expand_path('../test_helper', __FILE__)
2
2
 
3
3
  class TestListObject < Minitest::Test
4
4
  FIXTURE = API_FIXTURES.fetch(:single_field_list)
5
-
5
+
6
+ def setup
7
+ setup_for_api_requests
8
+ end
9
+
10
+ def teardown
11
+ teardown_for_api_request
12
+ end
13
+
6
14
  def test_each_loop
7
15
  data = ["x","y","z"]
8
- list = FieldView::ListObject.new(FieldView::Fields, new_auth_token, data, 200, next_token: nil)
16
+ list = FieldView::ListObject.new(FieldView::Field, new_auth_token, data, 200, next_token: nil)
9
17
 
10
18
  list.each_with_index do |x, i|
11
19
  assert_equal data[i], x
12
20
  end
13
21
  end
14
22
 
23
+ def test_restart
24
+ next_token = "JZIOJKLJ"
25
+ old_next_token = "DONTCARE"
26
+ list = FieldView::ListObject.new(FieldView::Field, new_auth_token,
27
+ ["dont","care"], 206, next_token: old_next_token)
28
+ stub_request(:get, /fields/).
29
+ to_return(status: 200, body: API_FIXTURES[:field_two_list].to_json(),
30
+ headers: next_token_headers(next_token))
31
+
32
+ list.restart!
33
+
34
+ assert_equal list.next_token, next_token
35
+ assert_equal list.last_http_status, 200
36
+ assert_equal API_FIXTURES[:field_two_list][:results][0][:id], list.data[0].id
37
+ end
38
+
15
39
  def test_get_next_page
16
40
  next_token = "JZIOJKLJ"
17
- list = FieldView::ListObject.new(FieldView::Fields, new_auth_token,
41
+ list = FieldView::ListObject.new(FieldView::Field, new_auth_token,
18
42
  ["dont","care"], 206, next_token: next_token)
19
43
  stub_request(:get, /fields/).
20
44
  with(headers: next_token_headers(next_token)).
@@ -26,7 +50,7 @@ class TestListObject < Minitest::Test
26
50
  end
27
51
 
28
52
  assert_equal 1, list.data.length
29
- assert_equal API_FIXTURES[:field_two_list][:results][0][:id], list.data[0].id
53
+ assert_equal API_FIXTURES[:field_two_list][:results][0][:id], list.data[0].id
30
54
  assert !list.more_pages?()
31
55
  end
32
56
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fieldview
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Susmarski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-19 00:00:00.000000000 Z
11
+ date: 2017-05-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: ' FieldView is used to make data-driven decisions to maximize your return
14
14
  on every acre. This Ruby Gem is provided as a convenient way to access their API.'
@@ -28,8 +28,8 @@ files:
28
28
  - lib/fieldview/auth_token.rb
29
29
  - lib/fieldview/boundary.rb
30
30
  - lib/fieldview/errors.rb
31
+ - lib/fieldview/feature.rb
31
32
  - lib/fieldview/field.rb
32
- - lib/fieldview/fields.rb
33
33
  - lib/fieldview/fieldview_response.rb
34
34
  - lib/fieldview/list_object.rb
35
35
  - lib/fieldview/requestable.rb
@@ -41,7 +41,6 @@ files:
41
41
  - test/test_auth_token.rb
42
42
  - test/test_boundary.rb
43
43
  - test/test_field.rb
44
- - test/test_fields.rb
45
44
  - test/test_fieldview.rb
46
45
  - test/test_helper.rb
47
46
  - test/test_list_object.rb
@@ -75,7 +74,6 @@ test_files:
75
74
  - test/test_auth_token.rb
76
75
  - test/test_boundary.rb
77
76
  - test/test_field.rb
78
- - test/test_fields.rb
79
77
  - test/test_fieldview.rb
80
78
  - test/test_helper.rb
81
79
  - test/test_list_object.rb
@@ -1,34 +0,0 @@
1
- module FieldView
2
- class Fields
3
- PATH = "fields"
4
- def self.list(auth_token, limit: nil, next_token: nil)
5
- limit ||= FieldView.default_page_limit
6
- response = auth_token.execute_request!(:get, PATH,
7
- headers: {
8
- FieldView::NEXT_TOKEN_HEADER_KEY => next_token,
9
- FieldView::PAGE_LIMIT_HEADER_KEY => limit
10
- })
11
- next_token = response.http_headers[FieldView::NEXT_TOKEN_HEADER_KEY]
12
-
13
- if (response.http_status == 200 || response.http_status == 206) then
14
- # 206: Partial result, will have more data
15
- # 200: When all the results were in the list
16
- return_data = response.data[:results]
17
- elsif (response.http_status == 304)
18
- # 304: Nothing modified since last request
19
- return_data = []
20
- else
21
- # This should never happen
22
- return_data = nil
23
- end
24
-
25
- return ListObject.new(
26
- self,
27
- auth_token,
28
- return_data.collect { |i| Field.new(i, auth_token) },
29
- response.http_status,
30
- next_token: next_token,
31
- limit: limit)
32
- end
33
- end
34
- end
data/test/test_fields.rb DELETED
@@ -1,70 +0,0 @@
1
- require File.expand_path('../test_helper', __FILE__)
2
-
3
- class TestFields < Minitest::Test
4
- FIXTURE = API_FIXTURES.fetch(:single_field_list)
5
-
6
- def setup
7
- setup_for_api_requests
8
- end
9
-
10
- def teardown
11
- teardown_for_api_request
12
- end
13
-
14
- def test_list_with_one_page()
15
- next_token = "AZXJKLA123"
16
- stub_request(:get, /fields/).
17
- with(headers: { FieldView::PAGE_LIMIT_HEADER_KEY => FieldView.default_page_limit.to_s}).
18
- to_return(status: 200, body: FIXTURE.to_json(),
19
- headers: next_token_headers)
20
- fields = FieldView::Fields.list(new_auth_token)
21
-
22
- assert_equal FIXTURE[:results].length, fields.data.length
23
- assert_equal FIXTURE[:results][0][:id], fields.data[0].id
24
- assert_equal next_token, fields.next_token
25
- assert_equal FieldView::Fields, fields.listable
26
- end
27
-
28
- def test_list_with_no_more_data
29
- stub_request(:get, /fields/).
30
- with(headers: { FieldView::PAGE_LIMIT_HEADER_KEY => FieldView.default_page_limit.to_s}).
31
- to_return(status: 304, body: {}.to_json(),
32
- headers: next_token_headers)
33
- fields = FieldView::Fields.list(new_auth_token)
34
-
35
- assert_equal 0, fields.data.length
36
- end
37
-
38
- def test_with_more_data
39
- next_token = "AZXJKLA123"
40
- stub_request(:get, /fields/).
41
- with(headers: { FieldView::PAGE_LIMIT_HEADER_KEY => FieldView.default_page_limit.to_s}).
42
- to_return(status: 200, body: FIXTURE.to_json(),
43
- headers: next_token_headers)
44
- fields = FieldView::Fields.list(new_auth_token)
45
-
46
- assert_equal FIXTURE[:results].length, fields.data.length
47
- assert_equal FIXTURE[:results][0][:id], fields.data[0].id
48
- assert_equal next_token, fields.next_token
49
- end
50
-
51
- def test_with_limit
52
- stub_request(:get, /fields/).
53
- with(headers: { FieldView::PAGE_LIMIT_HEADER_KEY => 1}).
54
- to_return(status: 200, body: FIXTURE.to_json(),
55
- headers: next_token_headers)
56
- fields = FieldView::Fields.list(new_auth_token, limit: 1)
57
-
58
- assert_equal 1, fields.data.length
59
- end
60
-
61
- def test_with_next_token
62
- next_token = "AZXJKLA123"
63
- stub_request(:get, /fields/).
64
- with(headers: { FieldView::NEXT_TOKEN_HEADER_KEY => next_token }).
65
- to_return(status: 200, body: FIXTURE.to_json())
66
-
67
- fields = FieldView::Fields.list(new_auth_token, next_token: next_token)
68
- assert_equal 1, fields.data.length
69
- end
70
- end