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
# 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)


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

Examples:

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

Parameters:

  • ids (Array)

    Array of category object ids

Returns:

  • (Hash)

    A category hash

Raises:

  • (ArgumentError)


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

Examples:

category(4174)
# => [Hash]

Parameters:

  • id (String)

    The id of a category object

Returns:

  • (Hash)

    A category hash



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

Examples:

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

Parameters:

  • manufacturer_name (String)

    Manufacturer name

  • mpn (String)

    Manufacturer part number

Returns:

  • (Hash)

    A part hash



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

Examples:

part(39619421)
# => [Hash]

Parameters:

  • uid (String)

    the id of a part object

Returns:

  • (Hash)

    A part hash



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

Examples:

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

Parameters:

  • fieldname (String)

    The fieldname of a partattribute object

Returns:

  • (Hash)

    A part attribute hash



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

Examples:

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

Parameters:

  • fieldnames (Array)

    The fieldnames of a partattribute objects

Returns:

  • (Hash)

    A part attribute hash

Raises:

  • (ArgumentError)


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

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)


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

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)


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

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)


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

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)


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

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)


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