authentise 0.0.1 → 0.1.0

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: 3a9cba594afbbcd29be2d1b3c1f29627e5ce6a95
4
- data.tar.gz: 36659769fc84a3b1af0a4578476414654ebe0ddf
3
+ metadata.gz: 8f1e56dbfbd3c62ea57126745ef129b508795a75
4
+ data.tar.gz: a67c2a69f9ee8dad71424f044af0946a720eb839
5
5
  SHA512:
6
- metadata.gz: e34a7b309ccc01ff79c2303f91e25ed6030a3987ef9b81c46bf5adcfd1747c635580aecd1395d7eabf943a78fc66d731d9b664f16f8bd45796696abc3e3e7d01
7
- data.tar.gz: 06b4505e6f8b8ce98e7079ef647541bf0064bcb1538eb35fa3f63a028d6d52df5651cd32686cad7b2e784d86b75786e4f6efe72806dcbf4def069241bd6056d1
6
+ metadata.gz: 499c0b034752c6177b9ca3ab58fdc7f65408ab543171dc26f4df3866ffe0fb170713280b2d5fda6a0018d1d440bfca48bfbcb29ba73853342e938b53e2118029
7
+ data.tar.gz: 3c3fe5cf2b1dc67db778e6e076e81d775107d96cb946adceebb582825901b5c258bc6f0bf4798aa7a59a8e6c69cd5598c785aa1ebf77267f8e8bfb34e81d1f9f
data/README.md CHANGED
@@ -1,48 +1,83 @@
1
1
  Ruby gem to access Authentise API v3
2
2
  ====================================
3
3
 
4
- See http://docs.dev-auth.com/
4
+ See http://docs.authentise.com/model/reference.html
5
5
 
6
- Example usage
6
+ Install
7
+ -------
8
+
9
+ Add the following line to your Gemfile:
10
+
11
+ ```rb
12
+ gem "authentise"
13
+ ```
14
+
15
+
16
+ Configuration
7
17
  -------------
8
18
 
19
+ Set your secret partner key:
20
+
9
21
  ```rb
10
22
  Authentise.configure do |c|
11
23
  c.secret_partner_key = "ZSBzaG9y-dCB2ZWhl-bWVuY2Ug-b2YgYW55-IGNhcm5h-bCB=="
12
- c.use_ssl = false
13
24
  end
25
+ ```
14
26
 
15
- upload = Authentise::Upload.new(
16
- stl_file: File.new("example.stl", "rb"),
17
- email: "example@example.com",
18
- cents: 2_00,
19
- currency: "EUR"
20
- )
27
+ Usage
28
+ ------
21
29
 
22
- upload.token
23
- # => "33b41d6e80d4918cfff768185d1d31a6"
30
+ ### Authentication
24
31
 
25
- upload.link_url
26
- # => "https://widget.sendshapes.com/?token=33b41d6e80d4918cfff768185d1d31a6"
32
+ ```rb
33
+ # Create a user
34
+ user = Authentise::User.new(
35
+ email: 'you@example.com',
36
+ name: 'You',
37
+ username: 'you',
38
+ password: 'p4ssw0rd;99'
39
+ )
40
+ user.create
27
41
 
28
- upload.status
29
- # => {
30
- # printing_job_status_name: "warming_up",
31
- # printing_percentage: 0,
32
- # minutes_left: 21,
33
- # message: ""
34
- # }
42
+ # Create a session
43
+ session = Authentise::Session.new(
44
+ username: 'you',
45
+ password: 'p4ssw0rd;99'
46
+ )
47
+ session.create
35
48
  ```
36
49
 
37
- Install
38
- -------
39
-
40
- Add the following line to your Gemfile:
50
+ ### Model Warehouse
41
51
 
42
52
  ```rb
43
- gem "authentise"
53
+ # Create a model
54
+ model = Authentise::Model.new(name: "My model")
55
+ model.create(session_token: session.token)
56
+ model.send_file(path: 'example.stl')
57
+ model.url # => "https://models.authentise.com/model/42424…"
58
+
59
+ # Get a model
60
+ model = Authentise::Model.find_by_url(
61
+ url: "https://models.authentise.com/model/42424…",
62
+ session_token: session.token
63
+ )
64
+ model.name # => "My model"
65
+ model.status # => "processing"
66
+ model.content_url # => ""https://prod-hoth-models.s3.amazonaws.com:443/07c74a…"
44
67
  ```
