diff --git a/lib/avo.rb b/lib/avo.rb index ee4f7ee48..d2bb5de06 100644 --- a/lib/avo.rb +++ b/lib/avo.rb @@ -1,5 +1,4 @@ require "zeitwerk" -require "ostruct" require "net/http" require_relative "avo/version" require_relative "avo/engine" if defined?(Rails) diff --git a/lib/avo/licensing/h_q.rb b/lib/avo/licensing/h_q.rb index a2b9668e7..26f8cc9d3 100644 --- a/lib/avo/licensing/h_q.rb +++ b/lib/avo/licensing/h_q.rb @@ -7,6 +7,7 @@ class HQ ENDPOINT = "https://v3.avohq.io/api/v3/licenses/check".freeze unless const_defined?(:ENDPOINT) REQUEST_TIMEOUT = 5 unless const_defined?(:REQUEST_TIMEOUT) # seconds CACHE_TIME = 6.hours.to_i unless const_defined?(:CACHE_TIME) # seconds + RESPONSE_STRUCT = Struct.new(:code, :body) unless const_defined?(:RESPONSE_STRUCT) class << self def cache_key @@ -116,7 +117,7 @@ def perform_and_cache_request case hq_response.code.to_i when 500 - cache_and_return_error "Avo HQ Internal server error.", hq_response.body if hq_response.code == 500 + cache_and_return_error "Avo HQ Internal server error.", hq_response.body when 200 cache_response response: JSON.parse(hq_response.body) else @@ -157,7 +158,7 @@ def perform_request Avo.logger.debug "Performing request to avohq.io API to check license availability." if Rails.env.development? if Rails.env.test? - OpenStruct.new({code: 200, body: "{\"id\":\"pro\",\"valid\":true}"}) + RESPONSE_STRUCT.new(200, "{\"id\":\"pro\",\"valid\":true}") else Avo::Licensing::Request.post ENDPOINT, body: payload.to_json, timeout: REQUEST_TIMEOUT end diff --git a/spec/dummy/app/models/user.rb b/spec/dummy/app/models/user.rb index 82625e871..f57b85937 100644 --- a/spec/dummy/app/models/user.rb +++ b/spec/dummy/app/models/user.rb @@ -20,6 +20,8 @@ # slug :string # class User < ApplicationRecord + ACCOUNT_STRUCT = Struct.new(:id, :name) unless const_defined?(:ACCOUNT_STRUCT) + extend FriendlyId # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable @@ -84,7 +86,10 @@ def self.ransackable_attributes(auth_object = nil) # Simulate accounts association def accounts - [OpenStruct.new(id: 1, name: "Foo"), OpenStruct.new(id: 2, name: "Bar")] + [ + ACCOUNT_STRUCT.new(1, "Foo"), + ACCOUNT_STRUCT.new(2, "Bar") + ] end def is_developer?