Class: Octopart::Client
- Inherits:
-
Object
- Object
- Octopart::Client
- Includes:
- HTTParty
- Defined in:
- lib/octopart/client.rb
Overview
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Public: The API key for the client.
Instance Method Summary collapse
-
#bom_match(lines) ⇒ Object
Public: Match lines of a BOM to parts.
-
#categories(ids) ⇒ Object
Public: Fetch multiple category objects by their ids.
-
#category(id) ⇒ Object
Public: Fetch a category object by its id.
-
#initialize(api_key = nil) ⇒ Client
constructor
Public: Initialize an Octopart client.
-
#match(manufacturer_name, mpn) ⇒ Object
Public: Helper method for match_part(manufacturer,mpn).
-
#match_part(manufacturer_name, mpn) ⇒ Object
Public: Match (manufacturer,mpn) to part uids.
-
#part(uid) ⇒ Object
Public: Fetch a part object by its id.
-
#part_attribute(fieldname) ⇒ Object
Public: Fetch a partattribute object by its id.
-
#part_attributes(fieldnames) ⇒ Object
Public: Fetch multiple partattribute objects by their ids.
-
#parts(uids) ⇒ Object
Public: Fetch multiple part objects by their ids.
-
#search(type, query, start = 0, limit = 10) ⇒ Object
Public: Helper method for searches.
-
#search_categories(query, start = 0, limit = 10) ⇒ Object
Public: Execute search over category objects.
-
#search_parts(query, start = 0, limit = 10) ⇒ Object
Public: Execute search over part objects.
-
#suggest_parts(query, limit = 5) ⇒ Object
Public: Suggest a part search query string.
Constructor Details
#initialize(api_key = nil) ⇒ Client
Public: Initialize an Octopart client
api_key - The API key to use You can get an Octopart API key at octopart.com/api/register
31 32 33 34 35 |
# File 'lib/octopart/client.rb', line 31 def initialize(api_key=nil) @api_key = api_key @api_key ||= Octopart.api_key end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Public: The API key for the client
25 26 27 |
# File 'lib/octopart/client.rb', line 25 def api_key @api_key end |
Instance Method Details
#bom_match(lines) ⇒ Object
Public: Match lines of a BOM to parts
lines - hash made up of the following optional parameters:
q - Free form query
mpn - MPN string
manufacturer - Manufacturer name
sku - Supplier SKU string
supplier - Supplier name
mpn_or_sku - Match on MPN or SKU
start=0 - Ordinal position of first item
limit=3 - Maximum number of items to return
reference - Arbitrary reference string to differentiate results
Examples
bom_match({"mpn_or_sku"=> "60K6871", "manufacturer" => "Texas Instruments"})
# => match hash
Returns a match hash
246 247 248 249 |
# File 'lib/octopart/client.rb', line 246 def bom_match(lines) raise(ArgumentError, 'lines must be a hash') unless lines.is_a?(::Hash) self.class.get('/bom/match', :query => {:lines => "[{"+lines.map{|k,v| "\"#{k}\":\"#{v}\""}.join(',')+"}]", :apikey => @api_key}).parsed_response end |
#categories(ids) ⇒ Object
Public: Fetch multiple category objects by their ids
ids - Array of category object ids
Examples
categories([4215,4174,4780])
# => category hash
Returns a category hash
67 68 69 70 |
# File 'lib/octopart/client.rb', line 67 def categories(ids) raise(ArgumentError, 'ids must be an array') unless ids.is_a?Array self.class.get('/categories/get_multi', :query => {:ids => "[#{ids.join(",")}]", :apikey => @api_key}).parsed_response end |
#category(id) ⇒ Object
Public: Fetch a category object by its id
id - The id of a category object
Examples
category(4174)
# => category hash
Returns a category hash
48 49 50 51 52 53 54 |
# File 'lib/octopart/client.rb', line 48 def category(id) if id.is_a? Array categories(id) else self.class.get('/categories/get', :query => {:id => id, :apikey => @api_key}).parsed_response end end |
#match(manufacturer_name, mpn) ⇒ Object
Public: Helper method for match_part(manufacturer,mpn)
manufacturer_name - Manufacturer name mpn - Manufacturer part number
Examples
match_part('Texas Instruments', 'SN74LS240N')
# => parts hash
Returns a part hash
190 191 192 |
# File 'lib/octopart/client.rb', line 190 def match(manufacturer_name, mpn) match_part(manufacturer_name, mpn) end |
#match_part(manufacturer_name, mpn) ⇒ Object
Public: Match (manufacturer,mpn) to part uids
manufacturer_name - Manufacturer name mpn - Manufacturer part number
Examples
match_part('Texas Instruments', 'SN74LS240N')
# => parts hash
Returns a part hash
175 176 177 |
# File 'lib/octopart/client.rb', line 175 def match_part(manufacturer_name, mpn) self.class.get('/parts/match', :query => {:manufacturer_name => manufacturer_name, :mpn => mpn, :apikey => @api_key}).parsed_response end |
#part(uid) ⇒ Object
Public: Fetch a part object by its id
uid - the id of a part object
Examples
part(39619421)
# => part hash
Returns a part hash
99 100 101 102 103 104 105 |
# File 'lib/octopart/client.rb', line 99 def part(uid) if uid.is_a? Array parts(uid) else self.class.get('/parts/get', :query => {:uid => uid, :apikey => @api_key}).parsed_response end end |
#part_attribute(fieldname) ⇒ Object
Public: Fetch a partattribute object by its id
fieldname - The fieldname of a partattribute object
Examples
part_attribute('capacitance')
# => partattribute hash
Returns a partattribute hash
204 205 206 207 208 209 210 |
# File 'lib/octopart/client.rb', line 204 def part_attribute(fieldname) if fieldname.is_a? Array part_attributes(fieldname) else self.class.get('/partattributes/get', :query => {:fieldname => fieldname, :apikey => @api_key}).parsed_response end end |
#part_attributes(fieldnames) ⇒ Object
Public: Fetch multiple partattribute objects by their ids
fieldnames - The fieldnames of a partattribute objects
Examples
part_attributes(['capacitance', 'resistance'])
# => partattribute hash
Returns a partattribute hash
222 223 224 225 |
# File 'lib/octopart/client.rb', line 222 def part_attributes(fieldnames) raise(ArgumentError, 'fieldnames must be an array') unless fieldnames.is_a?Array self.class.get('/partattributes/get_multi', :query => {:fieldnames => "["+fieldnames.map{|v| "\"#{v}\""}.join(',')+"]", :apikey => @api_key}).parsed_response end |
#parts(uids) ⇒ Object
Public: Fetch multiple part objects by their ids
uids - JSON encoded list of part object ids. Max number of ids is 100.
Examples
parts([39619421,29035751,31119928])
# => parts hash
Returns a part hash
117 118 119 120 |
# File 'lib/octopart/client.rb', line 117 def parts(uids) raise(ArgumentError, 'uids must be an array') unless uids.is_a?Array self.class.get('/parts/get_multi', :query => {:uids => "[#{uids.join(",")}]", :apikey => @api_key}).parsed_response end |
#search(type, query, start = 0, limit = 10) ⇒ Object
Public: Helper method for searches
type - String name of the type query - Query string 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.
Examples
search_parts('parts', 'capacitor')
# => part hash
search_parts('categories', 'capacitor', 50)
# => part hash
search_parts('parts', 'capacitor', 100, 25)
# => part hash
Returns a part hash
270 271 272 273 274 275 276 277 278 279 |
# File 'lib/octopart/client.rb', line 270 def search(type, query, start=0, limit=10) raise(ArgumentError, 'query must be a string > 2 characters and start/limit must be < 100') unless (query.is_a?(String) && query.length > 2 && start.between?(0, 100) &&limit.between?(0,100)) if type.downcase == 'part' || type.downcase == 'parts' search_parts(query, start, limit) elsif type.downcase == 'category' || type.downcase == 'categories' search_categories(query, start, limit) else raise(ArgumentError, "type must be either 'parts' or 'categories'") end end |
#search_categories(query, start = 0, limit = 10) ⇒ Object
Public: Execute search over category objects
q - Query string start - Ordinal position of first result. First position is 0. Default is 0 limit - Maximum number of results to return. Default is 10
Examples
search_categories('resistor')
# => category hash
Returns a category hash
84 85 86 87 |
# File 'lib/octopart/client.rb', line 84 def search_categories(query, start=0, limit=10) raise(ArgumentError, 'query must be a string > 2 characters and start/limit must be < 100') unless (query.is_a?(String) && query.length > 2 && start.between?(0, 100) &&limit.between?(0,100)) self.class.get('/categories/search', :query => {:q => query, :start => start, :limit => limit, :apikey => @api_key}).parsed_response end |
#search_parts(query, start = 0, limit = 10) ⇒ Object
Public: Execute search over part objects
query - Query string 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.
Examples
search_parts('capacitor')
# => part hash
search_parts('capacitor', 50)
# => part hash
search_parts('capacitor', 100, 25)
# => part hash
Returns a part hash
140 141 142 143 |
# File 'lib/octopart/client.rb', line 140 def search_parts(query, start=0, limit=10) raise(ArgumentError, 'query must be a string > 2 characters, start < 1000, and limit must be < 100') unless (query.is_a?(String) && query.length > 2 && start.between?(0, 1000) &&limit.between?(0,100)) self.class.get('/parts/search', :query => {:q => query, :start => start, :limit => limit, :apikey => @api_key}).parsed_response end |
#suggest_parts(query, limit = 5) ⇒ Object
Public: Suggest a part search query string
q - Query string. Minimum of 2 characters. limit - Maximum number of results to return. Default is 5. Maximum is 10.
Examples
suggest_parts('sn74f')
# => parts hash
suggest_parts('sn74f', 10)
# => parts hash
Returns a part hash
159 160 161 162 |
# File 'lib/octopart/client.rb', line 159 def suggest_parts(query, limit=5) raise(ArgumentError, 'query must be a string > 2 characters, and limit must be < 10') unless (query.is_a?(String) && query.length > 2 && limit.between?(0,10)) self.class.get('/parts/suggest', :query => {:q => query.split(' ').join('+'), :limit => limit, :apikey => @api_key}).parsed_response end |