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 |
# 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
198 199 200 201 202 |
# File 'lib/octopart/client.rb', line 198 def bom_match(lines) raise(ArgumentError, 'lines must be a hash') unless lines.is_a?(::Hash) query = {:lines => "[{"+lines.map{|k,v| "\"#{k}\":\"#{v}\""}.join(',')+"}]"} make_request('/bom/match', query) end |
#categories(ids) ⇒ Hash
Fetch multiple category objects by their ids
52 53 54 55 56 |
# File 'lib/octopart/client.rb', line 52 def categories(ids) raise(ArgumentError, 'ids must be an array') unless ids.is_a?Array query = {:ids => "[#{ids.join(",")}]"} make_request('/categories/get_multi', query) end |
#category(id) ⇒ Hash
Fetch a category object by its id
37 38 39 40 41 42 43 |
# File 'lib/octopart/client.rb', line 37 def category(id) if id.is_a? Array categories(id) else make_request('/categories/get', {:id => id}) end end |
#match_part(manufacturer_name, mpn) ⇒ Hash Also known as: match
Match (manufacturer,mpn) to part uids
148 149 150 151 |
# File 'lib/octopart/client.rb', line 148 def match_part(manufacturer_name, mpn) query = {:manufacturer_name => manufacturer_name, :mpn => mpn} make_request('/parts/match', query) end |
#part(uid) ⇒ Hash
Fetch a part object by its id
80 81 82 83 84 85 86 87 |
# File 'lib/octopart/client.rb', line 80 def part(uid) if uid.is_a? Array parts(uid) else query = {:uid => uid} make_request('/parts/get', query) end end |
#part_attribute(fieldname) ⇒ Hash
Fetch a partattribute object by its id
160 161 162 163 164 165 166 167 |
# File 'lib/octopart/client.rb', line 160 def part_attribute(fieldname) if fieldname.is_a? Array part_attributes(fieldname) else query = {:fieldname => fieldname} make_request('/partattributes/get', query) end end |
#part_attributes(fieldnames) ⇒ Hash
Fetch multiple partattribute objects by their ids
176 177 178 179 180 |
# File 'lib/octopart/client.rb', line 176 def part_attributes(fieldnames) raise(ArgumentError, 'fieldnames must be an array') unless fieldnames.is_a?Array query = {:fieldnames => "["+fieldnames.map{|v| "\"#{v}\""}.join(',')+"]"} make_request('/partattributes/get_multi', query) end |
#parts(uids) ⇒ Hash
Fetch multiple part objects by their ids
96 97 98 99 100 |
# File 'lib/octopart/client.rb', line 96 def parts(uids) raise(ArgumentError, 'uids must be an array') unless uids.is_a?Array query = {:uids => "[#{uids.join(",")}]"} make_request('/parts/get_multi', query) end |
#search(type, query, start = 0, limit = 10) ⇒ Hash
Helper method for searches
220 221 222 223 224 225 226 227 228 229 |
# File 'lib/octopart/client.rb', line 220 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
67 68 69 70 71 |
# File 'lib/octopart/client.rb', line 67 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)) query = {:q => query, :start => start, :limit => limit} make_request('/categories/search', query) end |
#search_parts(query, start = 0, limit = 10) ⇒ Hash
Execute search over part objects
117 118 119 120 121 |
# File 'lib/octopart/client.rb', line 117 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)) query = {:q => query, :start => start, :limit => limit} make_request('/parts/search', query) end |
#suggest_parts(query, limit = 5) ⇒ Hash
Suggest a part search query string
134 135 136 137 138 |
# File 'lib/octopart/client.rb', line 134 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)) query = {:q => query.split(' ').join('+'), :limit => limit} make_request('/parts/suggest', query) end |