Class: Ashikawa::Core::Connection
- Inherits:
-
Object
- Object
- Ashikawa::Core::Connection
- Extended by:
- Forwardable
- Defined in:
- lib/ashikawa-core/connection.rb
Overview
A Connection via HTTP to a certain host
Instance Attribute Summary collapse
-
#connection ⇒ Faraday
readonly
The Faraday connection object.
-
#database_name ⇒ String
readonly
The name of the database you want to talk with.
Instance Method Summary collapse
-
#authenticate_with(username, password) ⇒ String
private
Authenticate with given username and password.
-
#authentication? ⇒ Boolean
Checks if authentication for this Connection is active or not.
-
#host ⇒ String
The host part of the connection.
-
#initialize(api_string, database_name, options = {}) ⇒ Connection
constructor
Initialize a Connection with a given API String.
-
#port ⇒ Fixnum
The port of the connection.
-
#scheme ⇒ String
The scheme of the connection.
-
#send_request(path, params = {}) ⇒ hash
Sends a request to a given path returning the parsed result.
-
#send_request_without_database_suffix(path, params = {}) ⇒ hash
Sends a request to a given path without the database suffix returning the parsed result.
Constructor Details
#initialize(api_string, database_name, options = {}) ⇒ Connection
Initialize a Connection with a given API String
84 85 86 87 88 |
# File 'lib/ashikawa-core/connection.rb', line 84 def initialize(api_string, database_name, = {}) @api_string = api_string @database_name = database_name @connection = FaradayFactory.create_connection("#{api_string}/_db/#{database_name}/_api", ) end |
Instance Attribute Details
#connection ⇒ Faraday (readonly)
The Faraday connection object
54 55 56 |
# File 'lib/ashikawa-core/connection.rb', line 54 def connection @connection end |
#database_name ⇒ String (readonly)
The name of the database you want to talk with
62 63 64 |
# File 'lib/ashikawa-core/connection.rb', line 62 def database_name @database_name end |
Instance Method Details
#authenticate_with(username, password) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Authenticate with given username and password
143 144 145 |
# File 'lib/ashikawa-core/connection.rb', line 143 def authenticate_with(username, password) @authentication = @connection.basic_auth(username, password) end |
#authentication? ⇒ Boolean
Checks if authentication for this Connection is active or not
133 134 135 |
# File 'lib/ashikawa-core/connection.rb', line 133 def authentication? !!@authentication end |
#host ⇒ String
The host part of the connection
25 |
# File 'lib/ashikawa-core/connection.rb', line 25 def_delegator :@connection, :host |
#port ⇒ Fixnum
The port of the connection
45 |
# File 'lib/ashikawa-core/connection.rb', line 45 def_delegator :@connection, :port |
#scheme ⇒ String
The scheme of the connection
35 |
# File 'lib/ashikawa-core/connection.rb', line 35 def_delegator :@connection, :scheme |
#send_request(path, params = {}) ⇒ hash
prepends the api_string automatically
Sends a request to a given path returning the parsed result
101 102 103 104 105 106 107 |
# File 'lib/ashikawa-core/connection.rb', line 101 def send_request(path, params = {}) method = http_verb(params) result = @connection.public_send(method, path, params[method]) result.body rescue Faraday::Error::ParsingError raise Ashikawa::Core::JsonError end |
#send_request_without_database_suffix(path, params = {}) ⇒ hash
prepends the api_string automatically
Sends a request to a given path without the database suffix returning the parsed result
120 121 122 |
# File 'lib/ashikawa-core/connection.rb', line 120 def send_request_without_database_suffix(path, params = {}) send_request("#{@api_string}/_api/#{path}", params) end |