Skip to content

Commit

Permalink
refactor: replace OpenStruct with Data for performance and readabilit…
Browse files Browse the repository at this point in the history
…y improvement (#3307)

* Replace OpenStruct with Data in codebase

* Changed commments around for better readability .

* Remove docs.avohq.io submodule

* Lint fix

* Linting Fix

* Linting fix

* Linting Fix

* Fixed Data.Define being only available in ruby 3.2

* Linting Fix

* Reverted Docs Avohq Io Change

* use Struct

* drop ostruct dependency

* Update spec/dummy/app/models/user.rb

---------

Co-authored-by: Paul Bob <[email protected]>
Co-authored-by: Paul Bob <[email protected]>
  • Loading branch information
3 people authored Nov 1, 2024
1 parent 06081ae commit 4123495
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
1 change: 0 additions & 1 deletion lib/avo.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require "zeitwerk"
require "ostruct"
require "net/http"
require_relative "avo/version"
require_relative "avo/engine" if defined?(Rails)
Expand Down
5 changes: 3 additions & 2 deletions lib/avo/licensing/h_q.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion spec/dummy/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?
Expand Down

0 comments on commit 4123495

Please sign in to comment.