BarcodeLookup 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/barcode_lookup.rb +80 -23
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ffae6081b44cddfb461e26a8c4e486ff9889eb246405a0203d118f99dfcd0ce0
4
- data.tar.gz: d497f3247141303c8a4278096e2c3a0950d6ab1ac76ab40d037716b8dded6337
3
+ metadata.gz: c2f38b0530ec5be37c36dfa7be524a647bbf9d9be259e4ce279fef52b902063d
4
+ data.tar.gz: 7dc47a5eff0f8c23a07fa16f50768009d84b7136369d3a904c5488be2dbc672a
5
5
  SHA512:
6
- metadata.gz: 429a21ab7846c3701f0bb742ff63a7d1ac1dbeb5be8691e51dd40a3a9520514391f6cb37c5e66ca65c695ce08f745b94f106c0200b3e4cf6363c8d431c1509a5
7
- data.tar.gz: 0dc6d618d4ec754291cc7f2eebca4cb712c5696f9466476655663015c9b33e1c6519ab3de5cd0a1ad890928195f4e0871d0ea1b2662878ae78ee25707c179ba7
6
+ metadata.gz: 6a35999870ff5c6a51db3a3b6060d5c5c2c05b0840a7abd04f8c6882f78bf49b8e73c7f66b984e536f8e24abeac5d7c515b55eac93162b13ef2d34d5c1013ce4
7
+ data.tar.gz: 5e137be0519a9ceb2b29c8cc8f5030168678a0aa9e2d3201fb043246d6afb4fa4cee044798708c7ced44a09347645e7b618de1da7fc08023c1114bc19b0612aa
@@ -8,7 +8,7 @@ class BarcodeLookup
8
8
  class Version # :nodoc:
9
9
  MAJOR = 1
10
10
  MINOR = 0
11
- TINY = 1
11
+ TINY = 2
12
12
 
13
13
  String = [MAJOR, MINOR, TINY].join('.')
14
14
  end
@@ -35,7 +35,7 @@ class BarcodeLookup
35
35
  def barcode_lookup(ean, preferred_lang = 1)
36
36
  json = api_call("op=barcode-lookup&ean=#{ean}&language=#{preferred_lang}")
37
37
  result = JSON.parse(json)
38
- return nil if result.is_a?(Array) && result[0].key?('error')
38
+ return nil if no_result?(result)
39
39
 
40
40
  result[0]
41
41
  end
@@ -47,7 +47,7 @@ class BarcodeLookup
47
47
  def isbn_lookup(isbn)
48
48
  json = api_call("op=barcode-lookup&isbn=#{isbn}")
49
49
  result = JSON.parse(json)
50
- return nil if result.is_a?(Array) && result[0].key?('error')
50
+ return nil if no_result?(result)
51
51
 
52
52
  result[0]
53
53
  end
@@ -61,10 +61,7 @@ class BarcodeLookup
61
61
  def product_search(name, preferred_lang = 1, page = 0)
62
62
  name = CGI.escape(name)
63
63
  json = api_call("op=product-search&name=#{name}&language=#{preferred_lang}&page=#{page}")
64
- result = JSON.parse(json)
65
- raise result[0]['error'] if result.is_a?(Array) && result[0].key?('error')
66
-
67
- result['productlist']
64
+ product_list(JSON.parse(json))
68
65
  end
69
66
 
70
67
  # Search for a similar product by name
@@ -76,10 +73,7 @@ class BarcodeLookup
76
73
  def similar_product_search(name, preferred_lang = 1, page = 0)
77
74
  name = CGI.escape(name)
78
75
  json = api_call("op=similar-product-search&name=#{name}&language=#{preferred_lang}&page=#{page}")
79
- result = JSON.parse(json)
80
- raise result[0]['error'] if result.is_a?(Array) && result[0].key?('error')
81
-
82
- result['productlist']
76
+ product_list(JSON.parse(json))
83
77
  end
84
78
 
85
79
  # Search for a product by category and name (exact match)
@@ -92,10 +86,7 @@ class BarcodeLookup
92
86
  def category_search(category, name, preferred_lang = 1, page = 0)
93
87
  name = CGI.escape(name)
94
88
  json = api_call("op=category-search&category=#{category}&name=#{name}&language=#{preferred_lang}&page=#{page}")
95
- result = JSON.parse(json)
96
- raise result[0]['error'] if result.is_a?(Array) && result[0].key?('error')
97
-
98
- result['productlist']
89
+ product_list(JSON.parse(json))
99
90
  end
100
91
 
101
92
  # Search for all products that start with this barcode prefix
@@ -108,10 +99,7 @@ class BarcodeLookup
108
99
  def barcode_prefix_search(prefix, preferred_lang = 1, page = 0, only_preferred_language = true)
109
100
  only_preferred_language = (only_preferred_language ? 1 : 0)
110
101
  json = api_call("op=barcode-prefix-search&prefix=#{prefix}&language=#{preferred_lang}&only-preferred-language=#{only_preferred_language}&page=#{page}")
111
- result = JSON.parse(json)
112
- raise result[0]['error'] if result.is_a?(Array) && result[0].key?('error')
113
-
114
- result['productlist']
102
+ product_list(JSON.parse(json))
115
103
  end
116
104
 
