agx 0.2.0 → 0.2.2

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: 6757c8bcbacdc7d646a5378085660012c46155f9
4
- data.tar.gz: b31be8a6b2885075817a20937d03a5d218f3878a
3
+ metadata.gz: 358ca46754287a4f484a20f453f35462eb7596ed
4
+ data.tar.gz: fbb5693193f3bc99cd8119b991fe8373bf15f7fb
5
5
  SHA512:
6
- metadata.gz: 4b6e5172e7afec61d020a279302a47fba64a7ef77ea0a846d17bb0b71be4bfe1c697134fbb3e057472febdb7de4ade587caac2f6435f7987744592f2c7cfaac4
7
- data.tar.gz: 1df9291443ea6a451a448e0df9daf9b04421adc57c84f880448e70ab08784318f36a77a68a0c4541e4aa55c805d3cba90857a404f8a0e44b963ff422c3fd3db8
6
+ metadata.gz: 2070b7b7e2ea87526d16e490bf92eb7c257fe5df80fcdd8c298c302bb28128642379f7455e217aceae9a3c0475d83bfb5d4db8a0dad29399bd836b98fbb93d03
7
+ data.tar.gz: 2c5d0292b4efc172cbf850ce4d49874a12bf186983f5dfadf9af7549a154080a541de4639b01173b3a4a9998aae331665944724dbca49673a8be1d3a8fccd23a
data/README.md CHANGED
@@ -49,11 +49,11 @@ weeds = @agx_content_client.get("Weed", {publishDate: date.to_s})
49
49
 
50
50
  ### agX Sync API
51
51
 
52
- Setup agX Sync v4 Client (OAuth 2.0 / OpenID Connect 1.0 Authorization Code Flow)
52
+ Setup agX Sync Client (OAuth 2.0 / OpenID Connect 1.0 Authorization Code Flow)
53
53
 
54
54
  ***This requires that you have already previously authenticated and authorized
55
- a user to agX through the authorization code grant flow process and have
56
- persisted their sync ID, access token, refresh token, and access token
55
+ a user to agX with required scopes through the authorization code grant flow process
56
+ and have persisted their sync ID, access token, refresh token, and access token
57
57
  expiration timestamp.***
58
58
 
59
59
  ```ruby
@@ -65,7 +65,7 @@ expiration timestamp.***
65
65
  access_token: "agx_user_agx_token",
66
66
  refresh_token: "agx_user_refresh_token",
67
67
  token_expires_at: "access_token_expiration_timestamp",
68
- transaction_id: "agx_user_previous_transaction_id" # optional,
68
+ transaction_id: "agx_user_previous_transaction_id", # optional
69
69
  prod: true # optional, false for QA
70
70
  )
71
71
  ```
@@ -97,17 +97,48 @@ farms = @agx_sync_client.get("Grower/#{grower.guid}/Farm", last_sync_date.to_s)
97
97
  user_transaction_id = nil
