Class: TrainPlugins::Rest::Connection
- Inherits:
-
Train::Plugins::Transport::BaseConnection
- Object
- Train::Plugins::Transport::BaseConnection
- TrainPlugins::Rest::Connection
- Defined in:
- lib/train-rest/connection.rb
Instance Attribute Summary collapse
-
#detected_os ⇒ Object
writeonly
Allow overwriting to refine the type of REST API.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#auth_parameters ⇒ Object
Auth Handlers-faced API.
- #close ⇒ Object
- #connect ⇒ Object
-
#initialize(options) ⇒ Connection
constructor
A new instance of Connection.
- #inventory ⇒ Object (also: #os)
- #platform ⇒ Object
- #request(path, method = :get, request_parameters: {}, data: nil, headers: {}, json_processing: true) ⇒ Object
- #setup_vcr ⇒ Object
- #uri ⇒ Object
Constructor Details
#initialize(options) ⇒ Connection
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/train-rest/connection.rb', line 13 def initialize() super() # Plugin was called with an URI only [:endpoint] = [:target].sub("rest://", "https://") unless [:endpoint] # Accept string (CLI) and boolean (API) options [:verify_ssl] = [:verify_ssl].to_s == "false" ? false : true setup_vcr connect end |
Instance Attribute Details
#detected_os=(value) ⇒ Object (writeonly)
Allow overwriting to refine the type of REST API
66 67 68 |
# File 'lib/train-rest/connection.rb', line 66 def detected_os=(value) @detected_os = value end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
11 12 13 |
# File 'lib/train-rest/connection.rb', line 11 def end |
Instance Method Details
#auth_parameters ⇒ Object
Auth Handlers-faced API
121 122 123 |
# File 'lib/train-rest/connection.rb', line 121 def auth_parameters auth_handler.auth_parameters end |
#close ⇒ Object
55 56 57 |
# File 'lib/train-rest/connection.rb', line 55 def close logout if auth_handlers.include? auth_type end |
#connect ⇒ Object
51 52 53 |
# File 'lib/train-rest/connection.rb', line 51 def connect login if auth_handlers.include? auth_type end |
#inventory ⇒ Object Also known as: os
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/train-rest/connection.rb', line 68 def inventory OpenStruct.new({ name: @detected_os || "rest", release: TrainPlugins::Rest::VERSION, family_hierarchy: %w{rest api}, family: "api", platform: "rest", platform_version: 0, }) end |
#platform ⇒ Object
81 82 83 84 85 |
# File 'lib/train-rest/connection.rb', line 81 def platform Train::Platforms.name("rest").in_family("api") force_platform!("rest", { release: TrainPlugins::Rest::VERSION }) end |
#request(path, method = :get, request_parameters: {}, data: nil, headers: {}, json_processing: true) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/train-rest/connection.rb', line 95 def request(path, method = :get, request_parameters: {}, data: nil, headers: {}, json_processing: true) parameters = global_parameters.merge(request_parameters) parameters[:method] = method parameters[:url] = full_url(path) if json_processing parameters[:headers]["Accept"] = "application/json" parameters[:headers]["Content-Type"] = "application/json" parameters[:payload] = JSON.generate(data) unless data.nil? else parameters[:payload] = data end parameters[:headers].merge! headers parameters.compact! logger.info format("[REST] => %s", parameters.to_s) if [:debug_rest] response = RestClient::Request.execute(parameters) logger.info format("[REST] <= %s", response.to_s) if [:debug_rest] transform_response(response, json_processing) end |
#setup_vcr ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/train-rest/connection.rb', line 27 def setup_vcr return unless [:vcr_cassette] require "vcr" unless defined?(VCR) # TODO: Starts from "/" :( library = [:vcr_library] match_on = [:vcr_match_on].split.map(&:to_sym) VCR.configure do |config| config.cassette_library_dir = library config.hook_into [:vcr_hook_into] config. = { record: [:vcr_record].to_sym, match_requests_on: match_on, } end VCR.insert_cassette [:vcr_cassette] rescue LoadError logger.fatal "Install the vcr gem to use HTTP(S) playback capability" raise end |
#uri ⇒ Object
59 60 61 62 63 |
# File 'lib/train-rest/connection.rb', line 59 def uri components = URI([:endpoint]) components.scheme = "rest" components.to_s end |