Class: BrickFTP::API::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/brick_ftp/api/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Base

Returns a new instance of Base.



41
42
43
# File 'lib/brick_ftp/api/base.rb', line 41

def initialize(params = {})
  params.each { |k, v| instance_variable_set(:"@#{k}", v) }
end

Class Method Details

.all(params = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/brick_ftp/api/base.rb', line 10

def self.all(params = {})
  params.symbolize_keys!

  data = BrickFTP::HTTPClient.new.send(
    endpoints[:index][:http_method],
    api_path_for(:index, params),
    params: api_component_for(:index).except_path_and_query(params)
  )
  data.map { |x| new(x.symbolize_keys) }
end

.create(params = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/brick_ftp/api/base.rb', line 29

def self.create(params = {})
  params.symbolize_keys!

  data = BrickFTP::HTTPClient.new.send(
    endpoints[:create][:http_method],
    api_path_for(:create, params),
    params: api_component_for(:create).except_path_and_query(params)
  )
  data = {} if data.is_a?(Array)
  new(data.symbolize_keys)
end

.find(id) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/brick_ftp/api/base.rb', line 21

def self.find(id)
  data = BrickFTP::HTTPClient.new.send(
    endpoints[:show][:http_method],
    api_path_for(:show, id)
  )
  data.empty? ? nil : new(data.symbolize_keys)
end

.inherited(subclass) ⇒ Object



6
7
8
# File 'lib/brick_ftp/api/base.rb', line 6

def self.inherited(subclass)
  subclass.include APIDefinition
end

Instance Method Details

#destroy ⇒ Object



58
59
60
61
62
63
64
# File 'lib/brick_ftp/api/base.rb', line 58

def destroy
  BrickFTP::HTTPClient.new.send(
    self.class.endpoints[:delete][:http_method],
    self.class.api_path_for(:delete, self),
  )
  true
end

#update(params = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/brick_ftp/api/base.rb', line 45

def update(params = {})
  params.symbolize_keys!

  data = BrickFTP::HTTPClient.new.send(
    self.class.endpoints[:update][:http_method],
    self.class.api_path_for(:update, self),
    params: self.class.api_component_for(:update).except_path_and_query(params)
  )
  data.each { |k, v| instance_variable_set(:"@#{k}", v) }

  self
end