Class: Octopart::Part
Constant Summary
Constants inherited from Base
Class Method Summary collapse
-
.bom(lines) ⇒ Object
Public: Matches a list of part numbers to an Array of Octopart::Part.
-
.build(object) ⇒ Object
Internal: Converts a Hash or an Array of Hash into an Octopart::Part or an Array of Octopart::Part.
-
.find(*args) ⇒ Object
Public: Finds a part for a given uid and returns an Octopart::Part or an Array of Octopart::Part if multiple UIDs are given.
-
.match(manufacturer, mpn) ⇒ Object
Public: Matches a manufacturer and manufacturer part number to an Octopart part UID.
-
.search(query, options = {}) ⇒ Object
Public: Search for parts that match the given query and returns an Array of Octopart::Part.
Instance Method Summary collapse
-
#average_price ⇒ Object
Public: Returns the average price of the part in USD currency.
-
#best_offer(quantity = 1) ⇒ Object
Public: Returns the offer with the lowest price for the quantity requested.
-
#best_price(quantity = 1) ⇒ Object
Public: Returns the the lowest price of the best offer for the quantity requested.
-
#datasheet ⇒ Object
Public: Returns the datasheet with the highest score, or nil of no datasheets.
Methods inherited from Base
Class Method Details
.bom(lines) ⇒ Object
80 81 82 83 84 |
# File 'lib/octopart/part.rb', line 80 def bom(lines) lines = [lines] unless lines.is_a?(Array) response = JSON.parse(self.get('bom/match', lines: lines.to_json)) response['results'].map { |line| self.build(line['items']) } end |
.build(object) ⇒ Object
Internal: Converts a Hash or an Array of Hash into an Octopart::Part or an Array of Octopart::Part
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/octopart/part.rb', line 88 def build(object) if object.is_a?(Array) object.map { |obj| self.build(obj) } elsif object.is_a?(Hash) object.delete('__class__') object = Hashie::Mash.new(object) self.new.tap { |p| p.replace(object) } else raise "What is this? I don't even..." end end |
.find(*args) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/octopart/part.rb', line 16 def find(*args) case args.length when 0 raise ArgumentError.new("Please specify atleast 1 uid") when 1 response = JSON.parse(self.get('parts/get', uid: args.first)) else response = JSON.parse(self.get('parts/get_multi', uids: args.to_json)) end self.build(response) end |
.match(manufacturer, mpn) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/octopart/part.rb', line 62 def match(manufacturer, mpn) params = { manufacturer_name: manufacturer, mpn: mpn } response = JSON.parse(self.get('parts/match', params)) case response.length when 1 response.first.first end end |
.search(query, options = {}) ⇒ Object
Public: Search for parts that match the given query and returns an Array of Octopart::Part
query - A search term options - A set of options (default: {})
:start - Ordinal position of first result. First position is 0.
Default is 0. Maximum is 1000.
:limit - Number of results to return. Default is 10. Maximum is 100.
:filters - JSON encoded list of (fieldname,values) pairs
:rangedfilters - JSON encoded list of (fieldname, min/max values) pairs,
using null as wildcard.
:sortby - JSON encoded list of (fieldname,sort-order) pairs. Default is
[["score","desc"]]
Examples
parts = Octopart::Part.search('resistor', limit: 10)
45 46 47 48 49 50 |
# File 'lib/octopart/part.rb', line 45 def search(query, = {}) params = .merge(q: query) response = JSON.parse(self.get('parts/search', params)) parts = response['results'].map { |part| part['item'] } self.build(parts) end |
Instance Method Details
#average_price ⇒ Object
120 121 122 |
# File 'lib/octopart/part.rb', line 120 def average_price avg_price[0] end |
#best_offer(quantity = 1) ⇒ Object
137 138 139 140 141 142 143 144 145 |
# File 'lib/octopart/part.rb', line 137 def best_offer(quantity = 1) prices = [] offers.each do |offer| p = offer.prices.select { |p| p[0] <= quantity }.last prices << { sku: offer.sku, price: p[1] } if p end best = prices.min_by { |p| p[:price] } offers.find { |offer| offer.sku == best[:sku] } end |
#best_price(quantity = 1) ⇒ Object
160 161 162 |
# File 'lib/octopart/part.rb', line 160 def best_price(quantity = 1) best_offer.prices.reject { |p| p[0] > quantity }.last[1] end |
#datasheet ⇒ Object
109 110 111 112 |
# File 'lib/octopart/part.rb', line 109 def datasheet return nil unless datasheets.length > 0 datasheets.first.url end |