Class: Flexipass::Client

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

Overview

The Client class is responsible for making API requests to the Flexipass server.

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/flexipass/client.rb', line 7

def initialize
  Flexipass.configuration.validate!
  @conn = Faraday.new(url: Flexipass.configuration.server_address) do |faraday|
    if Flexipass.configuration.enable_logging
      # Create a logger instance
      logger = Logger.new(STDOUT)
      logger.level = Logger::DEBUG
      faraday.response :logger, logger, bodies: true  # Set bodies: true to log request and response bodies
    end
    faraday.request :url_encoded
    faraday.adapter Faraday.default_adapter
    faraday.headers['Content-Type'] = 'application/json'
    faraday.headers['Authorization'] = basic_auth_header
  end
end

Instance Method Details

#companyObject

Returns an instance of the Company class.



34
35
36
# File 'lib/flexipass/client.rb', line 34

def company
  @company ||= Company.new(self)
end

#doorObject

Returns an instance of the Door class.



29
30
31
# File 'lib/flexipass/client.rb', line 29

def door
  @door ||= Door.new(self)
end

#mobile_keyObject

Returns an instance of the MobileKey class.



24
25
26
# File 'lib/flexipass/client.rb', line 24

def mobile_key
  @mobile_key ||= MobileKey.new(self)
end

#send_request(method, endpoint, params = {}) ⇒ Hash

Sends a request to the Flexipass server.

Parameters:

  • method (String)

    The HTTP method for the request.

  • endpoint (String)

    The API endpoint.

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

    The request parameters.

Returns:

  • (Hash)

    The parsed JSON response from the server.

Raises:

  • (ApiError)

    If the API request fails.



45
46
47
48
49
50
51
52
53
# File 'lib/flexipass/client.rb', line 45

def send_request(method, endpoint, params = {})
  response = @conn.send(method.downcase) do |req|
    req.url endpoint
    req.params['companyToken'] = Flexipass.configuration.company_token
    req.body = params.to_json if ['POST', 'PUT'].include?(method.upcase)
  end

  handle_response(response)
end