class Oshpark::Client

Attributes

connection[RW]
token[RW]

Public Class Methods

new(args = {}) click to toggle source

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() click to toggle source

Abandon a previous authentication.

# File lib/oshpark/client.rb, line 42
def abandon
  self.token = nil
  refresh_token
end
add_order_item(id, project_id, quantity) click to toggle source

Add a Project to an Order

@param id @param project_id @param quantity

# File lib/oshpark/client.rb, line 117
def add_order_item id, project_id, quantity
  post_request "orders/#{id}/add_item", {order: {project_id: project_id, quantity: quantity}}
end
approve_project(id) click to toggle source

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(email, credentials={}) click to toggle source

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
authenticated?() click to toggle source

Are we successfully authenticated to the API?

# File lib/oshpark/client.rb, line 209
def authenticated?
  @token && !!@token.user
end
cancel_order(id) click to toggle source

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_order(id) click to toggle source

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_import(url) click to toggle source

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_order() click to toggle source

Create a new Order

# File lib/oshpark/client.rb, line 108
def create_order
  post_request "orders"
end
create_upload(io) click to toggle source

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_project(id) click to toggle source

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
has_token?() click to toggle source

Do we have a currently valid API token?

# File lib/oshpark/client.rb, line 204
def has_token?
  !!@token
end
import(id) click to toggle source

Retrieve a specific import by ID

@param id

# File lib/oshpark/client.rb, line 191
def import id
  get_request "imports/#{id}"
end
order(id) click to toggle source

Retrieve a specific order by ID.

@param id

# File lib/oshpark/client.rb, line 103
def order id
  get_request "orders/#{id}"
end
orders() click to toggle source

List all the current user’s orders, and their status.

# File lib/oshpark/client.rb, line 96
def orders
  get_request 'orders'
end
panel(id) click to toggle source

Retrieve a specific panel by ID.

@param id

# File lib/oshpark/client.rb, line 169
def panel id
  get_request "panels/#{id}"
end
panels() click to toggle source

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
pricing(width, height, pcb_layers, quantity = nil) click to toggle source

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
project(id) click to toggle source

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
projects() click to toggle source

Retrieve a list of projects for the current user.

# File lib/oshpark/client.rb, line 48
def projects
  get_request 'projects'
end
set_order_address(id, address) click to toggle source

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_order_shipping_rate(id, shipping_rate) click to toggle source

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
shipping_rates(address_params) click to toggle source
# File lib/oshpark/client.rb, line 130
def shipping_rates address_params
  post_request "shipping_rates", {address: address_params}
end
update_project(id, attrs) click to toggle source

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
upload(id) click to toggle source

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

delete_request(endpoint, params={}) click to toggle source
# File lib/oshpark/client.rb, line 237
def delete_request endpoint, params={}
  connection.request :delete, endpoint, params, token
end
get_request(endpoint, params={}) click to toggle source
# File lib/oshpark/client.rb, line 233
def get_request endpoint, params={}
  connection.request :get, endpoint, params, token
end
post_request(endpoint, params={}) click to toggle source
# File lib/oshpark/client.rb, line 225
def post_request endpoint, params={}
  connection.request :post, endpoint, params, token
end
put_request(endpoint, params={}) click to toggle source
# File lib/oshpark/client.rb, line 229
def put_request endpoint, params={}
  connection.request :put, endpoint, params, token
end
refresh_token(params={}) click to toggle source
# 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