imaterialise 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 226e89bd1af711176ae72e57a13fb7e1bcbd5431
4
+ data.tar.gz: 064da1ccfe88098f95180b4aed7dc77718022224
5
+ SHA512:
6
+ metadata.gz: 7ad7bd946d55522de53be420b4bc985ffe78246c96b2dbae0c0d3c02be4bfb822ac856cf13c6bcbf5910021fdc5e352e37ce3f1b3406d93d0b2bf914ed60e5dd
7
+ data.tar.gz: 32d835cd021cdb6e7b5a8a61d08d9866bd3a3b390e1ad5aedb69aa6a288bc170d84ad95dc7fb6547d24cfd3416cfe37f4ed666c8dd429cab3737b60cb46ca540
@@ -0,0 +1,20 @@
1
+ Copyright 2016 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,61 @@
1
+ Ruby gem to access i.materialise's API
2
+ ======================================
3
+
4
+ Configuration
5
+ -------------
6
+
7
+ ```rb
8
+ Imaterialise.configure do |c|
9
+ c.tool_id = "YOUR-TOOL-ID-HERE"
10
+
11
+ # Use the sandbox environment during development
12
+ c.sandbox = true
13
+ end
14
+ ```
15
+
16
+ Usage
17
+ -----
18
+
19
+ ### 3D Print Lab Connection
20
+
21
+ ```rb
22
+ model = Imaterialise::Connection.new(file_url: "/path/to/example.stl"),
23
+ currency: "eur")
24
+ model.upload
25
+ model.url # => "http://i.materialise.com/UploadRouter?modelID=1234-5678-1234-1…"
26
+ ```
27
+
28
+ You can redirect your users to the given URL.
29
+
30
+
31
+ Install
32
+ -------
33
+
34
+ Add the following line to your Gemfile if you are using the `bundler` gem:
35
+
36
+ ```rb
37
+ gem "imaterialise", github: "sunny/imaterialise"
38
+ ```
39
+
40
+
41
+ Development
42
+ -----------
43
+
44
+ To launch specs:
45
+
46
+ ```sh
47
+ $ rake
48
+ ```
49
+
50
+
51
+ License
52
+ -------
53
+
54
+ Created by Sunny Ripert for [Cults.](https://cults3d.com),
55
+ licensed under the MIT License.
56
+
57
+
58
+ See also
59
+ --------
60
+
61
+ - https://i.materialise.com/api/
@@ -0,0 +1,14 @@
1
+ # Bundler
2
+ require "bundler/gem_tasks"
3
+
4
+ # Specs
5
+ require "rspec/core/rake_task"
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ # Console
9
+ desc "Open a pry session preloaded with this library"
10
+ task :console do
11
+ sh "pry --gem -r ./console.rb"
12
+ end
13
+
14
+ task default: :spec
@@ -0,0 +1,11 @@
1
+ require "pry"
2
+ require "net-http-spy"
3
+ Net::HTTP.http_logger_options = { verbose: true }
4
+
5
+ require "imaterialise"
6
+
7
+ Imaterialise.configure do |c|
8
+ c.api_code = "test-api-code"
9
+ c.tool_id = "test-tool-id"
10
+ c.sandbox = true
11
+ end
@@ -0,0 +1,2 @@
1
+ require "imaterialise/configuration"
2
+ require "imaterialise/connection"
@@ -0,0 +1,29 @@
1
+ require "rest_client"
2
+
3
+ module Imaterialise
4
+ # Namespace for the API.
5
+ module API
6
+ # JSON HTTP errors from the API.
7
+ class APIError < StandardError
8
+ def initialize(http_code, body)
9
+ json = JSON.parse(body)
10
+ error = json && json["error"]
11
+ if error
12
+ super "#{http_code}: #{error['message']} (code #{error['code']})"
13
+ else
14
+ super "#{http_code}: #{body}"
15
+ end
16
+ end
17
+ end
18
+
19
+ module_function
20
+
21
+ def host
22
+ if Imaterialise.configuration.sandbox
23
+ "https://imatsandbox.materialise.net"
24
+ else
25
+ "https://i.materialise.com"
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,35 @@
1
+ require "imaterialise/api"
2
+
3
+ module Imaterialise
4
+ module API
5
+ # 3D Print Lab Connection API
6
+ # See http://i.materialise.com/api/docs/3d-print-lab-connection-api/
7
+ module Connection
8
+ module_function
9
+
10
+ def post(model)
11
+ url = "#{API.host}/upload"
12
+ body = post_body(model)
13
+
14
+ RestClient.post(url, body, accept: :json) do |response, _, _|
15
+ case response.code
16
+ when 302
17
+ { url: response.headers[:location] }
18
+ else
19
+ fail APIError.new(response.code, response.body)
20
+ end
21
+ end
22
+ end
23
+
24
+ def post_body(model)
25
+ {
26
+ useAjax: true,
27
+ forceEmbedding: false,
28
+ plugin: Imaterialise.configuration.tool_id,
29
+ fileUrl: model.file_url,
30
+ currency: model.currency.upcase,
31
+ }
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,24 @@
1
+ # Settings for connecting to the API.
2
+ module Imaterialise
3
+ # Container for configuration settings.
4
+ class Configuration
5
+ attr_accessor :api_code
6
+ attr_accessor :tool_id
7
+ attr_accessor :sandbox
8
+
9
+ def initialize
10
+ @api_code = ""
11
+ @tool_id = ""
12
+ @sandbox = false
13
+ end
14
+ end
15
+
16
+ class << self
17
+ attr_accessor :configuration
18
+ end
19
+
20
+ def self.configure
21
+ self.configuration ||= Configuration.new
22
+ yield(configuration)
23
+ end
24
+ end
@@ -0,0 +1,20 @@
1
+ require "imaterialise/api/connection"
2
+
3
+ module Imaterialise
4
+ # Represents a 3D Print Lab Connection between a model and imaterialise.
5
+ #
6
+ # See README.md for usage.
7
+ class Connection
8
+ attr_reader :file_url, :currency, :url
9
+
10
+ def initialize(file_url:, currency:)
11
+ @file_url = file_url
12
+ @currency = currency
13
+ end
14
+
15
+ def upload
16
+ result = API::Connection.post(self)
17
+ @url = result.fetch(:url)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module Imaterialise
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,38 @@
1
+ require "spec_helper"
2
+
3
+ module Imaterialise
4
+ module API
5
+ describe Connection do
6
+ let(:connection) {
7
+ Imaterialise::Connection.new(file_url: "http://example.com/foo.stl",
8
+ currency: "eur")
9
+ }
10
+
11
+ describe ".post" do
12
+ subject { described_class.post(connection) }
13
+
14
+ it "returns a hash" do
15
+ expected_body = {
16
+ "currency" => "EUR",
17
+ "fileUrl" => "http://example.com/foo.stl",
18
+ "forceEmbedding" => "false",
19
+ "plugin" => "test-tool-id",
20
+ "useAjax" => "true",
21
+ }
22
+ expected_headers = {
23
+ "Accept" => "application/json",
24
+ }
25
+ response_headers = {
26
+ "Location" => "http://i.materialise.com/foo.stl",
27
+ }
28
+
29
+ stub_request(:post, "https://imatsandbox.materialise.net/upload")
30
+ .with(body: expected_body, headers: expected_headers)
31
+ .to_return(status: 302, headers: response_headers)
32
+
33
+ expect(subject).to eq(url: "http://i.materialise.com/foo.stl")
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,36 @@
1
+ require "spec_helper"
2
+
3
+ describe Imaterialise::Connection do
4
+ subject {
5
+ described_class.new(file_url: "http://example.com/foo.stl",
6
+ currency: "eur")
7
+ }
8
+
9
+ it "has a file url" do
10
+ expect(subject.file_url).to eq("http://example.com/foo.stl")
11
+ end
12
+
13
+ it "has a currency" do
14
+ expect(subject.currency).to eq("eur")
15
+ end
16
+
17
+ context "on upload" do
18
+ let(:upload_response) { { url: "http://test" } }
19
+ before do
20
+ allow(Imaterialise::API::Connection).to receive(:post) { upload_response }
21
+ end
22
+
23
+ it { expect(subject.upload).to be_truthy }
24
+
25
+ it "calls the API correctly" do
26
+ subject.upload
27
+ expect(Imaterialise::API::Connection).to have_received(:post)
28
+ .with(subject)
29
+ end
30
+
31
+ it "sets the url" do
32
+ subject.upload
33
+ expect(subject.url).to eq("http://test")
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,13 @@
1
+ require "webmock/rspec"
2
+
3
+ # Cut off the interwebs
4
+ WebMock.disable_net_connect!(allow_localhost: true)
5
+
6
+
7
+ require "imaterialise"
8
+
9
+ Imaterialise.configure do |c|
10
+ c.api_code = "test-api-code"
11
+ c.tool_id = "test-tool-id"
12
+ c.sandbox = true
13
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: imaterialise
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: 2016-01-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: webmock
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
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: rake
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: rspec
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
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Upload 3D Models on imaterialise's service
70
+ email:
71
+ - sunny@cults3d.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - MIT-LICENSE
77
+ - README.md
78
+ - Rakefile
79
+ - console.rb
80
+ - lib/imaterialise.rb
81
+ - lib/imaterialise/api.rb
82
+ - lib/imaterialise/api/connection.rb
83
+ - lib/imaterialise/configuration.rb
84
+ - lib/imaterialise/connection.rb
85
+ - lib/imaterialise/version.rb
86
+ - spec/api/connection_spec.rb
87
+ - spec/connection_spec.rb
88
+ - spec/spec_helper.rb
89
+ homepage: http://github.com/sunny/imaterialise
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.4.5
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Access imaterialise's API
113
+ test_files:
114
+ - spec/api/connection_spec.rb
115
+ - spec/connection_spec.rb
116
+ - spec/spec_helper.rb