Module: BrickFTP::APIDefinition::ClassMethods

Defined in:
lib/brick_ftp/api_definition.rb

Instance Method Summary collapse

Instance Method Details

#api_component_for(name) ⇒ BrickFTP::APIComponent

Build BrickFTP::APIComponent.

Parameters:

  • name (Symbol)

Returns:

Raises:



60
61
62
63
# File 'lib/brick_ftp/api_definition.rb', line 60

def api_component_for(name)
  raise BrickFTP::API::NoSuchAPI, "#{name} #{self.name}" unless endpoints.key?(name)
  BrickFTP::APIComponent.new(endpoints[name][:path_template], endpoints[name][:query_keys])
end

#api_path_for(name, params = {}) ⇒ String

Build path for API endpoint.

Parameters:

  • name (Symbol)
  • params (Hash) (defaults to: {}) —

    mixed path parameters and query parameters.

Returns:

  • (String)


53
54
55
# File 'lib/brick_ftp/api_definition.rb', line 53

def api_path_for(name, params = {})
  api_component_for(name).path(params)
end

#attribute(name, writable: false) ⇒ Object

Define attribute.

Parameters:

  • name (Symbol) —

    name of attribute.

  • writable (Boolean) (defaults to: false)


34
35
36
37
38
39
40
41
# File 'lib/brick_ftp/api_definition.rb', line 34

def attribute(name, writable: false)
  if writable
    writable_attributes << name.to_sym
  else
    readonly_attributes << name.to_sym
  end
  attr_reader name.to_s.tr('-', '_')
end

#attributes ⇒ Array

Return all attributes.

Returns:

  • (Array)


45
46
47
# File 'lib/brick_ftp/api_definition.rb', line 45

def attributes
  writable_attributes + readonly_attributes
end

#endpoint(http_method, name, path_template, *query_keys) ⇒ Object

Define API endpoint.

Parameters:

  • http_method (:Symbol) —

    any one of :get, :post, :put, :delete

  • name (Symbol) —

    any one of :index, :show, :create, :update, :destroy

  • path_template (Stryng) —

    template of endpoint path.

  • query_keys (Array) —

    array of query_string's parameter name.



23
24
25
26
27
28
29
# File 'lib/brick_ftp/api_definition.rb', line 23

def endpoint(http_method, name, path_template, *query_keys)
  endpoints[name] = {
    http_method: http_method,
    path_template: path_template,
    query_keys: query_keys,
  }
end