Class: Octopart::Client
- Inherits:
-
Object
- Object
- Octopart::Client
- Includes:
- HTTParty
- Defined in:
- lib/octopart/client.rb
Overview
An Octopart.com API Client
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
The API key for the client.
Instance Method Summary collapse
-
#bom_match(lines) ⇒ Hash
Match lines of a BOM to parts.
-
#categories(ids) ⇒ Hash
Fetch multiple category objects by their ids.
-
#category(id) ⇒ Hash
Fetch a category object by its id.
-
#initialize(api_key = nil) ⇒ Client
constructor
Initialize an Octopart client.
-
#match_part(manufacturer_name, mpn) ⇒ Hash
(also: #match)
Match (manufacturer,mpn) to part uids.
-
#part(uid) ⇒ Hash
Fetch a part object by its id.
-
#part_attribute(fieldname) ⇒ Hash
Fetch a partattribute object by its id.
-
#part_attributes(fieldnames) ⇒ Hash
Fetch multiple partattribute objects by their ids.
-
#parts(uids) ⇒ Hash
Fetch multiple part objects by their ids.
-
#search(type, query, start = 0, limit = 10) ⇒ Hash
Helper method for searches.
-
#search_categories(query, start = 0, limit = 10) ⇒ Hash
Execute search over category objects.
-
#search_parts(query, start = 0, limit = 10) ⇒ Hash
Execute search over part objects.
-
#suggest_parts(query, limit = 5) ⇒ Hash
Suggest a part search query string.
Constructor Details
#initialize(api_key = nil) ⇒ Client
You can get an Octopart API key at octopart.com/api/register
Initialize an Octopart client
25 26 27 28 29 |
# File 'lib/octopart/client.rb', line 25 def initialize(api_key=nil) @api_key = api_key @api_key ||= Octopart.api_key end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
The API key for the client
19 20 21 |
# File 'lib/octopart/client.rb', line 19 def api_key @api_key end |
Instance Method Details
#bom_match(lines) ⇒ Hash
Match lines of a BOM to parts
199 200 201 202 203 |
# File 'lib/octopart/client.rb', line 199 def bom_match(lines) raise(ArgumentError, 'lines must be a hash') unless lines.is_a?(::Hash) response = self.class.get('/bom/match', :query => {:lines => "[{"+lines.map{|k,v| "\"#{k}\":\"#{v}\""}.join(',')+"}]", :apikey => @api_key}) validate_response(response) end |
#categories(ids) ⇒ Hash
Fetch multiple category objects by their ids
54 55 56 57 58 |
# File 'lib/octopart/client.rb', line 54 def categories(ids) raise(ArgumentError, 'ids must be an array') unless ids.is_a?Array response = self.class.get('/categories/get_multi', :query => {:ids => "[#{ids.join(",")}]", :apikey => @api_key}) validate_response(response) end |
#category(id) ⇒ Hash
Fetch a category object by its id
38 39 40 41 42 43 44 45 |
# File 'lib/octopart/client.rb', line 38 def category(id) if id.is_a? Array categories(id) else response = self.class.get('/categories/get', :query => {:id => id, :apikey => @api_key}) validate_response(response) end end |
#match_part(manufacturer_name, mpn) ⇒ Hash Also known as: match
Match (manufacturer,mpn) to part uids
149 150 151 152 |
# File 'lib/octopart/client.rb', line 149 def match_part(manufacturer_name, mpn) response = self.class.get('/parts/match', :query => {:manufacturer_name => manufacturer_name, :mpn => mpn, :apikey => @api_key}) validate_response(response) end |
#part(uid) ⇒ Hash
Fetch a part object by its id
82 83 84 85 86 87 88 89 |
# File 'lib/octopart/client.rb', line 82 def part(uid) if uid.is_a? Array parts(uid) else response = self.class.get('/parts/get', :query => {:uid => uid, :apikey => @api_key}) validate_response(response) end end |
#part_attribute(fieldname) ⇒ Hash
Fetch a partattribute object by its id
161 162 163 164 165 166 167 168 |
# File 'lib/octopart/client.rb', line 161 def part_attribute(fieldname) if fieldname.is_a? Array part_attributes(fieldname) else response = self.class.get('/partattributes/get', :query => {:fieldname => fieldname, :apikey => @api_key}) validate_response(response) end end |
#part_attributes(fieldnames) ⇒ Hash
Fetch multiple partattribute objects by their ids
177 178 179 180 181 |
# File 'lib/octopart/client.rb', line 177 def part_attributes(fieldnames) raise(ArgumentError, 'fieldnames must be an array') unless fieldnames.is_a?Array response = self.class.get('/partattributes/get_multi', :query => {:fieldnames => "["+fieldnames.map{|v| "\"#{v}\""}.join(',')+"]", :apikey => @api_key}) validate_response(response) end |
#parts(uids) ⇒ Hash
Fetch multiple part objects by their ids
98 99 100 101 102 |
# File 'lib/octopart/client.rb', line 98 def parts(uids) raise(ArgumentError, 'uids must be an array') unless uids.is_a?Array response = self.class.get('/parts/get_multi', :query => {:uids => "[#{uids.join(",")}]", :apikey => @api_key}) validate_response(response) end |
#search(type, query, start = 0, limit = 10) ⇒ Hash
Helper method for searches
221 222 223 224 225 226 227 228 229 230 |
# File 'lib/octopart/client.rb', line 221 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) ⇒ Hash
Execute search over category objects
69 70 71 72 73 |
# File 'lib/octopart/client.rb', line 69 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)) response = self.class.get('/categories/search', :query => {:q => query, :start => start, :limit => limit, :apikey => @api_key}) validate_response(response) end |
#search_parts(query, start = 0, limit = 10) ⇒ Hash
Execute search over part objects
119 120 121 122 123 |
# File 'lib/octopart/client.rb', line 119 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)) response = self.class.get('/parts/search', :query => {:q => query, :start => start, :limit => limit, :apikey => @api_key}) validate_response(response) end |
#suggest_parts(query, limit = 5) ⇒ Hash
Suggest a part search query string
136 137 138 139 |
# File 'lib/octopart/client.rb', line 136 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)) response = self.class.get('/parts/suggest', :query => {:q => query.split(' ').join('+'), :limit => limit, :apikey => @api_key}) end |