Class: ECSBundler::RestClient

Inherits:
Object
  • Object
show all
Defined in:
lib/ecs_bundler/rest_client.rb

Overview

Wrapper under rest-client with authorization headers

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ RestClient

Returns a new instance of RestClient.



11
12
13
# File 'lib/ecs_bundler/rest_client.rb', line 11

def initialize(options = {})
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

get delete head options post patch put methods



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ecs_bundler/rest_client.rb', line 31

def method_missing(name, *args)
  if %i[get delete head options post patch put].include?(name)
    if %i[get delete head options].include?(name)
      path, headers = args
      payload = nil
    else
      path, payload, headers = args
    end
    return ::RestClient::Request.execute(
      method: name,
      url: "#{options[:url]}#{path}",
      payload: payload ? payload.to_json : payload,
      headers: self.headers.merge(headers || {})
    ) do |response|
      parsed_response = JSON.parse(response.body) rescue {}
      message = [
        "#{response.code} #{::RestClient::STATUSES[response.code]}",
        parsed_response['info'],
        (parsed_response['messages'] || []).map { |message| message['message'] }
      ].compact.join(', ')
      yield(response, parsed_response, message) if block_given?
      response
    end
  end
  super(name, *args)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/ecs_bundler/rest_client.rb', line 9

def options
  @options
end

Instance Method Details

#headersObject



15
16
17
18
19
20
21
22
23
# File 'lib/ecs_bundler/rest_client.rb', line 15

def headers
  {
    content_type: :json,
    accept: :json,
    'User-Agent' => options[:userAgent],
    'X-ApiKey' => options[:apiKey],
    'X-User' => options[:userName]
  }
end

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
# File 'lib/ecs_bundler/rest_client.rb', line 25

def respond_to?(name)
  return true if %i[get delete head options post patch put].include?(name)
  super(name)
end