octopart-ruby 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +14 -0
- data/VERSION +1 -1
- data/lib/octopart-ruby.rb +2 -7
- data/lib/octopart/client.rb +31 -22
- data/lib/octopart/exceptions.rb +9 -0
- data/octopart-ruby.gemspec +4 -2
- data/test/test_octopart_ruby.rb +5 -5
- metadata +19 -17
data/CHANGELOG.md
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
0.1.2
|
2
|
+
-----
|
3
|
+
* [Added CHANGELOG.md](https://github.com/parkerboundy/octopart-ruby/commit/840ec6f89f0d5d07a10f77251a8571f2521a4c0b)
|
4
|
+
* [Move APIKeyNotSetError and APIResponseError to exceptions.rb](https://github.com/parkerboundy/octopart-ruby/commit/1860324c6ee4f9ab5348643583f03ac33579de50)
|
5
|
+
* [Abstracted requests into make_request(endpoint, query)](https://github.com/parkerboundy/octopart-ruby/commit/fe74cc9c72c75ef757e71e4ad9f422b9a6e1fe1c)
|
6
|
+
|
7
|
+
0.1.1
|
8
|
+
-----
|
9
|
+
* [Cleaned up match method](https://github.com/parkerboundy/octopart-ruby/commit/b64ac2970cdf353e709819c6d4eabe5bd1b7251c)
|
10
|
+
* Cleaned up documentation and converted to YARD
|
11
|
+
|
12
|
+
0.1.0
|
13
|
+
-----
|
14
|
+
* [Initial release](https://github.com/parkerboundy/octopart-ruby/commit/08c5a30e3558dfb8895748bbfccf677788da4a66)
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.2
|
data/lib/octopart-ruby.rb
CHANGED
@@ -1,11 +1,5 @@
|
|
1
1
|
require 'httparty'
|
2
2
|
|
3
|
-
# APIKeyNotSetError is called when a client is instantiated without an api_key
|
4
|
-
class APIKeyNotSetError < StandardError; end
|
5
|
-
|
6
|
-
# APIResponseError is called when an API request returns with a code other than 200
|
7
|
-
class APIResponseError < StandardError; end
|
8
|
-
|
9
3
|
module Octopart
|
10
4
|
|
11
5
|
# api_key - The API key to use
|
@@ -23,4 +17,5 @@ end
|
|
23
17
|
|
24
18
|
directory = File.expand_path(File.dirname(__FILE__))
|
25
19
|
|
26
|
-
require File.join(directory, 'octopart', 'client')
|
20
|
+
require File.join(directory, 'octopart', 'client')
|
21
|
+
require File.join(directory, 'octopart', 'exceptions')
|
data/lib/octopart/client.rb
CHANGED
@@ -25,7 +25,6 @@ module Octopart
|
|
25
25
|
def initialize(api_key=nil)
|
26
26
|
@api_key = api_key
|
27
27
|
@api_key ||= Octopart.api_key
|
28
|
-
|
29
28
|
end
|
30
29
|
|
31
30
|
# Fetch a category object by its id
|
@@ -39,8 +38,7 @@ module Octopart
|
|
39
38
|
if id.is_a? Array
|
40
39
|
categories(id)
|
41
40
|
else
|
42
|
-
|
43
|
-
validate_response(response)
|
41
|
+
make_request('/categories/get', {:id => id})
|
44
42
|
end
|
45
43
|
end
|
46
44
|
|
@@ -53,8 +51,8 @@ module Octopart
|
|
53
51
|
# # => [Hash]
|
54
52
|
def categories(ids)
|
55
53
|
raise(ArgumentError, 'ids must be an array') unless ids.is_a?Array
|
56
|
-
|
57
|
-
|
54
|
+
query = {:ids => "[#{ids.join(",")}]"}
|
55
|
+
make_request('/categories/get_multi', query)
|
58
56
|
end
|
59
57
|
|
60
58
|
# Execute search over category objects
|
@@ -68,8 +66,8 @@ module Octopart
|
|
68
66
|
# # => [Hash]
|
69
67
|
def search_categories(query, start=0, limit=10)
|
70
68
|
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))
|
71
|
-
|
72
|
-
|
69
|
+
query = {:q => query, :start => start, :limit => limit}
|
70
|
+
make_request('/categories/search', query)
|
73
71
|
end
|
74
72
|
|
75
73
|
# Fetch a part object by its id
|
@@ -83,8 +81,8 @@ module Octopart
|
|
83
81
|
if uid.is_a? Array
|
84
82
|
parts(uid)
|
85
83
|
else
|
86
|
-
|
87
|
-
|
84
|
+
query = {:uid => uid}
|
85
|
+
make_request('/parts/get', query)
|
88
86
|
end
|
89
87
|
end
|
90
88
|
|
@@ -97,8 +95,8 @@ module Octopart
|
|
97
95
|
# # => [Hash]
|
98
96
|
def parts(uids)
|
99
97
|
raise(ArgumentError, 'uids must be an array') unless uids.is_a?Array
|
100
|
-
|
101
|
-
|
98
|
+
query = {:uids => "[#{uids.join(",")}]"}
|
99
|
+
make_request('/parts/get_multi', query)
|
102
100
|
end
|
103
101
|
|
104
102
|
# Execute search over part objects
|
@@ -118,8 +116,8 @@ module Octopart
|
|
118
116
|
# # => [Hash]
|
119
117
|
def search_parts(query, start=0, limit=10)
|
120
118
|
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))
|
121
|
-
|
122
|
-
|
119
|
+
query = {:q => query, :start => start, :limit => limit}
|
120
|
+
make_request('/parts/search', query)
|
123
121
|
end
|
124
122
|
|
125
123
|
# Suggest a part search query string
|
@@ -135,7 +133,8 @@ module Octopart
|
|
135
133
|
# # => [Hash]
|
136
134
|
def suggest_parts(query, limit=5)
|
137
135
|
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))
|
138
|
-
|
136
|
+
query = {:q => query.split(' ').join('+'), :limit => limit}
|
137
|
+
make_request('/parts/suggest', query)
|
139
138
|
end
|
140
139
|
|
141
140
|
# Match (manufacturer,mpn) to part uids
|
@@ -147,8 +146,8 @@ module Octopart
|
|
147
146
|
# match_part('Texas Instruments', 'SN74LS240N')
|
148
147
|
# # => [Hash]
|
149
148
|
def match_part(manufacturer_name, mpn)
|
150
|
-
|
151
|
-
|
149
|
+
query = {:manufacturer_name => manufacturer_name, :mpn => mpn}
|
150
|
+
make_request('/parts/match', query)
|
152
151
|
end
|
153
152
|
|
154
153
|
# Fetch a partattribute object by its id
|
@@ -162,8 +161,8 @@ module Octopart
|
|
162
161
|
if fieldname.is_a? Array
|
163
162
|
part_attributes(fieldname)
|
164
163
|
else
|
165
|
-
|
166
|
-
|
164
|
+
query = {:fieldname => fieldname}
|
165
|
+
make_request('/partattributes/get', query)
|
167
166
|
end
|
168
167
|
end
|
169
168
|
|
@@ -176,8 +175,8 @@ module Octopart
|
|
176
175
|
# # => partattribute hash
|
177
176
|
def part_attributes(fieldnames)
|
178
177
|
raise(ArgumentError, 'fieldnames must be an array') unless fieldnames.is_a?Array
|
179
|
-
|
180
|
-
|
178
|
+
query = {:fieldnames => "["+fieldnames.map{|v| "\"#{v}\""}.join(',')+"]"}
|
179
|
+
make_request('/partattributes/get_multi', query)
|
181
180
|
end
|
182
181
|
|
183
182
|
# Match lines of a BOM to parts
|
@@ -198,8 +197,8 @@ module Octopart
|
|
198
197
|
# # => [Hash]
|
199
198
|
def bom_match(lines)
|
200
199
|
raise(ArgumentError, 'lines must be a hash') unless lines.is_a?(::Hash)
|
201
|
-
|
202
|
-
|
200
|
+
query = {:lines => "[{"+lines.map{|k,v| "\"#{k}\":\"#{v}\""}.join(',')+"}]"}
|
201
|
+
make_request('/bom/match', query)
|
203
202
|
end
|
204
203
|
|
205
204
|
# Helper method for searches
|
@@ -232,10 +231,20 @@ module Octopart
|
|
232
231
|
alias_method :match, :match_part
|
233
232
|
|
234
233
|
protected
|
234
|
+
def make_request(endpoint, query)
|
235
|
+
query = default_options.merge(query)
|
236
|
+
response = self.class.get(endpoint.to_s, :query => query)
|
237
|
+
validate_response(response)
|
238
|
+
end
|
239
|
+
|
235
240
|
def validate_response(response)
|
236
241
|
response.code == 200 ? response.parsed_response : raise(APIResponseError, response.code)
|
237
242
|
end
|
238
243
|
|
244
|
+
def default_options
|
245
|
+
{:apikey => @api_key}
|
246
|
+
end
|
247
|
+
|
239
248
|
end
|
240
249
|
|
241
250
|
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
module Octopart
|
2
|
+
|
3
|
+
# APIKeyNotSetError is called when a client is instantiated without an api_key
|
4
|
+
class APIKeyNotSetError < StandardError; end
|
5
|
+
|
6
|
+
# APIResponseError is called when an API request returns with a code other than 200
|
7
|
+
class APIResponseError < StandardError; end
|
8
|
+
|
9
|
+
end
|
data/octopart-ruby.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "octopart-ruby"
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Parker Boundy"]
|
12
|
-
s.date = "2012-12-
|
12
|
+
s.date = "2012-12-24"
|
13
13
|
s.description = "Ruby wrapper for the Octopart API"
|
14
14
|
s.email = "parkerboundy@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
|
|
18
18
|
]
|
19
19
|
s.files = [
|
20
20
|
".document",
|
21
|
+
"CHANGELOG.md",
|
21
22
|
"Gemfile",
|
22
23
|
"Gemfile.lock",
|
23
24
|
"LICENSE.txt",
|
@@ -27,6 +28,7 @@ Gem::Specification.new do |s|
|
|
27
28
|
"examples/example.rb",
|
28
29
|
"lib/octopart-ruby.rb",
|
29
30
|
"lib/octopart/client.rb",
|
31
|
+
"lib/octopart/exceptions.rb",
|
30
32
|
"octopart-ruby.gemspec",
|
31
33
|
"test/fixtures/attribute.json",
|
32
34
|
"test/fixtures/attribute_multi.json",
|
data/test/test_octopart_ruby.rb
CHANGED
@@ -7,7 +7,7 @@ class TestOctopartRuby < Test::Unit::TestCase
|
|
7
7
|
should "require an api key" do
|
8
8
|
Octopart.api_key = nil
|
9
9
|
|
10
|
-
assert_raise APIKeyNotSetError do
|
10
|
+
assert_raise Octopart::APIKeyNotSetError do
|
11
11
|
Octopart::Client.new()
|
12
12
|
end
|
13
13
|
end
|
@@ -96,7 +96,7 @@ class TestOctopartRuby < Test::Unit::TestCase
|
|
96
96
|
should "respond to error codes properly" do
|
97
97
|
FakeWeb.register_uri(:get, "http://octopart.com/api/v2/categories/get?id=4174&apikey=123456789", :body => fixture_file("category.json"), :status => ["404", "Not Found"])
|
98
98
|
|
99
|
-
assert_raise APIResponseError do
|
99
|
+
assert_raise Octopart::APIResponseError do
|
100
100
|
@client.category(4174)
|
101
101
|
end
|
102
102
|
end
|
@@ -173,7 +173,7 @@ class TestOctopartRuby < Test::Unit::TestCase
|
|
173
173
|
should "respond to error codes properly" do
|
174
174
|
FakeWeb.register_uri(:get, "http://octopart.com/api/v2/parts/get?uid=39619421&apikey=123456789", :body => fixture_file("part.json"), :status => ["404", "Not Found"])
|
175
175
|
|
176
|
-
assert_raise APIResponseError do
|
176
|
+
assert_raise Octopart::APIResponseError do
|
177
177
|
@client.part(39619421)
|
178
178
|
end
|
179
179
|
end
|
@@ -216,7 +216,7 @@ class TestOctopartRuby < Test::Unit::TestCase
|
|
216
216
|
should "respond to error codes properly" do
|
217
217
|
FakeWeb.register_uri(:get, "http://octopart.com/api/v2/partattributes/get?fieldname=capacitance&apikey=123456789", :body => fixture_file("attribute.json"), :status => ["404", "Not Found"])
|
218
218
|
|
219
|
-
assert_raise APIResponseError do
|
219
|
+
assert_raise Octopart::APIResponseError do
|
220
220
|
@client.part_attribute('capacitance')
|
221
221
|
end
|
222
222
|
end
|
@@ -244,7 +244,7 @@ class TestOctopartRuby < Test::Unit::TestCase
|
|
244
244
|
should "respond to error codes properly" do
|
245
245
|
FakeWeb.register_uri(:get, "http://octopart.com/api/v2/bom/match?lines=%5B%7B%22mpn%22%3A%22SN74LS240N%22%2C%22manufacturer%22%3A%22Texas%20Instruments%22%7D%5D&apikey=123456789", :body => fixture_file("bom.json"), :status => ["404", "Not Found"])
|
246
246
|
|
247
|
-
assert_raise APIResponseError do
|
247
|
+
assert_raise Octopart::APIResponseError do
|
248
248
|
@client.bom_match({"mpn" => "SN74LS240N", "manufacturer" => "Texas Instruments"})
|
249
249
|
end
|
250
250
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: octopart-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-12-
|
12
|
+
date: 2012-12-24 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: httparty
|
16
|
-
requirement: &
|
16
|
+
requirement: &70142811581320 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - =
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 0.9.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70142811581320
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: shoulda
|
27
|
-
requirement: &
|
27
|
+
requirement: &70142811580820 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70142811580820
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: yard
|
38
|
-
requirement: &
|
38
|
+
requirement: &70142811580320 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 0.8.3
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70142811580320
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: bundler
|
49
|
-
requirement: &
|
49
|
+
requirement: &70142811578640 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '1.0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70142811578640
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: jeweler
|
60
|
-
requirement: &
|
60
|
+
requirement: &70142811578100 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ~>
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: 1.8.4
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70142811578100
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: simplecov
|
71
|
-
requirement: &
|
71
|
+
requirement: &70142811569780 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ~>
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: 0.7.1
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *70142811569780
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: fakeweb
|
82
|
-
requirement: &
|
82
|
+
requirement: &70142811568700 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ~>
|
@@ -87,7 +87,7 @@ dependencies:
|
|
87
87
|
version: 1.3.0
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *70142811568700
|
91
91
|
description: Ruby wrapper for the Octopart API
|
92
92
|
email: parkerboundy@gmail.com
|
93
93
|
executables: []
|
@@ -97,6 +97,7 @@ extra_rdoc_files:
|
|
97
97
|
- README.rdoc
|
98
98
|
files:
|
99
99
|
- .document
|
100
|
+
- CHANGELOG.md
|
100
101
|
- Gemfile
|
101
102
|
- Gemfile.lock
|
102
103
|
- LICENSE.txt
|
@@ -106,6 +107,7 @@ files:
|
|
106
107
|
- examples/example.rb
|
107
108
|
- lib/octopart-ruby.rb
|
108
109
|
- lib/octopart/client.rb
|
110
|
+
- lib/octopart/exceptions.rb
|
109
111
|
- octopart-ruby.gemspec
|
110
112
|
- test/fixtures/attribute.json
|
111
113
|
- test/fixtures/attribute_multi.json
|
@@ -135,7 +137,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
135
137
|
version: '0'
|
136
138
|
segments:
|
137
139
|
- 0
|
138
|
-
hash:
|
140
|
+
hash: -4320709216140336670
|
139
141
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
140
142
|
none: false
|
141
143
|
requirements:
|