Skip to content

Commit a28c112

Browse files
authored
Allow users to configure the api client (#5)
* Add Client and Configuration classes for API interaction - Introduced the Client class as the main entry point for interacting with the PromoStandards API. - Created the Configuration class to manage configuration options. * Refactor: Move files to PromostandardsRubyClient namespace - Updated file structure and module names accordingly. - Adjusted gemspec to reflect the new naming convention. - Updated RSpec tests to use the new module name.
1 parent 847c867 commit a28c112

File tree

11 files changed

+248
-39
lines changed

11 files changed

+248
-39
lines changed

lib/promostandards/ruby/client.rb

Lines changed: 0 additions & 12 deletions
This file was deleted.

lib/promostandards/ruby/client/version.rb

Lines changed: 0 additions & 9 deletions
This file was deleted.

lib/promostandards_ruby_client.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
# PromoStandardsRubyClient is an unofficial Ruby client for interacting with
4+
# PromoStandards API services, providing a flexible configuration interface
5+
# for accessing services such as Product Data, Media Content, and Pricing.
6+
#
7+
# This gem allows users to set up a client with options for service URL and
8+
# logging, making it easy to integrate PromoStandards functionality into
9+
# Ruby applications.
10+
module PromoStandardsRubyClient
11+
require_relative "promostandards_ruby_client/version"
12+
require_relative "promostandards_ruby_client/client"
13+
require_relative "promostandards_ruby_client/configuration"
14+
end
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# frozen_string_literal: true
2+
3+
module PromostandardsRubyClient
4+
# The Client class is the main entry point for interacting with the
5+
# PromoStandards API through the gem.
6+
# This class provides flexible configuration options, allowing users
7+
# to specify the API service URL, logger, and other settings either
8+
# globally or per instance.
9+
#
10+
# == Configuration
11+
#
12+
# Configuration can be set globally across the application, typically
13+
# in a Rails initializer:
14+
#
15+
# # config/initializers/promostandards_ruby_client.rb
16+
# PromostandardsRubyClient::Client.configure do |config|
17+
# config.service_url = 'https://custom-service-url.com'
18+
# config.logger = Rails.logger
19+
# end
20+
#
21+
# Alternatively, options can be passed directly to an instance of the
22+
# Client:
23+
#
24+
# client = PromostandardsRubyClient::Client.new(
25+
# service_url: 'https://another-service-url.com',
26+
# logger: custom_logger
27+
# )
28+
#
29+
# == Attributes
30+
#
31+
# * +service_url+ - The base URL for the PromoStandards-compatible
32+
# service, set through global configuration or passed to the instance.
33+
# Defaults to nil and raises an error if not set.
34+
#
35+
# * +logger+ - A logger object with an API compatible with Rails.logger,
36+
# used for logging API interactions and other messages.
37+
#
38+
# == Example Usage
39+
#
40+
# # Configure the client globally in a Rails initializer
41+
# PromostandardsRubyClient::Client.configure do |config|
42+
# config.service_url = 'https://custom-service-url.com'
43+
# config.logger = Rails.logger
44+
# end
45+
#
46+
# # Instantiate the client
47+
# client = PromostandardsRubyClient::Client.new
48+
#
49+
# # Or override configuration settings at instance level
50+
# options = { service_url: 'https://another-service-url.com' }
51+
# client = PromostandardsRubyClient::Client.new(options)
52+
class Client
53+
class << self
54+
attr_accessor :configuration
55+
end
56+
57+
# Allows global configuration of the Client class. When called with a
58+
# block, it yields an instance of Configuration, enabling settings like
59+
# +service_url+ and +logger+ to be set for the entire application.
60+
#
61+
# @example Configuring the client globally
62+
# PromostandardsRubyClient::Client.configure do |config|
63+
# config.service_url = 'https://custom-service-url.com'
64+
# config.logger = Rails.logger
65+
# end
66+
#
67+
# @yield [Configuration] Yields the configuration instance to the block.
68+
def self.configure
69+
self.configuration ||= Configuration.new
70+
yield(configuration)
71+
end
72+
73+
# Initializes a new instance of Client with the specified options.
74+
# If options are not provided, defaults to the global configuration
75+
# or, if none exists, falls back to Configuration defaults.
76+
#
77+
# @param options [Hash] Optional settings to configure the client
78+
# instance.
79+
# @option options [String] :service_url URL where the PromoStandards
80+
# API compatible service is hosted.
81+
# @option options [Logger] :logger Custom logger for this client
82+
# instance.
83+
def initialize(options = {})
84+
config = self.class.configuration || Configuration.new
85+
@service_url = options[:service_url] || config.service_url
86+
@logger = options[:logger] || config.logger
87+
88+
raise ArgumentError, "service_url must be set" if @service_url.nil?
89+
end
90+
end
91+
end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# frozen_string_literal: true
2+
3+
require "logger"
4+
5+
module PromostandardsRubyClient
6+
# The Configuration class provides a way to set up and manage configuration
7+
# options for the gem.
8+
#
9+
# == Usage
10+
#
11+
# Configuration can be set for the application in a Rails initializer:
12+
#
13+
# # config/initializers/promostandards_ruby_client.rb
14+
# PromostandardsRubyClient::Client.configure do |config|
15+
# config.service_url = 'https://custom-service-url.com'
16+
# config.logger = Rails.logger
17+
# end
18+
#
19+
# Alternatively, configuration options can be passed directly when
20+
# creating an instance of the client:
21+
#
22+
# client = PromostandardsRubyClient::Client.new(
23+
# service_url: 'https://another-service-url.com',
24+
# logger: custom_logger
25+
# )
26+
#
27+
# == Attributes
28+
#
29+
# * +service_url+ - The base URL for the PromoStandards-compatible
30+
# service.
31+
#
32+
# * +logger+ - A logger with an API compatible with Rails.logger (
33+
# responding to +info+, +warn+, +error+, etc.). Defaults to a standard
34+
# Logger instance outputting to STDOUT.
35+
#
36+
class Configuration
37+
# The base URL for the PromoStandards-compatible service.
38+
# This attribute is required; it must be set in either global
39+
# configuration or directly passed to the client instance. If not set,
40+
# an error will be raised.
41+
attr_accessor :service_url
42+
# A logger with an API compatible with Rails.logger (responding to
43+
# +info+, +warn+, +error+, etc.). Defaults to a standard Logger
44+
# instance outputting to STDOUT.
45+
attr_accessor :logger
46+
47+
def initialize
48+
@logger = ::Logger.new($stdout)
49+
end
50+
end
51+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# frozen_string_literal: true
2+
3+
module PromostandardsRubyClient
4+
VERSION = "0.1.0"
5+
end

promostandards-ruby-client.gemspec

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# frozen_string_literal: true
22

3-
require_relative "lib/promostandards/ruby/client/version"
3+
require_relative "lib/promostandards_ruby_client/version"
44

55
Gem::Specification.new do |spec|
66
spec.name = "promostandards-ruby-client"
7-
spec.version = Promostandards::Ruby::Client::VERSION
7+
spec.version = PromostandardsRubyClient::VERSION
88
spec.authors = ["quintsys"]
99
spec.email = ["[email protected]"]
1010

@@ -36,4 +36,6 @@ Gem::Specification.new do |spec|
3636
spec.bindir = "exe"
3737
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
3838
spec.require_paths = ["lib"]
39+
40+
spec.add_dependency("logger")
3941
end

sig/promostandards/ruby/client.rbs

Lines changed: 0 additions & 8 deletions
This file was deleted.

spec/promostandards/ruby/client_spec.rb

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe PromostandardsRubyClient::Client do
4+
let(:custom_logger) { Logger.new("/dev/null") }
5+
let(:custom_service_url) { "https://custom-service-url.com" }
6+
7+
after do
8+
described_class.configuration = nil
9+
end
10+
11+
it "has a version number" do
12+
expect(PromostandardsRubyClient::VERSION).not_to be_nil
13+
end
14+
15+
describe ".configure" do
16+
before do
17+
described_class.configure do |config|
18+
config.service_url = custom_service_url
19+
config.logger = custom_logger
20+
end
21+
end
22+
23+
it "allows setting a global service_url" do
24+
expect(described_class.configuration.service_url)
25+
.to eq(custom_service_url)
26+
end
27+
28+
it "allows setting a global logger" do
29+
expect(described_class.configuration.logger).to eq(custom_logger)
30+
end
31+
end
32+
33+
describe "#initialize" do
34+
context "with global configuration" do
35+
before do
36+
described_class.configure do |config|
37+
config.service_url = custom_service_url
38+
config.logger = custom_logger
39+
end
40+
end
41+
42+
it "uses global configuration for service_url" do
43+
client = described_class.new
44+
expect(client.instance_variable_get(:@service_url))
45+
.to(eq(custom_service_url))
46+
end
47+
48+
it "uses global configuration for logger" do
49+
client = described_class.new
50+
expect(client.instance_variable_get(:@logger)).to eq(custom_logger)
51+
end
52+
53+
it "overrides global configuration with instance options" do
54+
alternate_service_url = "https://another-service-url.com"
55+
client = described_class.new(service_url: alternate_service_url)
56+
expect(client.instance_variable_get(:@service_url))
57+
.to(eq(alternate_service_url))
58+
end
59+
end
60+
61+
context "without global configuration" do
62+
it "raises an error if service_url is not provided" do
63+
expect do
64+
described_class.new
65+
end.to raise_error(ArgumentError, "service_url must be set")
66+
end
67+
68+
it "uses provided service_url if given" do
69+
client = described_class.new(service_url: custom_service_url,
70+
logger: custom_logger)
71+
expect(client.instance_variable_get(:@service_url))
72+
.to(eq(custom_service_url))
73+
end
74+
75+
it "uses provided logger if given" do
76+
client = described_class.new(service_url: custom_service_url,
77+
logger: custom_logger)
78+
expect(client.instance_variable_get(:@logger)).to eq(custom_logger)
79+
end
80+
end
81+
end
82+
end

spec/spec_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
require "promostandards/ruby/client"
3+
require "promostandards_ruby_client"
44

55
RSpec.configure do |config|
66
# Enable flags like --only-failures and --next-failure

0 commit comments

Comments
 (0)