Class: Flipper::Adapters::Http::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/flipper/adapters/http/client.rb

Constant Summary collapse

DEFAULT_HEADERS =
{
  'content-type' => 'application/json',
  'accept' => 'application/json',
  'user-agent' => "Flipper HTTP Adapter v#{VERSION}",
}.freeze
HTTPS_SCHEME =
"https".freeze
CLIENT_FRAMEWORKS =
{
  rails:    -> { Rails.version if defined?(Rails) },
  sinatra:  -> { Sinatra::VERSION if defined?(Sinatra) },
  hanami:   -> { Hanami::VERSION if defined?(Hanami) },
  sidekiq:  -> { Sidekiq::VERSION if defined?(Sidekiq) },
  good_job: -> { GoodJob::VERSION if defined?(GoodJob) },
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/flipper/adapters/http/client.rb', line 30

def initialize(options = {})
  @uri = URI(options.fetch(:url))
  @basic_auth_username = options[:basic_auth_username]
  @basic_auth_password = options[:basic_auth_password]
  @read_timeout = options[:read_timeout]
  @open_timeout = options[:open_timeout]
  @write_timeout = options[:write_timeout]
  @max_retries = options.key?(:max_retries) ? options[:max_retries] : 0
  @debug_output = options[:debug_output]

  @headers = {}
  DEFAULT_HEADERS.each { |key, value| add_header key, value }
  if options[:headers]
    options[:headers].each { |key, value| add_header key, value }
  end
end

Instance Attribute Details

#basic_auth_passwordObject (readonly)

Returns the value of attribute basic_auth_password.



26
27
28
# File 'lib/flipper/adapters/http/client.rb', line 26

def basic_auth_password
  @basic_auth_password
end

#basic_auth_usernameObject (readonly)

Returns the value of attribute basic_auth_username.



26
27
28
# File 'lib/flipper/adapters/http/client.rb', line 26

def basic_auth_username
  @basic_auth_username
end

#debug_outputObject (readonly)

Returns the value of attribute debug_output.



28
29
30
# File 'lib/flipper/adapters/http/client.rb', line 28

def debug_output
  @debug_output
end

#headersObject (readonly)

Returns the value of attribute headers.



25
26
27
# File 'lib/flipper/adapters/http/client.rb', line 25

def headers
  @headers
end

#max_retriesObject (readonly)

Returns the value of attribute max_retries.



28
29
30
# File 'lib/flipper/adapters/http/client.rb', line 28

def max_retries
  @max_retries
end

#open_timeoutObject (readonly)

Returns the value of attribute open_timeout.



27
28
29
# File 'lib/flipper/adapters/http/client.rb', line 27

def open_timeout
  @open_timeout
end

#read_timeoutObject (readonly)

Returns the value of attribute read_timeout.



27
28
29
# File 'lib/flipper/adapters/http/client.rb', line 27

def read_timeout
  @read_timeout
end

#uriObject (readonly)

Returns the value of attribute uri.



25
26
27
# File 'lib/flipper/adapters/http/client.rb', line 25

def uri
  @uri
end

#write_timeoutObject (readonly)

Returns the value of attribute write_timeout.



27
28
29
# File 'lib/flipper/adapters/http/client.rb', line 27

def write_timeout
  @write_timeout
end

Instance Method Details

#add_header(key, value) ⇒ Object



47
48
49
50
# File 'lib/flipper/adapters/http/client.rb', line 47

def add_header(key, value)
  key = key.to_s.downcase.gsub('_'.freeze, '-'.freeze).freeze
  @headers[key] = value
end

#delete(path, body = nil) ⇒ Object



60
61
62
# File 'lib/flipper/adapters/http/client.rb', line 60

def delete(path, body = nil)
  perform Net::HTTP::Delete, path, @headers, body: body
end

#get(path) ⇒ Object



52
53
54
# File 'lib/flipper/adapters/http/client.rb', line 52

def get(path)
  perform Net::HTTP::Get, path, @headers
end

#post(path, body = nil) ⇒ Object



56
57
58
# File 'lib/flipper/adapters/http/client.rb', line 56

def post(path, body = nil)
  perform Net::HTTP::Post, path, @headers, body: body
end