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: Find’s a part for a given uid and returns an Octopart::Part or an Array of Octopart::Part if multipled 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.
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 |