Class: Cloudflare::Artifacts::Configuration
- Inherits:
-
Object
- Object
- Cloudflare::Artifacts::Configuration
- Defined in:
- lib/cloudflare/artifacts/configuration.rb
Constant Summary collapse
- DEFAULT_BASE_URL =
"https://api.cloudflare.com"- DEFAULT_NAMESPACE =
"default"- DEFAULT_TIMEOUT =
30
Instance Attribute Summary collapse
-
#account_id ⇒ Object
Returns the value of attribute account_id.
-
#adapter ⇒ Object
Returns the value of attribute adapter.
-
#api_token ⇒ Object
Returns the value of attribute api_token.
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#namespace ⇒ Object
Returns the value of attribute namespace.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
-
#user_agent ⇒ Object
Returns the value of attribute user_agent.
Class Method Summary collapse
Instance Method Summary collapse
- #assign(options) ⇒ Object
-
#initialize(base_url: DEFAULT_BASE_URL, namespace: DEFAULT_NAMESPACE, timeout: DEFAULT_TIMEOUT, **options) ⇒ Configuration
constructor
A new instance of Configuration.
- #merge(**options) ⇒ Object
- #to_h ⇒ Object
- #validate! ⇒ Object
Constructor Details
#initialize(base_url: DEFAULT_BASE_URL, namespace: DEFAULT_NAMESPACE, timeout: DEFAULT_TIMEOUT, **options) ⇒ Configuration
Returns a new instance of Configuration.
50 51 52 53 54 55 56 57 |
# File 'lib/cloudflare/artifacts/configuration.rb', line 50 def initialize(base_url: DEFAULT_BASE_URL, namespace: DEFAULT_NAMESPACE, timeout: DEFAULT_TIMEOUT, **) @base_url = base_url @namespace = namespace @timeout = timeout @adapter = Faraday.default_adapter @user_agent = "cloudflare-artifacts-ruby/#{VERSION}" assign() end |
Instance Attribute Details
#account_id ⇒ Object
Returns the value of attribute account_id.
10 11 12 |
# File 'lib/cloudflare/artifacts/configuration.rb', line 10 def account_id @account_id end |
#adapter ⇒ Object
Returns the value of attribute adapter.
10 11 12 |
# File 'lib/cloudflare/artifacts/configuration.rb', line 10 def adapter @adapter end |
#api_token ⇒ Object
Returns the value of attribute api_token.
10 11 12 |
# File 'lib/cloudflare/artifacts/configuration.rb', line 10 def api_token @api_token end |
#base_url ⇒ Object
Returns the value of attribute base_url.
10 11 12 |
# File 'lib/cloudflare/artifacts/configuration.rb', line 10 def base_url @base_url end |
#namespace ⇒ Object
Returns the value of attribute namespace.
10 11 12 |
# File 'lib/cloudflare/artifacts/configuration.rb', line 10 def namespace @namespace end |
#timeout ⇒ Object
Returns the value of attribute timeout.
10 11 12 |
# File 'lib/cloudflare/artifacts/configuration.rb', line 10 def timeout @timeout end |
#user_agent ⇒ Object
Returns the value of attribute user_agent.
10 11 12 |
# File 'lib/cloudflare/artifacts/configuration.rb', line 10 def user_agent @user_agent end |
Class Method Details
.from_env ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/cloudflare/artifacts/configuration.rb', line 14 def from_env new( base_url: ENV.fetch("CLOUDFLARE_API_BASE_URL", DEFAULT_BASE_URL), namespace: ENV.fetch("CLOUDFLARE_ARTIFACTS_NAMESPACE", DEFAULT_NAMESPACE), timeout: ENV.fetch("CLOUDFLARE_ARTIFACTS_TIMEOUT", DEFAULT_TIMEOUT).to_i, account_id: ENV.fetch("CLOUDFLARE_ACCOUNT_ID", nil), api_token: ENV.fetch("CLOUDFLARE_API_TOKEN", nil) ) end |
.from_file(path) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/cloudflare/artifacts/configuration.rb', line 24 def from_file(path) data = load_file(path) raise ArgumentError, "expected a hash in #{path}, got #{data.class}" unless data.is_a?(Hash) new(**symbolize_keys(data)) end |
Instance Method Details
#assign(options) ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/cloudflare/artifacts/configuration.rb', line 59 def assign() .each do |key, value| setter = "#{key}=" raise ArgumentError, "unknown configuration option: #{key}" unless respond_to?(setter) public_send(setter, value) end self end |
#merge(**options) ⇒ Object
69 70 71 |
# File 'lib/cloudflare/artifacts/configuration.rb', line 69 def merge(**) dup.assign() end |
#to_h ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/cloudflare/artifacts/configuration.rb', line 73 def to_h { account_id: account_id, api_token: api_token, namespace: namespace, base_url: base_url, adapter: adapter, timeout: timeout, user_agent: user_agent } end |
#validate! ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/cloudflare/artifacts/configuration.rb', line 85 def validate! raise ArgumentError, "account_id is required" if blank?(account_id) raise ArgumentError, "api_token is required" if blank?(api_token) raise ArgumentError, "namespace is required" if blank?(namespace) raise ArgumentError, "timeout must be positive" unless timeout.is_a?(Numeric) && timeout.positive? self end |