Class: Cloudflare::Artifacts::Configuration

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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, **options)
  @base_url   = base_url
  @namespace  = namespace
  @timeout    = timeout
  @adapter    = Faraday.default_adapter
  @user_agent = "cloudflare-artifacts-ruby/#{VERSION}"
  assign(options)
end

Instance Attribute Details

#account_idObject

Returns the value of attribute account_id.



10
11
12
# File 'lib/cloudflare/artifacts/configuration.rb', line 10

def 
  @account_id
end

#adapterObject

Returns the value of attribute adapter.



10
11
12
# File 'lib/cloudflare/artifacts/configuration.rb', line 10

def adapter
  @adapter
end

#api_tokenObject

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_urlObject

Returns the value of attribute base_url.



10
11
12
# File 'lib/cloudflare/artifacts/configuration.rb', line 10

def base_url
  @base_url
end

#namespaceObject

Returns the value of attribute namespace.



10
11
12
# File 'lib/cloudflare/artifacts/configuration.rb', line 10

def namespace
  @namespace
end

#timeoutObject

Returns the value of attribute timeout.



10
11
12
# File 'lib/cloudflare/artifacts/configuration.rb', line 10

def timeout
  @timeout
end

#user_agentObject

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_envObject



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

Raises:

  • (ArgumentError)


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(options)
  options.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(**options)
  dup.assign(options)
end

#to_hObject



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cloudflare/artifacts/configuration.rb', line 73

def to_h
  {
    account_id: ,
    api_token: api_token,
    namespace: namespace,
    base_url: base_url,
    adapter: adapter,
    timeout: timeout,
    user_agent: user_agent
  }
end

#validate!Object

Raises:

  • (ArgumentError)


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?()
  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