Module: Safettp::Client

Defined in:
lib/safettp/client.rb

Defined Under Namespace

Modules: ClassMethods Classes: Configuration

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



2
3
4
# File 'lib/safettp/client.rb', line 2

def base_url
  @base_url
end

#options_hashObject (readonly)

Returns the value of attribute options_hash.



2
3
4
# File 'lib/safettp/client.rb', line 2

def options_hash
  @options_hash
end

Class Method Details

.included(base) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/safettp/client.rb', line 48

def self.included(base)
  base.extend(ClassMethods)

  %i[get post put patch delete].each do |method|
    define_method(method) do |uri_suffix, options = {}, &block|
      perform(method, uri_suffix, options, &block)
    end
  end
end

Instance Method Details

#initialize(base_url = self.class.config.base_url, options_hash = self.class.config.default_options) ⇒ Object



4
5
6
7
8
# File 'lib/safettp/client.rb', line 4

def initialize(base_url = self.class.config.base_url,
               options_hash = self.class.config.default_options)
  @base_url = base_url
  @options_hash = options_hash
end

#perform(*args) {|guard| ... } ⇒ Object

Yields:

  • (guard)


10
11
12
13
14
15
# File 'lib/safettp/client.rb', line 10

def perform(*args, &block)
  response = perform_without_guard(*args)
  guard = Safettp::Guard.new(response)
  yield(guard)
  guard.evaluate!
end

#perform_without_guard(method, uri_suffix = '/', options = {}) ⇒ Object



17
18
19
20
21
# File 'lib/safettp/client.rb', line 17

def perform_without_guard(method, uri_suffix = '/', options = {})
  url = "#{base_url}#{uri_suffix}"
  Safettp::Request.new(url, options_hash.merge(options))
                  .perform(method)
end