BarcodeLookup 1.0.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 +7 -0
  2. data/lib/BarcodeLookup.rb +166 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a28e213b6d64fb8bc3149d00c04748a55381b5f630a274d80d5cb4dcffebef6f
4
+ data.tar.gz: 16f23ea5e1a6380f879a5431641c2af6612a7f034b315145f77632e5bff16cba
5
+ SHA512:
6
+ metadata.gz: 55b32aa5fac33197529cb6c8eb57dbd30894e30da3000318cd6d7fefeb85efa773a15cbaa5c970cab9986f3e47c76e42b53fc24fe6f78979edd2e7665e0ee735
7
+ data.tar.gz: 55a4d15030ba979c2fe59ff36f57b539fa5d2efccdab76bec791fb0339ae382003dd21365b973f4082878db17b969400dd6e9eccb3596803cc630af8ab0dc04d
@@ -0,0 +1,166 @@
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
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: BarcodeLookup
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Relaxed Communications GmbH
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-01-13 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A Ruby Gem for GTIN, UPC, EAN and ISBN barcode lookup and validation
14
+ using the API on ean-search.org
15
+ email: info@relaxedcommunications.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/BarcodeLookup.rb
21
+ homepage: https://github.com/eansearch/ruby-barcode-lookup
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubygems_version: 3.4.20
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: A Ruby Gem for GTIN, UPC, EAN and ISBN barcode lookup and validation
44
+ test_files: []