Class: Ashikawa::Core::Connection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ashikawa-core/connection.rb

Overview

A Connection via HTTP to a certain host

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_string, database_name, options = {}) ⇒ Connection

Initialize a Connection with a given API String

Examples:

Create a new Connection with no additional options

connection = Connection.new('http://localhost:8529', '_system')

Create a new Connection with additional options

connection = Connection.new('http://localhost:8529', '_system', {
  adapter: :typhoeus,
  logger: Yell.new(STDOUT),
  additional_request_middleware: [:my_request_middleware]
  additional_response_middleware: [:my_response_middleware]
})

Parameters:

  • api_string (String)

    scheme, hostname and port as a String

  • database_name (String)

    The name of the database you want to communicate with

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • adapter (Object)

    The Faraday adapter you want to use. Defaults to Default Adapter

  • debug_headers (Object)

    Should HTTP header be logged or not

  • logger (Object)

    The logger you want to use. Defaults to no logger.

  • additional_request_middlewares (Array)

    Additional request middlewares

  • additional_response_middlewares (Array)

    Additional response middlewares

  • additional_middlewares (Array)

    Additional middlewares



84
85
86
87
88
# File 'lib/ashikawa-core/connection.rb', line 84

def initialize(api_string, database_name, options = {})
  @api_string = api_string
  @database_name = database_name
  @connection = FaradayFactory.create_connection("#{api_string}/_db/#{database_name}/_api", options)
end

Instance Attribute Details

#connectionFaraday (readonly)

The Faraday connection object

Examples:

Set additional response middleware

connection = Connection.new('http://localhost:8529', '_system')
connection.connection.response :caching

Returns:

  • (Faraday)


54
55
56
# File 'lib/ashikawa-core/connection.rb', line 54

def connection
  @connection
end

#database_nameString (readonly)

The name of the database you want to talk with

Examples:

Get the name of the database

connection = Connection.new('http://localhost:8529', 'ashikawa')
connection.database_name # => 'ashikawa'

Returns:

  • (String)


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

Parameters:

  • username (String)
  • password (String)

Returns:

  • (String)

    Basic Auth info (Base 64 of username: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

Examples:

Is authentication activated for this connection?

connection = Connection.new('http://localhost:8529', '_system')
connection.authentication? #=> false
connection.authenticate_with(:username => 'james', :password => 'bond')
connection.authentication? #=> true

Returns:

  • (Boolean)


133
134
135
# File 'lib/ashikawa-core/connection.rb', line 133

def authentication?
  !!@authentication
end

#hostString

The host part of the connection

Examples:

Get the host part of the connection

connection = Connection.new('http://localhost:8529', '_system')
connection.host # => 'localhost'

Returns:

  • (String)


25
# File 'lib/ashikawa-core/connection.rb', line 25

def_delegator :@connection, :host

#portFixnum

The port of the connection

Examples:

Get the port of the connection

connection = Connection.new('http://localhost:8529', '_system')
connection.port # => 8529

Returns:

  • (Fixnum)


45
# File 'lib/ashikawa-core/connection.rb', line 45

def_delegator :@connection, :port

#schemeString

The scheme of the connection

Examples:

Get the scheme of the connection

connection = Connection.new('http://localhost:8529', '_system')
connection.scheme # => 'http'

Returns:

  • (String)


35
# File 'lib/ashikawa-core/connection.rb', line 35

def_delegator :@connection, :scheme

#send_request(path, params = {}) ⇒ hash

Note:

prepends the api_string automatically

Sends a request to a given path returning the parsed result

Examples:

get request

connection.send_request('/collection/new_collection')

post request

connection.send_request('/collection/new_collection', :post => { :name => 'new_collection' })

Parameters:

  • path (string)

    the path you wish to send a request to.

  • params (Hash) (defaults to: {})

    a customizable set of options

Options Hash (params):

  • :post (hash)

    post data in case you want to send a post request.

Returns:

  • (hash)

    parsed json response from the server



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

Note:

prepends the api_string automatically

Sends a request to a given path without the database suffix returning the parsed result

Examples:

get request

connection.send_request('/collection/new_collection')

post request

connection.send_request('/collection/new_collection', :post => { :name => 'new_collection' })

Parameters:

  • path (string)

    the path you wish to send a request to.

  • params (Hash) (defaults to: {})

    a customizable set of options

Options Hash (params):

  • :post (hash)

    post data in case you want to send a post request.

Returns:

  • (hash)

    parsed json response from the server



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