98
98
  ```
99
99
 
100
+ ### agX Pictures API
101
+
102
+ Setup agX Pictures Client
103
+
104
+ ```ruby
105
+ @agx_pictures_client = Agx::Pictures::Client.new(
106
+ client_id: "your_client_id",
107
+ client_secret: "your_client_secret",
108
+ version: "v1", # optional
109
+ sync_id: "agx_user_sync_id",
110
+ access_token: "agx_user_agx_token",
111
+ refresh_token: "agx_user_refresh_token",
112
+ token_expires_at: "access_token_expiration_timestamp",
113
+ filepath: "/path/to/pictures/",
114
+ prod: true # optional, false for QA
115
+ )
116
+ ```
117
+
118
+ Make get requests for Pictures API images and metadata
119
+
120
+ ***Currently only get requests are supported***
121
+ ```ruby
122
+
123
+ # Get metadata
124
+ image_meta = @agx_pictures_client.get_metadata("661ee0c0-0cbc-4a7b-be39-1a9de49acc86")
125
+
126
+ # Get image and save to {filepath}/{sync_id}_{picture_id}.jpeg
127
+ image = @agx_pictures_client.get("661ee0c0-0cbc-4a7b-be39-1a9de49acc86")
128
+ # => "/path/to/pictures/7_661ee0c0-0cbc-4a7b-be39-1a9de49acc86.jpeg"
129
+ ```
130
+
100
131
  ## Development
101
132
 
102
133
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. To install this gem onto your local machine, run `bundle exec rake install`.
103
134
 
104
135
  ## Contributing
105
136
 
106
- Bug reports and pull requests are welcome on GitHub at https://github.com/CropQuest/agx-ruby.
137
+ Bug reports and pull requests are welcome on GitHub at https://github.com/beaorn/agx-ruby.
107
138
 
108
139
 
109
140
  ## License
110
141
 
111
- The gem is available as open source under the terms of the MIT License (see [LICENSE.txt](https://github.com/CropQuest/agx-ruby/blob/master/LICENSE.txt))
142
+ The gem is available as open source under the terms of the MIT License (see [LICENSE.txt](https://github.com/beaorn/agx-ruby/blob/master/LICENSE.txt))
112
143
 
113
144
  [agX](http://www.agxplatform.com/) is a registered trademark of [SST Software](http://www.sstsoftware.com/).
@@ -7,11 +7,11 @@ Gem::Specification.new do |spec|
7
7
  spec.name = "agx"
8
8
  spec.version = Agx::VERSION
9
9
  spec.authors = ["Bryce Johnston"]
10
- spec.email = ["bjohnston@cropquest.com"]
10
+ spec.email = ["bryce@agdeveloper.com"]
11
11
 
12
12
  spec.summary = %q{Ruby client for accessing agX Platform APIs.}
13
13
  spec.description = %q{Ruby client for accessing SST Software's agX Platform APIs.}
14
- spec.homepage = "https://github.com/CropQuest/agx-ruby"
14
+ spec.homepage = "https://github.com/beaorn/agx-ruby"
15
15
  spec.license = "MIT"
16
16
 
17
17
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
data/lib/agx.rb CHANGED
@@ -5,6 +5,7 @@ require "agx/version"
5
5
  require "agx/error"
6
6
  require "agx/content"
7
7
  require "agx/sync"
8
+ require "agx/pictures"
8
9
 
9
10
  module Agx
10
11
  end
@@ -0,0 +1,6 @@
1
+ require "agx/pictures/client"
2
+
3
+ module Agx
4
+ module Pictures
5
+ end
6
+ end
@@ -0,0 +1,123 @@
1
+ module Agx
2
+ module Pictures
3
+ class Client
4
+ attr_accessor :client_id, :client_secret, :site, :host, :authorize_url,
5
+ :token_url, :version, :sync_id, :access_token, :refresh_token, :token_expires_at
6
+
7
+ def initialize(client_id: nil, client_secret: nil, version: nil,
8
+ sync_id: nil, access_token: nil, refresh_token: nil,
9
+ token_expires_at: nil, prod: true, filepath: nil)
10
+ domain = (prod ? "agxplatform.com" : "qaagxplatform.com")
11
+ @client_id = client_id || ENV['AGX_SYNC_CLIENT_ID']
12
+ @client_secret = client_secret || ENV['AGX_SYNC_CLIENT_SECRET']
13
+ @site = "https://pictures.#{domain}"
14
+ @host = host || "pictures.#{domain}"
15
+ @authorize_url = "https://auth.#{domain}/identity/connect/Authorize"
16
+ @token_url = "https://auth.#{domain}/identity/connect/Token"
17
+ @version = version || "v1"
18
+ @sync_id = sync_id
19
+ @api_url = "#{@site}/api/#{@version}/picture/"
20
+ @filepath = filepath
21
+ @headers = {
22
+ 'oauth-scopes' => "Sync",
23
+ 'Host' => @host
24
+ }
25
+ @client = set_client
26
+ @token = {
27
+ access_token: access_token,
28
+ refresh_token: refresh_token,
29
+ expires_at: token_expires_at
30
+ }
31
+ end
32
+
33
+ def get(id)
34
+ url = "#{@api_url}#{id}"
35
+ begin
36
+ response = current_token.get(url, :headers => @headers)
37
+ filename = "#{@filepath}/#{@sync_id}_#{id}.jpeg"
38
+ File.open(filename, 'wb') { |fp| fp.write(response.body) }
39
+ rescue => e
40
+ handle_error(e)
41
+ end
42
+ end
43
+
44
+ def get_metadata(id)
45
+ url = "#{@api_url}#{id}/metadata"
46
+ begin
47
+ response = current_token.get(url, :headers => @headers)
48
+ parse_response(response.body)
49
+ rescue => e
50
+ handle_error(e)
51
+ end
52
+ end
53
+
54
+ protected
55
+
56
+ def parse_response(response_body)
57
+ parsed_response = nil
58
+
59
+ if response_body && !response_body.empty?
60
+ begin
61
+ parsed_response = Oj.load(response_body)
62
+ rescue Oj::ParseError
63
+ error = Agx::Error.new("Unparseable response: #{response_body}")
64
+ error.title = "UNPARSEABLE_RESPONSE"
65
+ error.status_code = 500
66
+ raise error
67
+ end
68
+ end
69
+
70
+ parsed_response
71
+ end
72
+
73
+ def handle_error(error)
74
+ error_params = {}
75
+
76
+ begin
77
+ if error.is_a?(OAuth2::Error) && error.response
78
+ error_params[:title] = "HTTP_#{error.response.status}_ERROR"
79
+ error_params[:status_code] = error.response.status
80
+ error_params[:raw_body] = error.response.body
81
+ error_params[:body] = Oj.load(error.response.body)
82
+ elsif error.is_a?(Errno::ETIMEDOUT)
83
+ error_params[:title] = "TIMEOUT_ERROR"
84
+ end
85
+ rescue Oj::ParseError
86
+ end
87
+
88
+ error_to_raise = Agx::Error.new(error.message, error_params)
89
+ raise error_to_raise
90
+ end
91
+
92
+ def current_token
93
+ new_token = OAuth2::AccessToken.new @client, @token[:access_token], {
94
+ expires_at: @token[:expires_at],
95
+ refresh_token: @token[:refresh_token]
96
+ }
97
+ if Time.now.to_i + 90 >= @token[:expires_at]
98
+ new_token = new_token.refresh!
99
+ @token[:access_token] = new_token.token
100
+ @token[:refresh_token] = new_token.refresh_token
101
+ @token[:expires_at] = new_token.expires_at
102
+ end
103
+
104
+ new_token
105
+ end
106
+
107
+ def set_client
108
+ @client = OAuth2::Client.new(
109
+ @client_id,
110
+ @client_secret, {
111
+ site: @site,
112
+ authorize_url: @authorize_url,
113
+ token_url: @token_url,
114
+ options: {
115
+ ssl: { ca_path: "/usr/lib/ssl/certs" }
116
+ }
117
+ }
118
+ )
119
+ end
120
+
121
+ end
122
+ end
123
+ end
@@ -15,7 +15,7 @@ module Agx
15
15
  @host = host || "sync.#{domain}"
16
16
  @authorize_url = "https://auth.#{domain}/identity/connect/Authorize"
17
17
  @token_url = "https://auth.#{domain}/identity/connect/Token"
18
- @oic_config_url = "https://auth.#{domain}/.well-known/openid- configuration"
18
+ @oic_config_url = "https://auth.#{domain}/.well-known/openid-configuration"
19
19
  @version = version || "v4"
20
20
  @sync_id = sync_id
21
21
  @api_url = "#{@site}/api/#{@version}/Account/#{@sync_id}/"
@@ -1,3 +1,3 @@
1
1
  module Agx
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: agx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bryce Johnston
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-05-20 00:00:00.000000000 Z
11
+ date: 2017-08-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oj
@@ -82,7 +82,7 @@ dependencies:
82
82
  version: '3.0'
83
83
  description: Ruby client for accessing SST Software's agX Platform APIs.
84
84
  email:
85
- - bjohnston@cropquest.com
85
+ - bryce@agdeveloper.com
86
86
  executables: []
87
87
  extensions: []
88
88
  extra_rdoc_files: []
@@ -101,10 +101,12 @@ files:
101
101
  - lib/agx/content.rb
102
102
  - lib/agx/content/client.rb
103
103
  - lib/agx/error.rb
104
+ - lib/agx/pictures.rb
105
+ - lib/agx/pictures/client.rb
104
106
  - lib/agx/sync.rb
105
107
  - lib/agx/sync/client.rb
106
108
  - lib/agx/version.rb
107
- homepage: https://github.com/CropQuest/agx-ruby
109
+ homepage: https://github.com/beaorn/agx-ruby
108
110
  licenses:
109
111
  - MIT
110
112
  metadata: {}
@@ -124,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
126
  version: '0'
125
127
  requirements: []
126
128
  rubyforge_project:
127
- rubygems_version: 2.6.8
129
+ rubygems_version: 2.6.13
128
130
  signing_key:
129
131
  specification_version: 4
130
132
  summary: Ruby client for accessing agX Platform APIs.