BarcodeLookup 1.0.0 → 1.0.1
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.
- checksums.yaml +4 -4
- data/lib/barcode_lookup.rb +173 -0
- metadata +3 -3
- data/lib/BarcodeLookup.rb +0 -166
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ffae6081b44cddfb461e26a8c4e486ff9889eb246405a0203d118f99dfcd0ce0
|
4
|
+
data.tar.gz: d497f3247141303c8a4278096e2c3a0950d6ab1ac76ab40d037716b8dded6337
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 429a21ab7846c3701f0bb742ff63a7d1ac1dbeb5be8691e51dd40a3a9520514391f6cb37c5e66ca65c695ce08f745b94f106c0200b3e4cf6363c8d431c1509a5
|
7
|
+
data.tar.gz: 0dc6d618d4ec754291cc7f2eebca4cb712c5696f9466476655663015c9b33e1c6519ab3de5cd0a1ad890928195f4e0871d0ea1b2662878ae78ee25707c179ba7
|
@@ -0,0 +1,173 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'cgi'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
# Provide access to barcode lookup, validation and product search through the EAN-Search.org API
|
6
|
+
class BarcodeLookup
|
7
|
+
|
8
|
+
class Version # :nodoc:
|
9
|
+
MAJOR = 1
|
10
|
+
MINOR = 0
|
11
|
+
TINY = 1
|
12
|
+
|
13
|
+
String = [MAJOR, MINOR, TINY].join('.')
|
14
|
+
end
|
15
|
+
|
16
|
+
# Initialize the class with an API access token from ean-search.org
|
17
|
+
# See https://www.ean-search.org/ean-database-api.html
|
18
|
+
#
|
19
|
+
# Arguments:
|
20
|
+
# api_token: (String)
|
21
|
+
def initialize(api_token)
|
22
|
+
@token = api_token
|
23
|
+
@base_url = 'https://api.ean-search.org/api?format=json&token='
|
24
|
+
@timeout = 180
|
25
|
+
@max_api_tries = 3
|
26
|
+
@remain = -1
|
27
|
+
end
|
28
|
+
|
29
|
+
# Lookup a single barcode (GTIN, EAN, UPC or ISBN-13)
|
30
|
+
# you can optionally specify a preferred language for the product name
|
31
|
+
#
|
32
|
+
# Arguments:
|
33
|
+
# barcode: (String)
|
34
|
+
# language code: (Integer)
|
35
|
+
def barcode_lookup(ean, preferred_lang = 1)
|
36
|
+
json = api_call("op=barcode-lookup&ean=#{ean}&language=#{preferred_lang}")
|
37
|
+
result = JSON.parse(json)
|
38
|
+
return nil if result.is_a?(Array) && result[0].key?('error')
|
39
|
+
|
40
|
+
result[0]
|
41
|
+
end
|
42
|
+
|
43
|
+
# Lookup a single ISBN (ISBN-10)
|
44
|
+
#
|
45
|
+
# Arguments:
|
46
|
+
# isbn: (String)
|
47
|
+
def isbn_lookup(isbn)
|
48
|
+
json = api_call("op=barcode-lookup&isbn=#{isbn}")
|
49
|
+
result = JSON.parse(json)
|
50
|
+
return nil if result.is_a?(Array) && result[0].key?('error')
|
51
|
+
|
52
|
+
result[0]
|
53
|
+
end
|
54
|
+
|
55
|
+
# Search for a product by name (exact match)
|
56
|
+
#
|
57
|
+
# Arguments:
|
58
|
+
# name: (String)
|
59
|
+
# language code: (Integer)
|
60
|
+
# page: (Integer)
|
61
|
+
def product_search(name, preferred_lang = 1, page = 0)
|
62
|
+
name = CGI.escape(name)
|
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']
|
68
|
+
end
|
69
|
+
|
70
|
+
# Search for a similar product by name
|
71
|
+
#
|
72
|
+
# Arguments:
|
73
|
+
# name: (String)
|
74
|
+
# language code: (Integer)
|
75
|
+
# page: (Integer)
|
76
|
+
def similar_product_search(name, preferred_lang = 1, page = 0)
|
77
|
+
name = CGI.escape(name)
|
78
|
+
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']
|
83
|
+
end
|
84
|
+
|
85
|
+
# Search for a product by category and name (exact match)
|
86
|
+
#
|
87
|
+
# Arguments:
|
88
|
+
# category code: (Integer)
|
89
|
+
# name: (String)
|
90
|
+
# language code: (Integer)
|
91
|
+
# page: (Integer)
|
92
|
+
def category_search(category, name, preferred_lang = 1, page = 0)
|
93
|
+
name = CGI.escape(name)
|
94
|
+
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']
|
99
|
+
end
|
100
|
+
|
101
|
+
# Search for all products that start with this barcode prefix
|
102
|
+
#
|
103
|
+
# Arguments:
|
104
|
+
# prefix: (String)
|
105
|
+
# language code: (Integer)
|
106
|
+
# page: (Integer)
|
107
|
+
# only results in the preferred language: (Boolean)
|
108
|
+
def barcode_prefix_search(prefix, preferred_lang = 1, page = 0, only_preferred_language = true)
|
109
|
+
only_preferred_language = (only_preferred_language ? 1 : 0)
|
110
|
+
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']
|
115
|
+
end
|
116
|
+
|
117
|
+
# Lookup the issuing country for a single barcode (GTIN, EAN, UPC or ISBN-13)
|
118
|
+
# this works even if we don't have a product name for this barcode in our database
|
119
|
+
#
|
120
|
+
# Arguments:
|
121
|
+
# barcode: (String)
|
122
|
+
def issuing_country(ean)
|
123
|
+
json = api_call("op=issuing-country&ean=#{ean}")
|
124
|
+
result = JSON.parse(json)
|
125
|
+
return nil if result.is_a?(Array) && result[0].key?('error')
|
126
|
+
|
127
|
+
result[0]['issuingCountry']
|
128
|
+
end
|
129
|
+
|
130
|
+
# Generate a PNG barcode image fort a code (GTIN, EAN, UPC or ISBN-13)
|
131
|
+
#
|
132
|
+
# Arguments:
|
133
|
+
# barcode: (String)
|
134
|
+
# width: (Integer)
|
135
|
+
# height: (Integer)
|
136
|
+
def barcode_image(ean, width = 102, height = 50)
|
137
|
+
json = api_call("op=barcode-image&ean=#{ean}&width=#{width}&height=#{height}")
|
138
|
+
result = JSON.parse(json)
|
139
|
+
return nil if result.is_a?(Array) && result[0].key?('error')
|
140
|
+
|
141
|
+
result[0]['barcode']
|
142
|
+
end
|
143
|
+
|
144
|
+
# Set the HTTP timeout for API calls in seconds
|
145
|
+
#
|
146
|
+
# Arguments:
|
147
|
+
# second: (Integer)
|
148
|
+
def timeout(sec)
|
149
|
+
@timeout = sec
|
150
|
+
end
|
151
|
+
|
152
|
+
# Get the number of credits remaining for API calls
|
153
|
+
# will return -1 before the first API call is made
|
154
|
+
def credits_remaining
|
155
|
+
@remain
|
156
|
+
end
|
157
|
+
|
158
|
+
protected
|
159
|
+
|
160
|
+
def api_call(params, tries = 1)
|
161
|
+
@uri = URI("#{@base_url}#{@token}&#{params}")
|
162
|
+
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)
|
164
|
+
http.request(request)
|
165
|
+
end
|
166
|
+
if response.code == '429' && tries < @max_api_tries
|
167
|
+
sleep 1
|
168
|
+
return api_call(params, tries + 1)
|
169
|
+
end
|
170
|
+
@remain = response['X-Credits-Remaining'] if response.key?('X-Credits-Remaining')
|
171
|
+
response.body
|
172
|
+
end
|
173
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: BarcodeLookup
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Relaxed Communications GmbH
|
@@ -17,7 +17,7 @@ executables: []
|
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
|
-
- lib/
|
20
|
+
- lib/barcode_lookup.rb
|
21
21
|
homepage: https://github.com/eansearch/ruby-barcode-lookup
|
22
22
|
licenses:
|
23
23
|
- MIT
|
@@ -30,7 +30,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 2.4.0
|
34
34
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
35
|
requirements:
|
36
36
|
- - ">="
|
data/lib/BarcodeLookup.rb
DELETED
@@ -1,166 +0,0 @@
|
|
1
|
-
require 'net/http'
|
2
|
-
require 'cgi'
|
3
|
-
require 'json'
|
4
|
-
|
5
|
-
# Provide access to barcode lookup, validation and product search through the EAN-Search.org API
|
6
|
-
class BarcodeLookup
|
7
|
-
|
8
|
-
class Version #:nodoc:
|
9
|
-
Major = 1
|
10
|
-
Minor = 0
|
11
|
-
Tiny = 0
|
12
|
-
|
13
|
-
String = [Major, Minor, Tiny].join('.')
|
14
|
-
end
|
15
|
-
|
16
|
-
# Initialize the class with an API access token from ean-search.org
|
17
|
-
# See https://www.ean-search.org/ean-database-api.html
|
18
|
-
#
|
19
|
-
# Arguments:
|
20
|
-
# api_token: (String)
|
21
|
-
def initialize(api_token)
|
22
|
-
@token = api_token
|
23
|
-
@baseURL = 'https://api.ean-search.org/api?format=json&token='
|
24
|
-
@timeout = 180
|
25
|
-
@max_api_tries = 3
|
26
|
-
@remain = -1
|
27
|
-
end
|
28
|
-
|
29
|
-
# Lookup a single barcode (GTIN, EAN, UPC or ISBN-13)
|
30
|
-
# you can optionally specify a preferred language for the product name
|
31
|
-
#
|
32
|
-
# Arguments:
|
33
|
-
# barcode: (String)
|
34
|
-
# language code: (Integer)
|
35
|
-
def barcode_lookup(ean, preferred_lang = 1)
|
36
|
-
json = api_call("op=barcode-lookup&ean=#{ean}&language=#{preferred_lang}")
|
37
|
-
result = JSON.parse(json)
|
38
|
-
return nil if result.kind_of?(Array) && result[0].key?("error")
|
39
|
-
result[0]
|
40
|
-
end
|
41
|
-
|
42
|
-
# Lookup a single ISBN (ISBN-10)
|
43
|
-
#
|
44
|
-
# Arguments:
|
45
|
-
# isbn: (String)
|
46
|
-
def isbn_lookup(isbn)
|
47
|
-
json = api_call("op=barcode-lookup&isbn=#{isbn}")
|
48
|
-
result = JSON.parse(json)
|
49
|
-
return nil if result.kind_of?(Array) && result[0].key?("error")
|
50
|
-
result[0]
|
51
|
-
end
|
52
|
-
|
53
|
-
# Search for a product by name (exact match)
|
54
|
-
#
|
55
|
-
# Arguments:
|
56
|
-
# name: (String)
|
57
|
-
# language code: (Integer)
|
58
|
-
# page: (Integer)
|
59
|
-
def product_search(name, preferred_lang = 1, page = 0)
|
60
|
-
name = CGI.escape(name)
|
61
|
-
json = api_call("op=product-search&name=#{name}&language=#{preferred_lang}&page=#{page}")
|
62
|
-
result = JSON.parse(json)
|
63
|
-
raise result[0]['error'] if result.kind_of?(Array) && result[0].key?("error")
|
64
|
-
result['productlist']
|
65
|
-
end
|
66
|
-
|
67
|
-
# Search for a similar product by name
|
68
|
-
#
|
69
|
-
# Arguments:
|
70
|
-
# name: (String)
|
71
|
-
# language code: (Integer)
|
72
|
-
# page: (Integer)
|
73
|
-
def similar_product_search(name, preferred_lang = 1, page = 0)
|
74
|
-
name = CGI.escape(name)
|
75
|
-
json = api_call("op=similar-product-search&name=#{name}&language=#{preferred_lang}&page=#{page}")
|
76
|
-
result = JSON.parse(json)
|
77
|
-
raise result[0]['error'] if result.kind_of?(Array) && result[0].key?("error")
|
78
|
-
result['productlist']
|
79
|
-
end
|
80
|
-
|
81
|
-
# Search for a product by category and name (exact match)
|
82
|
-
#
|
83
|
-
# Arguments:
|
84
|
-
# category code: (Integer)
|
85
|
-
# name: (String)
|
86
|
-
# language code: (Integer)
|
87
|
-
# page: (Integer)
|
88
|
-
def category_search(category, name, preferred_lang = 1, page = 0)
|
89
|
-
name = CGI.escape(name)
|
90
|
-
json = api_call("op=category-search&category=#{category}&name=#{name}&language=#{preferred_lang}&page=#{page}")
|
91
|
-
result = JSON.parse(json)
|
92
|
-
raise result[0]['error'] if result.kind_of?(Array) && result[0].key?("error")
|
93
|
-
result['productlist']
|
94
|
-
end
|
95
|
-
|
96
|
-
# Search for all products that start with this barcode prefix
|
97
|
-
#
|
98
|
-
# Arguments:
|
99
|
-
# prefix: (String)
|
100
|
-
# language code: (Integer)
|
101
|
-
# page: (Integer)
|
102
|
-
# only results in the preferred language: (Boolean)
|
103
|
-
def barcode_prefix_search(prefix, preferred_lang = 1, page = 0, only_preferred_language = true)
|
104
|
-
only_preferred_language = (only_preferred_language ? 1 : 0)
|
105
|
-
json = api_call("op=barcode-prefix-search&prefix=#{prefix}&language=#{preferred_lang}&only-preferred-language=#{only_preferred_language}&page=#{page}")
|
106
|
-
result = JSON.parse(json)
|
107
|
-
raise result[0]['error'] if result.kind_of?(Array) && result[0].key?("error")
|
108
|
-
result['productlist']
|
109
|
-
end
|
110
|
-
|
111
|
-
# Lookup the issuing country for a single barcode (GTIN, EAN, UPC or ISBN-13)
|
112
|
-
# this works even if we don't have a product name for this barcode in our database
|
113
|
-
#
|
114
|
-
# Arguments:
|
115
|
-
# barcode: (String)
|
116
|
-
def issuing_country(ean)
|
117
|
-
json = api_call("op=issuing-country&ean=#{ean}")
|
118
|
-
result = JSON.parse(json)
|
119
|
-
return nil if result.kind_of?(Array) && result[0].key?("error")
|
120
|
-
result[0]['issuingCountry']
|
121
|
-
end
|
122
|
-
|
123
|
-
# Generate a PNG barcode image fort a code (GTIN, EAN, UPC or ISBN-13)
|
124
|
-
#
|
125
|
-
# Arguments:
|
126
|
-
# barcode: (String)
|
127
|
-
# width: (Integer)
|
128
|
-
# height: (Integer)
|
129
|
-
def barcode_image(ean, width = 102, height = 50)
|
130
|
-
json = api_call("op=barcode-image&ean=#{ean}&width=#{width}&height=#{height}")
|
131
|
-
result = JSON.parse(json)
|
132
|
-
return nil if result.kind_of?(Array) && result[0].key?("error")
|
133
|
-
result[0]['barcode']
|
134
|
-
end
|
135
|
-
|
136
|
-
# Set the HTTP timeout for API calls in second
|
137
|
-
#
|
138
|
-
# Arguments:
|
139
|
-
# second: (Integer)
|
140
|
-
def set_timeout(sec)
|
141
|
-
# Set HTTP timeout in seconds
|
142
|
-
@timeout = sec
|
143
|
-
end
|
144
|
-
|
145
|
-
# Get the number of credits remaining for API calls
|
146
|
-
# will return -1 before the first API call is made
|
147
|
-
def credits_remaining()
|
148
|
-
return @remain
|
149
|
-
end
|
150
|
-
|
151
|
-
protected
|
152
|
-
|
153
|
-
def api_call(params, tries = 1)
|
154
|
-
@uri = URI(@baseURL.to_s + @token.to_s + '&' + params)
|
155
|
-
response = Net::HTTP.start(@uri.host, @uri.port, use_ssl: true, read_timeout: @timeout) do |http|
|
156
|
-
request = Net::HTTP::Get.new(@uri.request_uri)
|
157
|
-
http.request(request)
|
158
|
-
end
|
159
|
-
if (response.code == "429" && tries < @max_api_tries)
|
160
|
-
sleep 1
|
161
|
-
return api_call(params, tries+1)
|
162
|
-
end
|
163
|
-
@remain = response['X-Credits-Remaining'] if response.key?("X-Credits-Remaining")
|
164
|
-
return response.body
|
165
|
-
end
|
166
|
-
end
|