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.



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

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



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

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, params: {}) ⇒ Object



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

def self.find(id, params: {})
  data = BrickFTP::HTTPClient.new.send(
    endpoints[:show][:http_method],
    api_path_for(:show, id),
    params: params
  )
  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

#as_json ⇒ Object



71
72
73
# File 'lib/brick_ftp/api/base.rb', line 71

def as_json
  self.class.attributes.each_with_object({}) { |name, res| res[name] = send(name) }
end

#destroy(recursive: false) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/brick_ftp/api/base.rb', line 59

def destroy(recursive: false)
  headers = {}
  headers['Depth'] = 'infinity' if recursive

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

#to_json ⇒ Object



75
76
77
# File 'lib/brick_ftp/api/base.rb', line 75

def to_json
  as_json.to_json
end

#update(params = {}) ⇒ Object



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

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