45
68
 
69
+ ### Streaming iframe
70
+
71
+ ```rb
72
+ # Create a print
73
+ print = Authentise::Print.new(
74
+ model_url: "https://models.authentise.com/model/42424…"
75
+ )
76
+
77
+ # Show an iframe to this URL to the user
78
+ print.url
79
+ # => "https://widget.sendshapes.com/?token=33b41d6e80d4918cfff768185d1d31a6"
80
+
46
81
 
47
82
  Development
48
83
  -----------
@@ -59,6 +94,11 @@ To launch specs:
59
94
  $ rake
60
95
  ```
61
96
 
97
+ List other tasks:
98
+
99
+ ```sh
100
+ $ rake -T
101
+ ```
62
102
 
63
103
  License
64
104
  -------
@@ -0,0 +1,38 @@
1
+ require "authentise/api"
2
+
3
+ module Authentise
4
+ module API
5
+ # Calls to the print streaming API
6
+ module Print
7
+ module_function
8
+
9
+ def create_token(receiver_email: nil,
10
+ model_url: nil,
11
+ print_value: nil,
12
+ print_value_currency: nil,
13
+ partner_job_id: nil)
14
+ url = "https://print.authentise.com/token/"
15
+ body = {
16
+ api_key: Authentise.configuration.secret_partner_key,
17
+ model: model_url,
18
+ receiver_email: receiver_email,
19
+ print_value: print_value,
20
+ print_value_currency: print_value_currency,
21
+ partner_job_id: partner_job_id,
22
+ }.to_json
23
+ options = {
24
+ content_type: :json,
25
+ accept: :json,
26
+ }
27
+ RestClient.post(url, body, options) do |response, _request, _result|
28
+ if response.code == 201
29
+ { url: response.headers[:x_token_location] }
30
+ else
31
+ fail API::Error, response
32
+ end
33
+ end
34
+ end
35
+
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,64 @@
1
+ require "authentise/api"
2
+
3
+ module Authentise
4
+ module API
5
+ # Calls to create a user and authenticate over the API
6
+ module Users
7
+ module_function
8
+
9
+ # Create a new user to use the API.
10
+ #
11
+ # Params:
12
+ # - email
13
+ # - name
14
+ # - username
15
+ # - password
16
+ #
17
+ # Returns a hash with:
18
+ # - url: URL to the new user
19
+ # - uuid: unique id for this user
20
+ def create_user(params)
21
+ url = "https://users.authentise.com/users/"
22
+ options = {
23
+ accept: :json,
24
+ }
25
+ RestClient.post(url, params, options) do |response, _request, _result|
26
+ json = JSON.parse(response)
27
+ if response.code == 201
28
+ {
29
+ url: json["uri"],
30
+ uuid: json["uuid"],
31
+ }
32
+ else
33
+ fail API::Error, json["message"]
34
+ end
35
+ end
36
+ end
37
+
38
+ # Create a new session to use in other API calls.
39
+ #
40
+ # Params:
41
+ # - username
42
+ # - password
43
+ #
44
+ # Returns a hash with:
45
+ # - token: cookie token to add to the following API cooke calls
46
+ def create_session(params)
47
+ url = "https://users.authentise.com/sessions/"
48
+ options = {
49
+ accept: :json,
50
+ }
51
+ RestClient.post(url, params, options) do |response, _request, _result|
52
+ if response.code == 201
53
+ {
54
+ token: response.cookies["session"],
55
+ }
56
+ else
57
+ json = JSON.parse(response)
58
+ fail API::Error, json["message"]
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,258 @@
1
+ require "authentise/api"
2
+
3
+ module Authentise
4
+ module API
5
+ # Upload models and take snapshot pictures
6
+ module Warehouse
7
+ module_function
8
+
9
+ # Params:
10
+ # - cookie
11
+ #
12
+ # - name (string) – Required. The name of the model. This can be any
13
+ # string and should be meaningful to the user
14
+ #
15
+ # - allowed_transformations (hash) – Optional. The transformations that
16
+ # are allowed on this model.
17
+ # * resize (boolean) – Optional. true if this
18
+ # model is allowed to be resized automatically by other services.
19
+ # Default: false.
20
+ # * rotation (boolean) – Optional. true if
21
+ # this model is allowed to be rotated automatically by other services.
22
+ # Default: false.
23
+ #
24
+ # - callback (hash) – Optional. The URL to call when this model changes
25
+ # states.
26
+ # * url (string) – Optional. The URL to request for the
27
+ # callback
28
+ # * method (string) – Optional. The method to use for the
29
+ # request, one of GET, POST or PUT.
30
+ def create_model(session_token: nil, name: nil)
31
+ url = "https://models.authentise.com/model/"
32
+ body = {
33
+ name: name,
34
+ }.to_json
35
+ options = {
36
+ content_type: :json,
37
+ accept: :json,
38
+ cookies: { session: session_token },
39
+ }
40
+ RestClient.post(url, body, options) do |response, _request, _result|
41
+ if response.code == 201
42
+ {
43
+ model_url: response.headers[:location],
44
+ upload_url: response.headers[:x_upload_location],
45
+ }
46
+ else
47
+ fail API::Error, JSON.parse(response)["message"]
48
+ end
49
+ end
50
+ end
51
+
52
+ def put_file(url: nil, path: nil)
53
+ file = File.read(path)
54
+ options = {
55
+ content_type: 'application/octet-stream',
56
+ }
57
+ RestClient.put(url, file, options) do |response, _request, _result|
58
+ if response.code == 200
59
+ true
60
+ else
61
+ fail API::Error, response
62
+ end
63
+ end
64
+ end
65
+
66
+ # Get information about a model from its URL or UUID.
67
+ def get_model(url: nil, uuid: nil, session_token: nil)
68
+ url ||= "https://models.authentise.com/model/#{uuid}/"
69
+ headers = {
70
+ content_type: :json,
71
+ accept: :json,
72
+ cookies: { session: session_token },
73
+ }
74
+ RestClient.get(url, headers) do |response, _request, _result|
75
+ if response.code == 200
76
+ parse_model(response, url)
77
+ elsif response.code == 404
78
+ fail Authentise::API::NotFoundError
79
+ else
80
+ fail Authentise::API::Error, "Error #{response.code}"
81
+ end
82
+ end
83
+ end
84
+
85
+
86
+ # # Get a list of all models the requester has access to based on
87
+ # # query filters.
88
+ # #
89
+ # # Params:
90
+ # # - session_token
91
+ # # - name: a partial name of models to search for. accepts the
92
+ # # wildcard character: “*”.
93
+ # # - status: a status of models to search for.
94
+ # # - created: a creation date to search for models.
95
+ # # - updated: a updated date to search for models.
96
+ # # - sort – one of the other queryable parameters, such as name,
97
+ # # status, created or updated. accepts + or - to indicate order of
98
+ # # the sort. parameters may be strung together, separated by commas.
99
+ # # example: "+status, +created, -name" or "name, created"
100
+ # def get_models(params = {})
101
+ # query = params.dup
102
+ # session_token = query.delete(:session_token)
103
+
104
+ # url = "https://models.authentise.com/model/"
105
+ # options = {
106
+ # params: query,
107
+ # content_type: :json,
108
+ # accept: :json,
109
+ # cookies: { session: session_token }
110
+ # }
111
+
112
+ # RestClient.get(url, options) do |response, request, result|
113
+ # if response.code == 200
114
+ # data = JSON.parse(response)
115
+ # p data
116
+ # {
117
+ # # ?
118
+ # models: data["models"]
119
+ # }
120
+ # else
121
+ # raise Authentise::API::Error, "Error #{response.code}"
122
+ # end
123
+ # end
124
+ # end
125
+
126
+
127
+ # Create a model snapshot.
128
+ #
129
+ # Required arguments:
130
+ # - session_token: for authentication.
131
+ # - model_uuid: which model to create a snapshot for.
132
+ #
133
+ # Optional arguments:
134
+ # - samples (int) – The number of samples to use in requesting the
135
+ # snapshot. Min 0, Max 5000. Higher numbers will take
136
+ # konger but yield better-looking results
137
+ # - layer (int) – The number of the layer of the model to show. This
138
+ # allows you to visualize the model when partially printed
139
+ # - color (string) – The color, in HTML color codes, to use for the
140
+ # material the model is made of. Ex: #AFAA75
141
+ # - height (int) – The height of the image in pixels. Min 0, Max 768
142
+ # - width (int) – The width oft he image in pixels. Min 0, Max 1024
143
+ # - x (float) – The position of the camera on the X axis
144
+ # - y (float) – The position of the camera on the Y axis
145
+ # - z (float) – The position of the camera on the Z axis
146
+ # - u (float) – The camera direction vector’s X component
147
+ # - v (float) – The camera direction vector’s Y component
148
+ # - w (float) – The camera direction vector’s Z component
149
+ # - callback (hash):
150
+ # * url (string) – The url to callback to once model processing is
151
+ # finished.
152
+ # * method (string) – The http method for the callback to use when
153
+ # calling back.
154
+ def create_snapshot(arguments = {})
155
+ params = arguments.dup
156
+ session_token = params.delete(:session_token)
157
+ model_uuid = params.delete(:model_uuid)
158
+
159
+ url = "https://models.authentise.com/model/#{model_uuid}/snapshot/"
160
+ body = params.to_json
161
+ headers = {
162
+ content_type: :json,
163
+ accept: :json,
164
+ cookies: { session: session_token },
165
+ }
166
+ RestClient.post(url, body, headers) do |response, _request, _result|
167
+ if response.code == 201
168
+ {
169
+ url: response.headers[:location],
170
+ }
171
+ else
172
+ fail API::Error, JSON.parse(response)["message"]
173
+ end
174
+ end
175
+ end
176
+
177
+
178
+ # Get information about a snapshot from its URL.
179
+ def get_snapshot(url: nil, session_token: nil)
180
+ headers = {
181
+ content_type: :json,
182
+ accept: :json,
183
+ cookies: { session: session_token },
184
+ }
185
+ RestClient.get(url, headers) do |response, _request, _result|
186
+ if response.code == 200
187
+ parse_snapshot(response)
188
+ elsif response.code == 404
189
+ fail Authentise::API::NotFoundError
190
+ else
191
+ fail Authentise::API::Error, "Error #{response.code}"
192
+ end
193
+ end
194
+ end
195
+
196
+
197
+ def parse_model(response, url)
198
+ data = JSON.parse(response)
199
+ {
200
+ # URL to fetch this model
201
+ url: url,
202
+ # Identifier for the model
203
+ uuid: url.split("/").last,
204
+ # The name of the model. (string)
205
+ name: data["name"],
206
+ # The current status of the model processing. Can be one of
207
+ # "processing", "processed", or "error".
208
+ status: data["status"],
209
+ # Link at which a snapshot of the model can be downloaded.
210
+ snapshot_url: data["snapshot"],
211
+ # Link at which a the model can be downloaded.
212
+ content_url: data["content"],
213
+ # Boolean represeting if the model is manifold. If the model is
214
+ # not manifold, there is a higher likelyhood that slicing will
215
+ # fail.
216
+ manifold: data["analyses.manifold"],
217
+ # The date and time the model was created.
218
+ created_at: parse_time(data["created"]),
219
+ # The date and time the model was last updated.
220
+ updated_at: parse_time(data["updated"]),
221
+ # An array of model uris from which this model is derived.
222
+ parents_urls: data["parents"],
223
+ # An array of model uris from which are derived from this model.
224
+ children_urls: data["children"],
225
+ }
226
+ end
227
+ private_class_method :parse_model
228
+
229
+ # rubocop:disable Metrics/AbcSize
230
+ def parse_snapshot(response)
231
+ data = JSON.parse(response)
232
+ {
233
+ status: "snapshot_rendering",
234
+ samples: data["samples"],
235
+ layer: data["layer"],
236
+ color: data["color"],
237
+ height: data["height"],
238
+ width: data["width"],
239
+ x: data["x"],
240
+ y: data["y"],
241
+ z: data["z"],
242
+ u: data["u"],
243
+ v: data["v"],
244
+ w: data["w"],
245
+ slice_height: data["slice_height"],
246
+ created_at: parse_time(data["created"]),
247
+ content_url: data["content"],
248
+ }
249
+ end
250
+ # rubocop:enable Metrics/AbcSize
251
+
252
+ def parse_time(string)
253
+ string && Time.parse(string)
254
+ end
255
+ private_class_method :parse_time
256
+ end
257
+ end
258
+ end
@@ -3,34 +3,41 @@ require "rest-client"
3
3
  require "authentise/configuration"
