Class: Travis::Client::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/travis/client/connection.rb

Defined Under Namespace

Classes: Env

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint: Travis.default_endpoint, request_headers: {}, access_token: nil, http_factory: HTTP) {|_self| ... } ⇒ Connection

Returns a new instance of Connection.

Yields:

  • (_self)

Yield Parameters:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/travis/client/connection.rb', line 18

def initialize(endpoint: Travis.default_endpoint, request_headers: {}, access_token: nil, http_factory: HTTP)
  @request_headers, @mixin                = DEFAULT_HEADERS.merge(request_headers).freeze, Module.new
  @error_types, @resource_types, @actions = {}, {}, {}
  @before_callbacks, @after_callbacks     = [], []
  @session_factory, @http_factory         = Class.new(Session), http_factory
  @access_token                           = access_token
  @default_session                        = create_session

  yield self if block_given?

  load_resource(PREDEFINED, endpoint)
  @service_index = @default_session.request(:get, endpoint)
  load_resource(service_index.resources, endpoint)
  load_errors(service_index.errors)
  define_actions

  @session_factory.include(@mixin)
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



16
17
18
# File 'lib/travis/client/connection.rb', line 16

def access_token
  @access_token
end

#actionsObject (readonly)

Returns the value of attribute actions.



16
17
18
# File 'lib/travis/client/connection.rb', line 16

def actions
  @actions
end

#error_typesObject (readonly)

Returns the value of attribute error_types.



16
17
18
# File 'lib/travis/client/connection.rb', line 16

def error_types
  @error_types
end

#http_factoryObject (readonly)

Returns the value of attribute http_factory.



16
17
18
# File 'lib/travis/client/connection.rb', line 16

def http_factory
  @http_factory
end

#mixinObject (readonly)

Returns the value of attribute mixin.



16
17
18
# File 'lib/travis/client/connection.rb', line 16

def mixin
  @mixin
end

#request_headersObject (readonly)

Returns the value of attribute request_headers.



16
17
18
# File 'lib/travis/client/connection.rb', line 16

def request_headers
  @request_headers
end

#resource_typesObject (readonly)

Returns the value of attribute resource_types.



16
17
18
# File 'lib/travis/client/connection.rb', line 16

def resource_types
  @resource_types
end

#service_indexObject (readonly)

Returns the value of attribute service_index.



16
17
18
# File 'lib/travis/client/connection.rb', line 16

def service_index
  @service_index
end

Instance Method Details

#action(resource_type, action_name) ⇒ Object



58
59
60
61
62
# File 'lib/travis/client/connection.rb', line 58

def action(resource_type, action_name)
  actions.
    fetch(resource_type.to_s) { raise ArgumentError, 'unknown resource type' }.
    fetch(action_name.to_s)   { raise ArgumentError, 'unknown action'        }
end

#after_request(callback = Proc.new) ⇒ Object



46
47
48
# File 'lib/travis/client/connection.rb', line 46

def after_request(callback = Proc.new)
  @after_callbacks << callback
end

#before_request(callback = Proc.new) ⇒ Object



42
43
44
# File 'lib/travis/client/connection.rb', line 42

def before_request(callback = Proc.new)
  @before_callbacks << callback
end

#create_session(**options) ⇒ Object



37
38
39
40
# File 'lib/travis/client/connection.rb', line 37

def create_session(**options)
  options[:access_token] ||= access_token
  @session_factory.new(self, **options)
end

#notify(method, uri, params) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/travis/client/connection.rb', line 50

def notify(method, uri, params)
  env = Env.new(method, uri, params, nil, {})
  @before_callbacks.each { |c| c.call(env) }
  env.response = yield
  @after_callbacks.each { |c| c.call(env) }
  env.response
end