Class: Travis::Client::Connection
- Inherits:
-
Object
- Object
- Travis::Client::Connection
- Defined in:
- lib/travis/client/connection.rb
Defined Under Namespace
Classes: Env
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#actions ⇒ Object
readonly
Returns the value of attribute actions.
-
#error_types ⇒ Object
readonly
Returns the value of attribute error_types.
-
#http_factory ⇒ Object
readonly
Returns the value of attribute http_factory.
-
#mixin ⇒ Object
readonly
Returns the value of attribute mixin.
-
#request_headers ⇒ Object
readonly
Returns the value of attribute request_headers.
-
#resource_types ⇒ Object
readonly
Returns the value of attribute resource_types.
-
#service_index ⇒ Object
readonly
Returns the value of attribute service_index.
Instance Method Summary collapse
- #action(resource_type, action_name) ⇒ Object
- #after_request(callback = Proc.new) ⇒ Object
- #before_request(callback = Proc.new) ⇒ Object
- #create_session(**options) ⇒ Object
-
#initialize(endpoint: Travis.default_endpoint, request_headers: {}, access_token: nil, http_factory: HTTP) {|_self| ... } ⇒ Connection
constructor
A new instance of Connection.
- #notify(method, uri, params) ⇒ Object
Constructor Details
#initialize(endpoint: Travis.default_endpoint, request_headers: {}, access_token: nil, http_factory: HTTP) {|_self| ... } ⇒ Connection
Returns a new instance of Connection.
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_token ⇒ Object (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 |
#actions ⇒ Object (readonly)
Returns the value of attribute actions.
16 17 18 |
# File 'lib/travis/client/connection.rb', line 16 def actions @actions end |
#error_types ⇒ Object (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_factory ⇒ Object (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 |
#mixin ⇒ Object (readonly)
Returns the value of attribute mixin.
16 17 18 |
# File 'lib/travis/client/connection.rb', line 16 def mixin @mixin end |
#request_headers ⇒ Object (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_types ⇒ Object (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_index ⇒ Object (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(**) [:access_token] ||= access_token @session_factory.new(self, **) 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 |