Class: Oshpark::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/oshpark/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Client

Create an new Client object.

pass in a subclass of connection which implements the ‘request` method with whichever HTTP client library you prefer. Default is Net::HTTP.

Parameters:

  • connection:


13
14
15
16
17
18
19
20
21
22
23
# 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

Instance Attribute Details

#connection=(value) ⇒ Object

Sets the attribute connection

Parameters:

  • value

    the value to set the attribute connection to.



6
7
8
# File 'lib/oshpark/client.rb', line 6

def connection=(value)
  @connection = value
end

#tokenObject

Returns the value of attribute token.



6
7
8
# File 'lib/oshpark/client.rb', line 6

def token
  @token
end

Instance Method Details

#abandonObject

Abandon a previous authentication.



42
43
44
45
# File 'lib/oshpark/client.rb', line 42

def abandon
  self.token = nil
  refresh_token
end

#add_order_item(id, project_id, quantity) ⇒ Object

Add a Project to an Order

Parameters:

  • id
  • project_id
  • quantity


117
118
119
# 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) ⇒ Object

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

Parameters:

  • id


64
65
66
# File 'lib/oshpark/client.rb', line 64

def approve_project id
  get_request "projects/#{id}/approve"
end

#authenticate(email, credentials = {}) ⇒ Object

Authenticate to the API using a email and password.

Parameters:

  • email
  • credentials (defaults to: {})

    A hash with either the ‘with_password` or `with_api_secret` key.



30
31
32
33
34
35
36
37
38
39
# 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?Boolean

Are we successfully authenticated to the API?

Returns:

  • (Boolean)


209
210
211
# File 'lib/oshpark/client.rb', line 209

def authenticated?
  @token && !!@token.user
end

#cancel_order(id) ⇒ Object

Cancel a specific order by ID. This can only be done when the order is in the ‘RECEIVED’ and ‘AWAITING PANEL’ states.

Parameters:

  • id


155
156
157
158
# File 'lib/oshpark/client.rb', line 155

def cancel_order id
  delete_request "orders/#{id}"
  true
end

#checkout_order(id) ⇒ Object

Checkout a specific order by ID.

Parameters:

  • id


146
147
148
# File 'lib/oshpark/client.rb', line 146

def checkout_order id
  post_request "orders/#{id}/checkout"
end

#create_import(url) ⇒ Object

Create an import by passing in a URL

A URL

Parameters:

  • io


199
200
201
# File 'lib/oshpark/client.rb', line 199

def create_import url
  post_request "imports", {url: url}
end

#create_orderObject

Create a new Order



108
109
110
# File 'lib/oshpark/client.rb', line 108

def create_order
  post_request "orders"
end

#create_upload(io) ⇒ Object

Create an upload by passing in an IO

An IO object.

Parameters:

  • io


184
185
186
# File 'lib/oshpark/client.rb', line 184

def create_upload io
  post_request "uploads", {file: io}
end

#destroy_project(id) ⇒ Object

Destroy a particular project from the current user’s collection by ID.

Parameters:

  • id


80
81
82
83
# File 'lib/oshpark/client.rb', line 80

def destroy_project id
  delete_request "projects/#{id}"
  true
end

#has_token?Boolean

Do we have a currently valid API token?

Returns:

  • (Boolean)


204
205
206
# File 'lib/oshpark/client.rb', line 204

def has_token?
  !!@token
end

#import(id) ⇒ Object

Retrieve a specific import by ID

Parameters:

  • id


191
192
193
# File 'lib/oshpark/client.rb', line 191

def import id
  get_request "imports/#{id}"
end

#order(id) ⇒ Object

Retrieve a specific order by ID.

Parameters:

  • id


103
104
105
# File 'lib/oshpark/client.rb', line 103

def order id
  get_request "orders/#{id}"
end

#ordersObject

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



96
97
98
# File 'lib/oshpark/client.rb', line 96

def orders
  get_request 'orders'
end

#panel(id) ⇒ Object

Retrieve a specific panel by ID.

Parameters:

  • id


169
170
171
# File 'lib/oshpark/client.rb', line 169

def panel id
  get_request "panels/#{id}"
end

#panelsObject

List all currently open and recently closed panels, including some interesting information about them.



162
163
164
# File 'lib/oshpark/client.rb', line 162

def panels
  get_request "panels"
end

#pricing(width, height, pcb_layers, quantity = nil) ⇒ Object

Request a price estimate

Parameters:

  • width

    In thousands of an Inch

  • height

    In thousands of an Inch

  • layers
  • quantity (defaults to: nil)

    Optional Defaults to the minimum quantity



91
92
93
# 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) ⇒ Object

Retrieve a particular project from the current user’s collection by ID.

Parameters:

  • id


55
56
57
# File 'lib/oshpark/client.rb', line 55

def project id
  get_request "projects/#{id}"
end

#projectsObject

Retrieve a list of projects for the current user.



48
49
50
# File 'lib/oshpark/client.rb', line 48

def projects
  get_request 'projects'
end

#set_order_address(id, address) ⇒ Object

Set the delivery address for an Order

An Address object or a Hash with at least the required keys: :name :address_line_1 :address_line_2 :city :country

Parameters:

  • id
  • address


126
127
128
# 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) ⇒ Object

Set the delivery address for an Order

A ShippingRate object or a Hash with the following keys: :carrier_name :service_name

Parameters:

  • id
  • shipping_rate


139
140
141
# 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) ⇒ Object



130
131
132
# File 'lib/oshpark/client.rb', line 130

def shipping_rates address_params
  post_request "shipping_rates", {address: address_params}
end

#update_project(id, attrs) ⇒ Object

Update a project’s data.

A hash of attributes to update.

Parameters:

  • id
  • attrs


73
74
75
# File 'lib/oshpark/client.rb', line 73

def update_project id, attrs
  put_request "projects/#{id}", project: attrs
end

#upload(id) ⇒ Object

Retrieve a specific upload by ID

Parameters:

  • id


176
177
178
# File 'lib/oshpark/client.rb', line 176

def upload id
  get_request "uploads/#{id}"
end