class Oshpark::Client
Attributes
Public Class Methods
Create an new Client
object.
@param connection: pass in a subclass of connection which implements the ‘request` method with whichever HTTP client library you prefer. Default is Net::HTTP.
# File lib/oshpark/client.rb, line 13 def initialize args = {} url = args.fetch(:url, "https://oshpark.com/api/v1") connection = args.fetch(:connection, Connection) self.connection = if connection.respond_to? :new connection.new url else connection end refresh_token end
Public Instance Methods
Abandon a previous authentication.
# File lib/oshpark/client.rb, line 42 def abandon self.token = nil refresh_token end
Approve a particular project from the current user’s collection by ID. We strongly suggest that you allow the user to view the rendered images in Project#top_image
, Project#bottom_image
and Project#layers
[]#image
@param id
# File lib/oshpark/client.rb, line 64 def approve_project id get_request "projects/#{id}/approve" end
Authenticate to the API using a email and password.
@param email @param credentials
A hash with either the `with_password` or `with_api_secret` key.
# File lib/oshpark/client.rb, line 30 def authenticate email, credentials={} if password = credentials[:with_password] refresh_token email: email, password: password elsif secret = credentials[:with_api_secret] api_key = OpenSSL::Digest::SHA256.new("#{email}:#{secret}:#{token.token}").to_s refresh_token email: email, api_key: api_key else raise ArgumentError, "Must provide either `with_password` or `with_api_secret` arguments." end end
Are we successfully authenticated to the API?
# File lib/oshpark/client.rb, line 209 def authenticated? @token && !!@token.user end
Cancel a specific order by ID. This can only be done when the order is in the ‘RECEIVED’ and ‘AWAITING PANEL’ states.
@param id
# File lib/oshpark/client.rb, line 155 def cancel_order id delete_request "orders/#{id}" true end
Checkout a specific order by ID.
@param id
# File lib/oshpark/client.rb, line 146 def checkout_order id post_request "orders/#{id}/checkout" end
Create an import by passing in a URL
@param io A URL
# File lib/oshpark/client.rb, line 199 def create_import url post_request "imports", {url: url} end
Create a new Order
# File lib/oshpark/client.rb, line 108 def create_order post_request "orders" end
Create an upload by passing in an IO
@param io An IO object.
# File lib/oshpark/client.rb, line 184 def create_upload io post_request "uploads", {file: io} end
Destroy a particular project from the current user’s collection by ID.
@param id
# File lib/oshpark/client.rb, line 80 def destroy_project id delete_request "projects/#{id}" true end
Do we have a currently valid API token?
# File lib/oshpark/client.rb, line 204 def has_token? !!@token end
Retrieve a specific import by ID
@param id
# File lib/oshpark/client.rb, line 191 def import id get_request "imports/#{id}" end
Retrieve a specific order by ID.
@param id
# File lib/oshpark/client.rb, line 103 def order id get_request "orders/#{id}" end
List all the current user’s orders, and their status.
# File lib/oshpark/client.rb, line 96 def orders get_request 'orders' end
Retrieve a specific panel by ID.
@param id
# File lib/oshpark/client.rb, line 169 def panel id get_request "panels/#{id}" end
List all currently open and recently closed panels, including some interesting information about them.
# File lib/oshpark/client.rb, line 162 def panels get_request "panels" end
Request a price estimate
@param width In thousands of an Inch @param height In thousands of an Inch @param layers @param quantity Optional Defaults to the minimum quantity
# File lib/oshpark/client.rb, line 91 def pricing width, height, pcb_layers, quantity = nil post_request "pricing", {width_in_mils: width, height_in_mils: height, pcb_layers: pcb_layers, quantity: quantity} end
Retrieve a particular project from the current user’s collection by ID.
@param id
# File lib/oshpark/client.rb, line 55 def project id get_request "projects/#{id}" end
Retrieve a list of projects for the current user.
# File lib/oshpark/client.rb, line 48 def projects get_request 'projects' end
Set the delivery address for an Order
@param id @param address An Address
object or a Hash with at least the required keys: :name :address_line_1 :address_line_2 :city :country
# File lib/oshpark/client.rb, line 126 def set_order_address id, address post_request "orders/#{id}/set_address", {order: {address: address.to_h}} end
Set the delivery address for an Order
@param id @param shipping_rate A ShippingRate
object or a Hash with the following keys: :carrier_name :service_name
# File lib/oshpark/client.rb, line 139 def set_order_shipping_rate id, shipping_rate post_request "orders/#{id}/set_shipping_rate", {order:{shipping_rate: shipping_rate.to_h}} end
# File lib/oshpark/client.rb, line 130 def shipping_rates address_params post_request "shipping_rates", {address: address_params} end
Update a project’s data.
@param id @param attrs A hash of attributes to update.
# File lib/oshpark/client.rb, line 73 def update_project id, attrs put_request "projects/#{id}", project: attrs end
Retrieve a specific upload by ID
@param id
# File lib/oshpark/client.rb, line 176 def upload id get_request "uploads/#{id}" end
Private Instance Methods
# File lib/oshpark/client.rb, line 237 def delete_request endpoint, params={} connection.request :delete, endpoint, params, token end
# File lib/oshpark/client.rb, line 233 def get_request endpoint, params={} connection.request :get, endpoint, params, token end
# File lib/oshpark/client.rb, line 225 def post_request endpoint, params={} connection.request :post, endpoint, params, token end
# File lib/oshpark/client.rb, line 229 def put_request endpoint, params={} connection.request :put, endpoint, params, token end
# File lib/oshpark/client.rb, line 219 def refresh_token params={} json = post_request 'sessions', params self.token = Token.from_json json['api_session_token'] # Hey @hmaddocks - how are we going to set a timer to make the token refresh? end