Class: MdNotes::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/md_notes/configuration.rb

Overview

All configuration including auth info and base URI for the API access are configured in this class.

Constant Summary collapse

ENVIRONMENTS =

All the environments the SDK can run in.

{
  Environment::PRODUCTION => {
    Server::DEFAULT => 'http://markdown-notes-app.herokuapp.com'
  }
}.freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timeout: 60, max_retries: 0, retry_interval: 1, backoff_factor: 2, retry_statuses: [408, 413, 429, 500, 502, 503, 504, 521, 522, 524], retry_methods: %i[get put],, environment: Environment::PRODUCTION, o_auth_client_id: 'TODO: Replace', o_auth_client_secret: 'TODO: Replace', o_auth_username: 'TODO: Replace', o_auth_password: 'TODO: Replace', o_auth_token: nil) ⇒ Configuration

Returns a new instance of Configuration.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/md_notes/configuration.rb', line 50

def initialize(timeout: 60, max_retries: 0, retry_interval: 1,
               backoff_factor: 2,
               retry_statuses: [408, 413, 429, 500, 502, 503, 504, 521, 522, 524],
               retry_methods: %i[get put],
               environment: Environment::PRODUCTION,
               o_auth_client_id: 'TODO: Replace',
               o_auth_client_secret: 'TODO: Replace',
               o_auth_username: 'TODO: Replace',
               o_auth_password: 'TODO: Replace', o_auth_token: nil)
  # The value to use for connection timeout
  @timeout = timeout

  # The number of times to retry an endpoint call if it fails
  @max_retries = max_retries

  # Pause in seconds between retries
  @retry_interval = retry_interval

  # The amount to multiply each successive retry's interval amount
  # by in order to provide backoff
  @backoff_factor = backoff_factor

  # A list of HTTP statuses to retry
  @retry_statuses = retry_statuses

  # A list of HTTP methods to retry
  @retry_methods = retry_methods

  # Current API environment
  @environment = String(environment)

  # OAuth 2 Client ID
  @o_auth_client_id = o_auth_client_id

  # OAuth 2 Client Secret
  @o_auth_client_secret = o_auth_client_secret

  # OAuth 2 Resource Owner Username
  @o_auth_username = o_auth_username

  # OAuth 2 Resource Owner Password
  @o_auth_password = o_auth_password

  # Object for storing information about the OAuth token
  @o_auth_token = if o_auth_token.is_a? OAuthToken
                    OAuthToken.from_hash o_auth_token.to_hash
                  else
                    o_auth_token
                  end

  # The Http Client to use for making requests.
  @http_client = create_http_client
end

Class Attribute Details

.environmentsObject (readonly)

Returns the value of attribute environments.



47
48
49
# File 'lib/md_notes/configuration.rb', line 47

def environments
  @environments
end

Instance Attribute Details

#backoff_factorObject (readonly)

Returns the value of attribute backoff_factor.



29
30
31
# File 'lib/md_notes/configuration.rb', line 29

def backoff_factor
  @backoff_factor
end

#environmentObject (readonly)

Returns the value of attribute environment.



32
33
34
# File 'lib/md_notes/configuration.rb', line 32

def environment
  @environment
end

#http_clientObject (readonly)

The attribute readers for properties.



25
26
27
# File 'lib/md_notes/configuration.rb', line 25

def http_client
  @http_client
end

#max_retriesObject (readonly)

Returns the value of attribute max_retries.



27
28
29
# File 'lib/md_notes/configuration.rb', line 27

def max_retries
  @max_retries
end

#o_auth_client_idObject (readonly)

Returns the value of attribute o_auth_client_id.



33
34
35
# File 'lib/md_notes/configuration.rb', line 33

def o_auth_client_id
  @o_auth_client_id
end

#o_auth_client_secretObject (readonly)

Returns the value of attribute o_auth_client_secret.



34
35
36
# File 'lib/md_notes/configuration.rb', line 34

def o_auth_client_secret
  @o_auth_client_secret
