Class: Xapixctl::PhoenixClient::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/xapixctl/phoenix_client/connection.rb

Constant Summary collapse

DEFAULT_CLIENT_OPTS =
{ verify_ssl: false, headers: { accept: :json } }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, token, default_success_handler, default_error_handler, logging) ⇒ Connection

Returns a new instance of Connection.



10
11
12
13
14
15
16
17
# File 'lib/xapixctl/phoenix_client/connection.rb', line 10

def initialize(url, token, default_success_handler, default_error_handler, logging)
  @xapix_url = url
  client_opts = DEFAULT_CLIENT_OPTS.deep_merge(headers: { Authorization: "Bearer #{token}" })
  client_opts.merge!(log: RestClient.create_log(logging)) if logging
  @client = RestClient::Resource.new(File.join(url, 'api/v1'), client_opts)
  @default_success_handler = default_success_handler
  @default_error_handler = default_error_handler
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



8
9
10
# File 'lib/xapixctl/phoenix_client/connection.rb', line 8

def client
  @client
end

#xapix_urlObject (readonly)

Returns the value of attribute xapix_url.



8
9
10
# File 'lib/xapixctl/phoenix_client/connection.rb', line 8

def xapix_url
  @xapix_url
end

Instance Method Details

#available_resource_types(&block) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/xapixctl/phoenix_client/connection.rb', line 23

def available_resource_types(&block)
  @available_resource_types ||= begin
    result_handler(block).
      prepare_data(->(data) { data['resource_types'].freeze }).
      run { @client[resource_types_path].get }
  end
end

#on_error(&block) ⇒ Object



21
# File 'lib/xapixctl/phoenix_client/connection.rb', line 21

def on_error(&block); @default_error_handler = block; self; end

#on_success(&block) ⇒ Object



19
# File 'lib/xapixctl/phoenix_client/connection.rb', line 19

def on_success(&block); @default_success_handler = block; self; end

#organization(org) ⇒ Object



31
32
33
# File 'lib/xapixctl/phoenix_client/connection.rb', line 31

def organization(org)
  OrganizationConnection.new(self, org)
end

#project(org:, project:) ⇒ Object



35
36
37
# File 'lib/xapixctl/phoenix_client/connection.rb', line 35

def project(org:, project:)
  ProjectConnection.new(self, org, project)
end

#result_handler(block) ⇒ Object



39
40
41
# File 'lib/xapixctl/phoenix_client/connection.rb', line 39

def result_handler(block)
  ResultHandler.new(default_success_handler: @default_success_handler, default_error_handler: @default_error_handler, &block)
end