authentise 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3a9cba594afbbcd29be2d1b3c1f29627e5ce6a95
4
+ data.tar.gz: 36659769fc84a3b1af0a4578476414654ebe0ddf
5
+ SHA512:
6
+ metadata.gz: e34a7b309ccc01ff79c2303f91e25ed6030a3987ef9b81c46bf5adcfd1747c635580aecd1395d7eabf943a78fc66d731d9b664f16f8bd45796696abc3e3e7d01
7
+ data.tar.gz: 06b4505e6f8b8ce98e7079ef647541bf0064bcb1538eb35fa3f63a028d6d52df5651cd32686cad7b2e784d86b75786e4f6efe72806dcbf4def069241bd6056d1
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Sunny Ripert
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,67 @@
1
+ Ruby gem to access Authentise API v3
2
+ ====================================
3
+
4
+ See http://docs.dev-auth.com/
5
+
6
+ Example usage
7
+ -------------
8
+
9
+ ```rb
10
+ Authentise.configure do |c|
11
+ c.secret_partner_key = "ZSBzaG9y-dCB2ZWhl-bWVuY2Ug-b2YgYW55-IGNhcm5h-bCB=="
12
+ c.use_ssl = false
13
+ end
14
+
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
+ )
21
+
22
+ upload.token
23
+ # => "33b41d6e80d4918cfff768185d1d31a6"
24
+
25
+ upload.link_url
26
+ # => "https://widget.sendshapes.com/?token=33b41d6e80d4918cfff768185d1d31a6"
27
+
28
+ upload.status
29
+ # => {
30
+ # printing_job_status_name: "warming_up",
31
+ # printing_percentage: 0,
32
+ # minutes_left: 21,
33
+ # message: ""
34
+ # }
35
+ ```
36
+
37
+ Install
38
+ -------
39
+
40
+ Add the following line to your Gemfile:
41
+
42
+ ```rb
43
+ gem "authentise"
44
+ ```
45
+
46
+
47
+ Development
48
+ -----------
49
+
50
+ For local development, please install the `bundler` gem then:
51
+
52
+ ```sh
53
+ $ bundle
54
+ ```
55
+
56
+ To launch specs:
57
+
58
+ ```sh
59
+ $ rake
60
+ ```
61
+
62
+
63
+ License
64
+ -------
65
+
66
+ Created by Sunny Ripert for [Cults.](https://cults3d.com),
67
+ licensed under the MIT License.
@@ -0,0 +1,11 @@
1
+ # Bundler
2
+ require 'bundler/gem_tasks'
3
+
4
+ # Specs
5
+ require "rake/testtask"
6
+ Rake::TestTask.new do |t|
7
+ t.libs << "spec"
8
+ t.test_files = Dir.glob("spec/**/*_spec.rb")
9
+ end
10
+
11
+ task default: :test
@@ -0,0 +1 @@
1
+ require "authentise/upload"
@@ -0,0 +1,92 @@
1
+ require "json"
2
+ require "rest-client"
3
+ require "authentise/configuration"
4
+
5
+ module Authentise
6
+ module API
7
+ class Error < RuntimeError; end
8
+
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"
12
+
13
+ module_function
14
+
15
+ def create_token
16
+ url = "#{host}/#{CREATE_TOKEN_PATH}"
17
+ params = {
18
+ api_key: Authentise.configuration.secret_partner_key
19
+ }
20
+ response = RestClient.get(url, params: params)
21
+ data = parse(response)
22
+ data["token"]
23
+ end
24
+
25
+ def upload_file(file: nil, token: nil, email: nil, cents: nil, currency: "USD")
26
+ url = "#{host}/#{UPLOAD_FILE_PATH}"
27
+ params = {
28
+ api_key: Authentise.configuration.secret_partner_key,
29
+ token: token,
30
+ receiver_email: email,
31
+ print_value: cents,
32
+ print_value_currency: currency,
33
+ stl_file: file
34
+ }
35
+ response = RestClient.post(url, params, accept: :json)
36
+ data = parse(response)
37
+
38
+ if Authentise.configuration.use_ssl
39
+ data["ssl_token_link"]
40
+ else
41
+ data["token_link"]
42
+ end
43
+ end
44
+
45
+ # Returns a status hash for the given token if the print has started.
46
+ # /!\ Do not call this more than once every 15 seconds.
47
+ #
48
+ # `:printing_job_status` can be one of:
49
+ # - `warming_up`
50
+ # - `printing`
51
+ # - `failure`
52
+ # - `success`
53
+ # - `confirmed_success`
54
+ # - `confirmed_failure`
55
+ def get_status(token: nil)
56
+ url = "#{host}/#{STATUS_PATH}"
57
+ params = {
58
+ api_key: Authentise.configuration.secret_partner_key,
59
+ token: token
60
+ }
61
+ response = RestClient.get(url, params: params)
62
+ data = parse(response)
63
+ {
64
+ printing_job_status_name: data["printing_job_status_name"].downcase,
65
+ printing_percentage: data["printing_percentage"],
66
+ minutes_left: data["minutes_left"],
67
+ message: data["message"]
68
+ }
69
+ end
70
+
71
+ private_class_method
72
+
73
+ def parse(response)
74
+ json = JSON.parse(response)
75
+ if json["status"] and json["status"]["code"] != "ok"
76
+ fail Error, json["status"]["extended_description"]
77
+ elsif json["data"]
78
+ json["data"]
79
+ else
80
+ fail Error, "JSON with no data: #{response}"
81
+ end
82
+ end
83
+
84
+ def host
85
+ if Authentise.configuration.use_ssl
86
+ "https://widget.sendshapes.com:3443"
87
+ else
88
+ "http://widget.sendshapes.com:3000"
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,19 @@
1
+ module Authentise
2
+ class Configuration
3
+ attr_accessor :secret_partner_key
4
+ attr_accessor :use_ssl
5
+
6
+ def initialize
7
+ @use_ssl = true
8
+ end
9
+ end
10
+
11
+ class << self
12
+ attr_accessor :configuration
13
+ end
14
+
15
+ def self.configure
16
+ self.configuration ||= Configuration.new
17
+ yield(configuration)
18
+ end
19
+ end
@@ -0,0 +1,35 @@
1
+ require "authentise/api"
2
+
3
+ module Authentise
4
+ class Upload
5
+ attr_reader :stl_file, :email, :cents, :currency
6
+
7
+ def initialize(stl_file: nil,
8
+ email: nil,
9
+ cents: nil,
10
+ currency: nil,
11
+ token: nil)
12
+ @stl_file = stl_file
13
+ @email = email
14
+ @cents = cents
15
+ @currency = currency
16
+ @token = token
17
+ end
18
+
19
+ def token
20
+ @token ||= API.create_token
21
+ end
22
+
23
+ def link_url
24
+ @upload_url ||= API.upload_file(file: stl_file,
25
+ token: token,
26
+ email: email,
27
+ cents: cents,
28
+ currency: currency)
29
+ end
30
+
31
+ def status
32
+ @status ||= API.get_status(token: token)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module Authentise
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,89 @@
1
+ require "spec_helper"
2
+
3
+ describe Authentise::API do
4
+ describe ".create_token" do
5
+ it "returns a token from the API" do
6
+ stub_request(:get, "https://widget.sendshapes.com:3443/api3/api_create_partner_token?api_key=test")
7
+ .to_return(status: 200, body: '{"data":{"token":"meh"}}')
8
+
9
+ Authentise::API.create_token.must_equal "meh"
10
+ end
11
+ end
12
+
13
+ describe ".upload_file" do
14
+ it "returns a URL" do
15
+ api_url = "https://widget.sendshapes.com:3443/api3/api_upload_partner_stl"
16
+ request_body = { "api_key" => "test",
17
+ "print_value" => "100",
18
+ "print_value_currency" => "EUR",
19
+ "receiver_email" => "example@example.com",
20
+ "stl_file" => "",
21
+ "token"=>"meh"}
22
+ request_headers = { "Accept" => "application/json",
23
+ "Accept-Encoding" => "gzip, deflate",
24
+ "Content-Length" => "110",
25
+ "Content-Type" => "application/x-www-form-urlencoded",
26
+ "User-Agent" => "Ruby"}
27
+
28
+ response_body = '{"data":{"ssl_token_link":"https://bah"}}'
29
+
30
+ stub_request(:post, api_url)
31
+ .with(body: request_body, headers: request_headers)
32
+ .to_return(status: 200, body: response_body)
33
+
34
+ returned = Authentise::API.upload_file(file: nil,
35
+ token: "meh",
36
+ email: "example@example.com",
37
+ cents: 100,
38
+ currency: "EUR")
39
+ returned.must_equal "https://bah"
40
+ end
41
+
42
+ it "uses the non-ssl link if use_ssl is false" do
43
+ api_url = "http://widget.sendshapes.com:3000/api3/api_upload_partner_stl"
44
+ stub_request(:post, api_url)
45
+ .to_return(body: '{"data":{"token_link":"http://bah"}}')
46
+
47
+ Authentise.configuration.stub :use_ssl, false do
48
+ returned = Authentise::API.upload_file(file: nil,
49
+ token: "meh",
50
+ email: "example@example.com",
51
+ cents: 100,
52
+ currency: "EUR")
53
+ returned.must_equal "http://bah"
54
+ end
55
+ end
56
+ end
57
+
58
+ describe ".get_status" do
59
+ it "returns a status hash" do
60
+ api_url = "https://widget.sendshapes.com:3443/api3/api_get_partner_print_status?api_key=test&token=meh"
61
+ request_body = { "api_key" => "test",
62
+ "print_value" => "100",
63
+ "print_value_currency" => "EUR",
64
+ "receiver_email" => "example@example.com",
65
+ "stl_file" => "",
66
+ "token"=>"meh"}
67
+ response_body = {
68
+ status: { code: "ok" },
69
+ data: {
70
+ printing_job_status_name: "WARMING_UP",
71
+ printing_percentage: 20,
72
+ minutes_left: nil,
73
+ message: nil,
74
+ }
75
+ }
76
+
77
+ stub_request(:get, api_url)
78
+ .to_return(status: 200, body: response_body.to_json)
79
+
80
+ returned = Authentise::API.get_status(token: "meh")
81
+ returned.must_equal(
82
+ printing_job_status_name: "warming_up",
83
+ printing_percentage: 20,
84
+ minutes_left: nil,
85
+ message: nil
86
+ )
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,8 @@
1
+ require "minitest/autorun"
2
+ require "webmock/minitest"
3
+
4
+ require "authentise"
5
+
6
+ Authentise.configure do |c|
7
+ c.secret_partner_key = "test"
8
+ end
@@ -0,0 +1,37 @@
1
+ require "spec_helper"
2
+
3
+ describe Authentise::Upload do
4
+ let(:upload) {
5
+ Authentise::Upload.new(email: "example@example.com",
6
+ currency: "EUR",
7
+ cents: 1_00)
8
+ }
9
+
10
+ describe "#token" do
11
+ it "returns a token from the API" do
12
+ Authentise::API.stub :create_token, "meh" do
13
+ upload.token.must_equal "meh"
14
+ end
15
+ end
16
+ end
17
+
18
+ describe '#link_url' do
19
+ it "returns a token from the API" do
20
+ upload.stub :token, "meh" do
21
+ Authentise::API.stub :upload_file, "https://bah" do
22
+ upload.link_url.must_equal "https://bah"
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ describe '#status' do
29
+ it "returns a status from the API" do
30
+ upload.stub :token, "meh" do
31
+ Authentise::API.stub :upload_file, "stats…" do
32
+ upload.link_url.must_equal "stats…"
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: authentise
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sunny Ripert
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: webmock
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Access Authentise's 3D-Printing streaming API
56
+ email:
57
+ - sunny@sunfox.org
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - lib/authentise.rb
66
+ - lib/authentise/api.rb
67
+ - lib/authentise/configuration.rb
68
+ - lib/authentise/upload.rb
69
+ - lib/authentise/version.rb
70
+ - spec/api_spec.rb
71
+ - spec/spec_helper.rb
72
+ - spec/upload_spec.rb
73
+ homepage: http://github.com/sunny/authentise
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.4.5
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Access the Authentise v3 API
97
+ test_files:
98
+ - spec/api_spec.rb
99
+ - spec/spec_helper.rb
100
+ - spec/upload_spec.rb