end

#o_auth_passwordObject (readonly)

Returns the value of attribute o_auth_password.



36
37
38
# File 'lib/md_notes/configuration.rb', line 36

def o_auth_password
  @o_auth_password
end

#o_auth_usernameObject (readonly)

Returns the value of attribute o_auth_username.



35
36
37
# File 'lib/md_notes/configuration.rb', line 35

def o_auth_username
  @o_auth_username
end

#retry_intervalObject (readonly)

Returns the value of attribute retry_interval.



28
29
30
# File 'lib/md_notes/configuration.rb', line 28

def retry_interval
  @retry_interval
end

#retry_methodsObject (readonly)

Returns the value of attribute retry_methods.



31
32
33
# File 'lib/md_notes/configuration.rb', line 31

def retry_methods
  @retry_methods
end

#retry_statusesObject (readonly)

Returns the value of attribute retry_statuses.



30
31
32
# File 'lib/md_notes/configuration.rb', line 30

def retry_statuses
  @retry_statuses
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



26
27
28
# File 'lib/md_notes/configuration.rb', line 26

def timeout
  @timeout
end

Instance Method Details

#clone_with(timeout: nil, max_retries: nil, retry_interval: nil, backoff_factor: nil, retry_statuses: nil, retry_methods: nil, environment: nil, o_auth_client_id: nil, o_auth_client_secret: nil, o_auth_username: nil, o_auth_password: nil, o_auth_token: nil) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/md_notes/configuration.rb', line 104

def clone_with(timeout: nil, max_retries: nil, retry_interval: nil,
               backoff_factor: nil, retry_statuses: nil, retry_methods: nil,
               environment: nil, o_auth_client_id: nil,
               o_auth_client_secret: nil, o_auth_username: nil,
               o_auth_password: nil, o_auth_token: nil)
  timeout ||= self.timeout
  max_retries ||= self.max_retries
  retry_interval ||= self.retry_interval
  backoff_factor ||= self.backoff_factor
  retry_statuses ||= self.retry_statuses
  retry_methods ||= self.retry_methods
  environment ||= self.environment
  o_auth_client_id ||= self.o_auth_client_id
  o_auth_client_secret ||= self.o_auth_client_secret
  o_auth_username ||= self.o_auth_username
  o_auth_password ||= self.o_auth_password
  o_auth_token ||= self.o_auth_token

  Configuration.new(timeout: timeout, max_retries: max_retries,
                    retry_interval: retry_interval,
                    backoff_factor: backoff_factor,
                    retry_statuses: retry_statuses,
                    retry_methods: retry_methods, environment: environment,
                    o_auth_client_id: o_auth_client_id,
                    o_auth_client_secret: o_auth_client_secret,
                    o_auth_username: o_auth_username,
                    o_auth_password: o_auth_password,
                    o_auth_token: o_auth_token)
end

#create_http_clientObject



134
135
136
137
138
139
140
# File 'lib/md_notes/configuration.rb', line 134

def create_http_client
  FaradayClient.new(timeout: timeout, max_retries: max_retries,
                    retry_interval: retry_interval,
                    backoff_factor: backoff_factor,
                    retry_statuses: retry_statuses,
                    retry_methods: retry_methods)
end

#get_base_uri(server = Server::DEFAULT) ⇒ String

Generates the appropriate base URI for the environment and the server. required.

Parameters:

  • The (Configuration::Server)

    server enum for which the base URI is

Returns:

  • (String)

    The base URI.



153
154
155
# File 'lib/md_notes/configuration.rb', line 153

def get_base_uri(server = Server::DEFAULT)
  ENVIRONMENTS[environment][server].clone
end

#o_auth_tokenObject



38
39
40
41
42
43
44
# File 'lib/md_notes/configuration.rb', line 38

def o_auth_token
  if @o_auth_token.is_a? OAuthToken
    OAuthToken.from_hash @o_auth_token.to_hash
  else
    @o_auth_token
  end
end