4
4
 
5
5
  module Authentise
6
+ # Module for all API-Related
6
7
  module API
8
+ # Common Error
7
9
  class Error < RuntimeError; end
8
10
 
9
- CREATE_TOKEN_PATH = "api3/api_create_partner_token"
10
- UPLOAD_FILE_PATH = "api3/api_upload_partner_stl"
11
- STATUS_PATH = "api3/api_get_partner_print_status"
11
+ # 404 Error
12
+ class NotFoundError < Error; end
12
13
 
13
14
  module_function
14
15
 
16
+ # DEPRECATED, use Authentise::API::Print.create_token(…)
15
17
  def create_token
16
- url = "#{host}/#{CREATE_TOKEN_PATH}"
18
+ url = "#{host}/api3/api_create_partner_token"
17
19
  params = {
18
- api_key: Authentise.configuration.secret_partner_key
20
+ api_key: Authentise.configuration.secret_partner_key,
19
21
  }
20
22
  response = RestClient.get(url, params: params)
21
23
  data = parse(response)
22
24
  data["token"]
23
25
  end
24
26
 
25
- def upload_file(file: nil, token: nil, email: nil, cents: nil, currency: "USD")
26
- url = "#{host}/#{UPLOAD_FILE_PATH}"
27
+ # DEPRECATED, use Authentise::API::Print.create_token(…)
28
+ def upload_file(token: nil,
29
+ file: nil,
30
+ email: nil,
31
+ cents: nil,
32
+ currency: "USD")
33
+ url = "#{host}/api3/api_upload_partner_stl"
27
34
  params = {
28
35
  api_key: Authentise.configuration.secret_partner_key,
29
36
  token: token,
30
37
  receiver_email: email,
31
38
  print_value: cents,
32
39
  print_value_currency: currency,
33
- stl_file: file
40
+ stl_file: file,
34
41
  }
