Class: BrickFTP::API::Base

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Base

Returns a new instance of Base.



45
46
47
48
# File 'lib/brick_ftp/api/base.rb', line 45

def initialize(params = {})
  @properties = {}
  params.each { |k, v| write_property(k, v) }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object (private)



101
102
103
104
105
# File 'lib/brick_ftp/api/base.rb', line 101

def method_missing(method_name, *args)
  super unless self.class.attributes.include?(method_name.to_sym)

  read_property(method_name)
end

Instance Attribute Details

#propertiesHash{String => Object} (readonly)

Returns Key Value pairs of API properties.

Returns:

  • (Hash{String => Object})

    Key Value pairs of API properties



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

def properties
  @properties
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_jsonObject



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

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

#delete_property(key) ⇒ Object



91
92
93
# File 'lib/brick_ftp/api/base.rb', line 91

def delete_property(key)
  properties.delete(key.to_s)
end

#destroy(recursive: false) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/brick_ftp/api/base.rb', line 63

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

#read_property(key) ⇒ Object



87
88
89
# File 'lib/brick_ftp/api/base.rb', line 87

def read_property(key)
  properties[key.to_s]
end

#to_jsonObject



79
80
81
# File 'lib/brick_ftp/api/base.rb', line 79

def to_json
  as_json.to_json
end

#update(params = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/brick_ftp/api/base.rb', line 50

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| write_property(k, v) }

  self
end

#write_property(key, value) ⇒ Object



83
84
85
# File 'lib/brick_ftp/api/base.rb', line 83

def write_property(key, value)
  properties[key.to_s] = value
end