Class: Octopart::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/octopart/client.rb

Overview

An Octopart.com API Client

Examples:

Octopart::Client.new('apikey')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key = nil) ⇒ Client

Note:

You can get an Octopart API key at octopart.com/api/register

Initialize an Octopart client

Parameters:

  • api_key (String) (defaults to: nil)

    The API key to use



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_keyObject (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

Examples:

bom_match({"mpn_or_sku"=> "60K6871", "manufacturer" => "Texas Instruments"})
# => [Hash]

Parameters:

  • lines (Hash)

    hash made up of the following optional parameters:

Options Hash (lines):

  • q (String)

    Free form query

  • mpn (String)

    MPN string

  • manufacturer (String)

    Manufacturer name

  • sku (String)

    Supplier SKU string

  • supplier (String)

    Supplier name

  • mpn_or_sku (String)

    Match on MPN or SKU

  • start (Integer)

    Ordinal position of first item

  • limit (Integer)

    Maximum number of items to return

  • reference (String)

    Arbitrary reference string to differentiate results

Returns:

  • (Hash)

    A match hash

Raises:

  • (ArgumentError)


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

Examples:

categories([4215,4174,4780])
# => [Hash]

Parameters:

  • ids (Array)

    Array of category object ids

Returns:

  • (Hash)

    A category hash

Raises:

  • (ArgumentError)


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

Examples:

category(4174)
# => [Hash]

Parameters:

  • id (String)

    The id of a category object

Returns:

  • (Hash)

    A category hash



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

Examples:

match_part('Texas Instruments', 'SN74LS240N')
# => [Hash]

Parameters:

  • manufacturer_name (String)

    Manufacturer name

  • mpn (String)

    Manufacturer part number

Returns:

  • (Hash)

    A part hash



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

Examples:

part(39619421)
# => [Hash]

Parameters:

  • uid (String)

    the id of a part object

Returns:

  • (Hash)

    A part hash



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

Examples:

part_attribute('capacitance')
# => [Hash]

Parameters:

  • fieldname (String)

    The fieldname of a partattribute object

Returns:

  • (Hash)

    A part attribute hash



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

Examples:

part_attributes(['capacitance', 'resistance'])
# => partattribute hash

Parameters:

  • fieldnames (Array)

    The fieldnames of a partattribute objects

Returns:

  • (Hash)

    A part attribute hash

Raises:

  • (ArgumentError)


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

Examples:

parts([39619421,29035751,31119928])
# => [Hash]

Parameters:

  • uids (Array)

    JSON encoded list of part object ids. Max number of ids is 100.

Returns:

  • (Hash)

    A part hash

Raises:

  • (ArgumentError)


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

Examples:

search_parts('parts', 'capacitor')
# => [Hash]

search_parts('categories', 'capacitor', 50)
# => [Hash]

search_parts('parts', 'capacitor', 100, 25)
# => [Hash]

Parameters:

  • type (String)

    String name of the type

  • query (String)

    Query string

  • start (Integer) (defaults to: 0)

    Ordinal position of first result. First position is 0. Default is 0. Maximum is 1000.

  • limit (Integer) (defaults to: 10)

    Number of results to return. Default is 10. Maximum is 100.

Returns:

  • (Hash)

    A part hash

Raises:

  • (ArgumentError)


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

Examples:

search_categories('resistor')
# => [Hash]

Parameters:

  • query (String)

    Query string

  • start (Integer) (defaults to: 0)

    Ordinal position of first result. First position is 0. Default is 0

  • limit (Integer) (defaults to: 10)

    Maximum number of results to return. Default is 10

Returns:

  • (Hash)

    A category hash

Raises:

  • (ArgumentError)


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

Examples:

search_parts('capacitor')
# => [Hash]

search_parts('capacitor', 50)
# => [Hash]

search_parts('capacitor', 100, 25)
# => [Hash]

Parameters:

  • query (String)

    Query string

  • start (Integer) (defaults to: 0)

    Ordinal position of first result. First position is 0. Default is 0. Maximum is 1000.

  • limit (Integer) (defaults to: 10)

    Number of results to return. Default is 10. Maximum is 100.

Returns:

  • (Hash)

    A part hash

Raises:

  • (ArgumentError)


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

Examples:

suggest_parts('sn74f')
# => [Hash]

suggest_parts('sn74f', 10)
# => [Hash]

Parameters:

  • query (String)

    Query string. Minimum of 2 characters.

  • limit (Integer) (defaults to: 5)

    Maximum number of results to return. Default is 5. Maximum is 10.

Returns:

  • (Hash)

    A part hash

Raises:

  • (ArgumentError)


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