35
42
  response = RestClient.post(url, params, accept: :json)
36
43
  data = parse(response)
@@ -42,6 +49,7 @@ module Authentise
42
49
  end
43
50
  end
44
51
 
52
+ # DEPRECATED
45
53
  # Returns a status hash for the given token if the print has started.
46
54
  # /!\ Do not call this more than once every 15 seconds.
47
55
  #
@@ -53,10 +61,10 @@ module Authentise
53
61
  # - `confirmed_success`
54
62
  # - `confirmed_failure`
55
63
  def get_status(token: nil)
56
- url = "#{host}/#{STATUS_PATH}"
64
+ url = "#{host}/api3/api_get_partner_print_status"
57
65
  params = {
58
66
  api_key: Authentise.configuration.secret_partner_key,
59
- token: token
67
+ token: token,
60
68
  }
61
69
  response = RestClient.get(url, params: params)
62
70
  data = parse(response)
@@ -64,15 +72,15 @@ module Authentise
64
72
  printing_job_status_name: data["printing_job_status_name"].downcase,
65
73
  printing_percentage: data["printing_percentage"],
66
74
  minutes_left: data["minutes_left"],
67
- message: data["message"]
75
+ message: data["message"],
68
76
  }
69
77
  end
70
78
 
71
- private_class_method
79
+ # private
72
80
 
