Class: ShapewaysAPI
- Inherits:
-
Object
- Object
- ShapewaysAPI
- Defined in:
- lib/shapeways_api.rb
Constant Summary collapse
- BASE_URL =
'https://api.shapeways.com'
Instance Method Summary collapse
- #authenticate ⇒ Object
-
#create_order(order_data) ⇒ Object
Creates an order using the provided order_data order_data should be a hash with the following structure: { ‘items’ => [{ ‘materialId’ => material_id, ‘modelId’ => model_id, ‘quantity’ => 1 }], ‘firstName’ => ‘John’, ‘lastName’ => ‘Doe’, ‘country’ => ‘US’, ‘state’ => ‘NY’, ‘city’ => ‘New York’, ‘address1’ => ‘419 Park Ave S’, ‘address2’ => ‘Suite 900’, ‘zipCode’ => ‘10016’, ‘phoneNumber’ => ‘0000000000’, ‘paymentVerificationId’ => ‘YOUR_PAYMENT_VERIFICATION_ID’, ‘paymentMethod’ => ‘credit_card’, ‘shippingOption’ => ‘Cheapest’ }.
- #get_model(model_id) ⇒ Object
-
#initialize(client_id, client_secret) ⇒ ShapewaysAPI
constructor
A new instance of ShapewaysAPI.
- #materials ⇒ Object
- #upload_model(file_path, description) ⇒ Object
Constructor Details
#initialize(client_id, client_secret) ⇒ ShapewaysAPI
Returns a new instance of ShapewaysAPI.
9 10 11 12 13 |
# File 'lib/shapeways_api.rb', line 9 def initialize(client_id, client_secret) @client_id = client_id @client_secret = client_secret @access_token = authenticate end |
Instance Method Details
#authenticate ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/shapeways_api.rb', line 15 def authenticate uri = URI("#{BASE_URL}/oauth2/token") request = Net::HTTP::Post.new(uri) request.basic_auth(@client_id, @client_secret) request.set_form_data('grant_type' => 'client_credentials') response = send_request(uri, request) response['access_token'] end |
#create_order(order_data) ⇒ Object
Creates an order using the provided order_data order_data should be a hash with the following structure: {
'items' => [{ 'materialId' => material_id, 'modelId' => model_id, 'quantity' => 1 }],
'firstName' => 'John',
'lastName' => 'Doe',
'country' => 'US',
'state' => 'NY',
'city' => 'New York',
'address1' => '419 Park Ave S',
'address2' => 'Suite 900',
'zipCode' => '10016',
'phoneNumber' => '0000000000',
'paymentVerificationId' => 'YOUR_PAYMENT_VERIFICATION_ID',
'paymentMethod' => 'credit_card',
'shippingOption' => 'Cheapest'
}
80 81 82 83 84 85 86 87 88 |
# File 'lib/shapeways_api.rb', line 80 def create_order(order_data) uri = URI("#{BASE_URL}/orders/v1") request = Net::HTTP::Post.new(uri) request['Authorization'] = "Bearer #{@access_token}" request['Content-Type'] = 'application/json' request.body = order_data.to_json send_request(uri, request) end |
#get_model(model_id) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/shapeways_api.rb', line 33 def get_model(model_id) uri = URI("#{BASE_URL}/models/#{model_id}/v1?") request = Net::HTTP::Get.new(uri) request['Authorization'] = "Bearer #{@access_token}" send_request(uri, request) end |
#materials ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/shapeways_api.rb', line 25 def materials uri = URI("#{BASE_URL}/materials/v1?") request = Net::HTTP::Get.new(uri) request['Authorization'] = "Bearer #{@access_token}" send_request(uri, request) end |
#upload_model(file_path, description) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/shapeways_api.rb', line 41 def upload_model(file_path, description) file_name = File.basename(file_path) file_data = File.read(file_path) encoded_file = Base64.strict_encode64(file_data) uri = URI("#{BASE_URL}/models/v1") request = Net::HTTP::Post.new(uri) request['Authorization'] = "Bearer #{@access_token}" request['Content-Type'] = 'application/json' model_upload_data = { 'fileName' => file_name, 'file' => encoded_file, 'description' => description, 'hasRightsToModel' => 1, 'acceptTermsAndConditions' => 1 }.to_json request.body = model_upload_data send_request(uri, request) end |