117
105
  # Lookup the issuing country for a single barcode (GTIN, EAN, UPC or ISBN-13)
@@ -122,7 +110,7 @@ class BarcodeLookup
122
110
  def issuing_country(ean)
123
111
  json = api_call("op=issuing-country&ean=#{ean}")
124
112
  result = JSON.parse(json)
125
- return nil if result.is_a?(Array) && result[0].key?('error')
113
+ return nil if no_result?(result)
126
114
 
127
115
  result[0]['issuingCountry']
128
116
  end
@@ -136,11 +124,66 @@ class BarcodeLookup
136
124
  def barcode_image(ean, width = 102, height = 50)
137
125
  json = api_call("op=barcode-image&ean=#{ean}&width=#{width}&height=#{height}")
138
126
  result = JSON.parse(json)
139
- return nil if result.is_a?(Array) && result[0].key?('error')
127
+ return nil if no_result?(result)
140
128
 
141
129
  result[0]['barcode']
142
130
  end
143
131
 
132
+ # Find the Amazon ASIN for a barcode (GTIN, EAN, UPC or ISBN-13)
133
+ # returns nil if no ASIN was found
134
+ #
135
+ # Arguments:
136
+ # barcode: (String)
137
+ def find_asin_for_ean(ean)
138
+ json = api_call("op=asin-for-ean-lookup&ean=#{ean}")
139
+ result = JSON.parse(json)
140
+ return nil if !result.is_a?(Array) || no_result?(result)
141
+
142
+ result[0]['asin']
143
+ end
144
+
145
+ # Find the barcode (EAN) for an Amazon ASIN
146
+ # returns nil if no barcode was found
147
+ #
148
+ # Arguments:
149
+ # asin: (String)
150
+ def find_ean_for_asin(asin)
151
+ asin = CGI.escape(asin)
152
+ json = api_call("op=ean-for-asin-lookup&asin=#{asin}")
153
+ result = JSON.parse(json)
154
+ return nil if !result.is_a?(Array) || no_result?(result)
155
+
156
+ result[0]['ean']
157
+ end
158
+
159
+ # Find the Library of Congress Control Number (LCCN) for a barcode (GTIN, EAN, UPC or ISBN-13)
160
+ # returns nil if no LCCN was found
161
+ #
162
+ # Arguments:
163
+ # barcode: (String)
164
+ def find_lccn_for_ean(ean)
165
+ json = api_call("op=lccn-for-ean-lookup&ean=#{ean}")
166
+ result = JSON.parse(json)
167
+ return nil if !result.is_a?(Array) || no_result?(result)
168
+
169
+ result[0]['lccn']
170
+ end
171
+
172
+ # Find the barcode (EAN) for a Library of Congress Control Number (LCCN)
173
+ # there can be multiple barcodes for one LCCN, this returns the first one found
174
+ # returns nil if no barcode was found
175
+ #
176
+ # Arguments:
177
+ # lccn: (String)
178
+ def find_ean_for_lccn(lccn)
179
+ lccn = CGI.escape(lccn)
180
+ json = api_call("op=ean-for-lccn-lookup&lccn=#{lccn}")
181
+ result = JSON.parse(json)
182
+ return nil if !result.is_a?(Array) || no_result?(result)
183
+
184
+ result[0]['ean']
185
+ end
186
+
144
187
  # Set the HTTP timeout for API calls in seconds
145
188
  #
146
189
  # Arguments:
@@ -157,17 +200,31 @@ class BarcodeLookup
157
200
 
158
201
  protected
159
202
 
203
+ # true if the API returned an empty list or an error message instead of a result
204
+ def no_result?(result)
205
+ result.is_a?(Array) && (result.empty? || result[0].key?('error'))
206
+ end
207
+
208
+ # the product list of a search result, raises the error message if the API returned one
209
+ def product_list(result)
210
+ return [] if result.is_a?(Array) && result.empty?
211
+ raise result[0]['error'] if result.is_a?(Array) && result[0].key?('error')
212
+
213
+ result['productlist']
214
+ end
215
+
160
216
  def api_call(params, tries = 1)
161
217
  @uri = URI("#{@base_url}#{@token}&#{params}")
162
218
  response = Net::HTTP.start(@uri.host, @uri.port, use_ssl: true, read_timeout: @timeout) do |http|
163
- request = Net::HTTP::Get.new(@uri.request_uri)
219
+ request = Net::HTTP::Get.new(@uri.request_uri, {'User-Agent' => 'ruby-eansearch/1.0'})
164
220
  http.request(request)
165
221
  end
166
222
  if response.code == '429' && tries < @max_api_tries
167
223
  sleep 1
168
224
  return api_call(params, tries + 1)
169
225
  end
170
- @remain = response['X-Credits-Remaining'] if response.key?('X-Credits-Remaining')
226
+ credits = response['X-Credits-Remaining'].to_s.strip
227
+ @remain = credits.to_i if credits.match?(/\A\d+\z/)
171
228
  response.body
172
229
  end
173
230
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: BarcodeLookup
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Relaxed Communications GmbH
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-01-13 00:00:00.000000000 Z
11
+ date: 2026-09-20 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A Ruby Gem for GTIN, UPC, EAN and ISBN barcode lookup and validation
14
14
  using the API on ean-search.org