Class: Valnzbn::Lookup
- Inherits:
-
Object
- Object
- Valnzbn::Lookup
- Defined in:
- lib/valnzbn/lookup.rb
Constant Summary collapse
- PRODUCTION_URL =
'https://api.business.govt.nz/services/v3/nzbn/entities/'- SANDBOX_URL =
'https://sandbox.api.business.govt.nz/services/v3/nzbn/entities/'- @@cache_host =
nil- @@cache_port =
nil- @@access_token =
nil- @@mode =
:sandbox
Class Method Summary collapse
- .access_token=(value) ⇒ Object
- .cache_host=(value) ⇒ Object
- .cache_port=(value) ⇒ Object
- .configure {|_self| ... } ⇒ Object
- .mode=(value) ⇒ Object
- .validate(number, options = {}) ⇒ Object
Class Method Details
.access_token=(value) ⇒ Object
17 18 19 |
# File 'lib/valnzbn/lookup.rb', line 17 def self.access_token=(value) @@access_token = value end |
.cache_host=(value) ⇒ Object
25 26 27 |
# File 'lib/valnzbn/lookup.rb', line 25 def self.cache_host=(value) @@cache_host = value end |
.cache_port=(value) ⇒ Object
29 30 31 |
# File 'lib/valnzbn/lookup.rb', line 29 def self.cache_port=(value) @@cache_port = value end |
.configure {|_self| ... } ⇒ Object
33 34 35 |
# File 'lib/valnzbn/lookup.rb', line 33 def self.configure yield self end |
.mode=(value) ⇒ Object
21 22 23 |
# File 'lib/valnzbn/lookup.rb', line 21 def self.mode=(value) @@mode = value.to_sym end |
.validate(number, options = {}) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/valnzbn/lookup.rb', line 37 def self.validate(number, = {}) url = @@mode == :sandbox ? SANDBOX_URL : PRODUCTION_URL number = number.to_s.gsub(/\W/, '') if Valnzbn::Utils.valid_format?(number) cache = if @@cache_port && @@cache_host redis_client = Redis.new(host: @@cache_host, port: @@cache_port, db: 15) namespaced_redis = Redis::Namespace.new(:valnzbn_cache, redis: redis_client) JSON.parse(namespaced_redis.get(number) || '{}', symbolize_names: true) else {} end if cache.any? && Time.at(cache[:expires_at]) > Time.now cache else begin response = HTTParty.get("#{url}#{number}", headers: { 'Authorization' => "Bearer #{@@access_token}" }).parsed_response response = JSON.parse(response.to_json, symbolize_names: true) response[:expires_at] = (Time.now + 7200).to_i namespaced_redis.set(number, response.to_json) if @@cache_port && @@cache_host response rescue nil end end else {} end end |