73
81
  def parse(response)
74
82
  json = JSON.parse(response)
75
- if json["status"] and json["status"]["code"] != "ok"
83
+ if json["status"] && json["status"]["code"] != "ok"
76
84
  fail Error, json["status"]["extended_description"]
77
85
  elsif json["data"]
78
86
  json["data"]
@@ -80,6 +88,7 @@ module Authentise
80
88
  fail Error, "JSON with no data: #{response}"
81
89
  end
82
90
  end
91
+ private_class_method :parse
83
92
 
84
93
  def host
85
94
  if Authentise.configuration.use_ssl
@@ -88,5 +97,6 @@ module Authentise
88
97
  "http://widget.sendshapes.com:3000"
89
98
  end
90
99
  end
100
+ private_class_method :host
91
101
  end
92
102
  end
@@ -1,6 +1,16 @@
1
+ # Configure your application using this construct:
2
+ #
3
+ # Authentise.configure do |c|
4
+ # c.secret_partner_key = "…"
5
+ # end
1
6
  module Authentise
7
+ # Store the config
2
8
  class Configuration
9
+ # The partner key Authentise gave you
3
10
  attr_accessor :secret_partner_key
11
+
12
+ # DEPRECATED
13
+ # Switch off the use of SSL for the old streaming API
4
14
  attr_accessor :use_ssl
5
15
 
6
16
  def initialize
@@ -0,0 +1,88 @@
1
+ require "authentise/api/warehouse"
2
+
3
+ module Authentise
4
+ # Represents a model in the Model Warehouse
5
+ class Model
6
+ attr_accessor :name, # Required
7
+
8
+ # Available only when model is created or fetched
9
+ :url,
10
+
11
+ # Available only when model is created
12
+ :upload_url,
13
+
14
+ # Available only when model is fetched
15
+ :status,
16
+ :snapshot_url,
17
+ :content_url,
18
+ :manifold,
19
+ :created_at,
20
+ :updated_at,
21
+ :parents_urls,
22
+ :children_urls,
23
+
24
+ # You can initialize a model with it but it is not available
25
+ # when fetched
26
+ :uuid
27
+
28
+
29
+ def initialize(name: nil,
30
+ url: nil,
31
+ upload_url: nil,
32
+ uuid: nil)
33
+ @name = name
34
+ @upload_url = upload_url
35
+ @url = url
36
+ @uuid = uuid
37
+ end
38
+
39
+ def create(session_token: nil)
40
+ response = API::Warehouse.create_model(
41
+ session_token: session_token,
42
+ name: name,
43
+ )
44
+ @upload_url = response[:upload_url]
45
+ @url = response[:model_url]
46
+ true
47
+ end
48
+
49
+ def send_file(path: nil)
50
+ API::Warehouse.put_file(
51
+ url: upload_url,
52
+ path: path,
53
+ )
54
+ end
55
+
56
+ # rubocop:disable Metrics/AbcSize
57
+ def fetch(session_token: nil)
58
+ response = API::Warehouse.get_model(uuid: uuid,
59
+ url: url,
60
+ session_token: session_token)
61
+ @url = response[:url]
62
+ @uuid = response[:uuid]
63
+ @name = response[:name]
64
+ @status = response[:status]
65
+ @snapshot_url = response[:snapshot_url]
66
+ @content_url = response[:content_url]
67
+ @manifold = response[:manifold]
68
+ @parents_urls = response[:parents_urls]
69
+ @children_urls = response[:children_urls]
70
+ @created_at = response[:created_at]
71
+ @updated_at = response[:updated_at]
72
+ true
73
+ end
74
+ # rubocop:enable Metrics/AbcSize
75
+
76
+ def self.find_by_url(url: nil, session_token: nil)
77
+ model = new(url: url)
78
+ model.fetch(session_token: session_token)
79
+ model
80
+ end
81
+
82
+ def self.find_by_uuid(uuid: nil, session_token: nil)
83
+ model = new(uuid: uuid)
84
+ model.fetch(session_token: session_token)
85
+ model
86
+ end
87
+ end
88
+ end