Class: Passwordstate::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/passwordstate/resource.rb

Overview

A simple resource DSL

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Resource

Returns a new instance of Resource.



23
24
25
26
27
# File 'lib/passwordstate/resource.rb', line 23

def initialize(data)
  @client = data.delete :_client
  set! data, false
  old
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



4
5
6
# File 'lib/passwordstate/resource.rb', line 4

def client
  @client
end

Class Method Details

.all(client, query = {}) ⇒ Object Also known as: search



33
34
35
36
37
38
39
40
# File 'lib/passwordstate/resource.rb', line 33

def self.all(client, query = {})
  path = query.fetch(:_api_path, api_path)
  query = passwordstateify_hash query.reject { |k| k.to_s.start_with? '_' }

  [client.request(:get, path, query: query)].flatten.map do |object|
    new object.merge(_client: client)
  end
end

.api_path(path = nil) ⇒ Object



137
138
139
140
# File 'lib/passwordstate/resource.rb', line 137

def api_path(path = nil)
  @api_path = path unless path.nil?
  @api_path
end

.delete(client, object, query = {}) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/passwordstate/resource.rb', line 70

def self.delete(client, object, query = {})
  path = query.fetch(:_api_path, api_path)
  query = passwordstateify_hash query.reject { |k| k.to_s.start_with? '_' }

  object = object.send(object.class.send(index_field)) if object.is_a? Resource
  client.request :delete, "#{path}/#{object}", query: query
end

.get(client, object, query = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/passwordstate/resource.rb', line 42

def self.get(client, object, query = {})
  path = query.fetch(:_api_path, api_path)
  query = passwordstateify_hash query.reject { |k| k.to_s.start_with? '_' }

  object = object.send(object.class.send(index_field)) if object.is_a? Resource
  resp = client.request(:get, "#{path}/#{object}", query: query).map do |data|
    new data.merge(_client: client)
  end
  return resp.first if resp.one? || resp.empty?
  resp
end

.index_field(field = nil) ⇒ Object



142
143
144
145
# File 'lib/passwordstate/resource.rb', line 142

def index_field(field = nil)
  @index_field = field unless field.nil?
  @index_field
end

.passwordstate_to_ruby_field(field) ⇒ Object



147
148
149
150
# File 'lib/passwordstate/resource.rb', line 147

def passwordstate_to_ruby_field(field)
  opts = send(:field_options).find { |(_k, v)| v[:name] == field }
  opts&.first || field.to_s.snake_case.to_sym
end

.passwordstateify_hash(hash) ⇒ Object



78
79
80
# File 'lib/passwordstate/resource.rb', line 78

def self.passwordstateify_hash(hash)
  Hash[hash.map  { |k, v| [ruby_to_passwordstate_field(k), v] }]
end

.post(client, data, query = {}) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/passwordstate/resource.rb', line 54

def self.post(client, data, query = {})
  path = query.fetch(:_api_path, api_path)
  data = passwordstateify_hash data
  query = passwordstateify_hash query.reject { |k| k.to_s.start_with? '_' }

  new [client.request(:post, path, body: data, query: query)].flatten.first.merge(_client: client)
end

.put(client, data, query = {}) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/passwordstate/resource.rb', line 62

def self.put(client, data, query = {})
  path = query.fetch(:_api_path, api_path)
  data = passwordstateify_hash data
  query = passwordstateify_hash query.reject { |k| k.to_s.start_with? '_' }

  client.request :put, path, body: data, query: query
end

.ruby_to_passwordstate_field(field) ⇒ Object



152
153
154
# File 'lib/passwordstate/resource.rb', line 152

def ruby_to_passwordstate_field(field)
  send(:field_options)[field]&.[](:name) || field.to_s.camel_case
end

Instance Method Details

#api_pathObject



82
83
84
# File 'lib/passwordstate/resource.rb', line 82

def api_path
  self.class.instance_variable_get :@api_path
end

#attributes(ignore_redact = true) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/passwordstate/resource.rb', line 86

def attributes(ignore_redact = true)
  Hash[(self.class.send(:accessor_field_names) + self.class.send(:read_field_names) + self.class.send(:write_field_names)).map do |field|
    redact = self.class.send(:field_options)[field]&.fetch(:redact, false) && !ignore_redact
    value = instance_variable_get("@#{field}".to_sym) unless redact
    value = '[ REDACTED ]' if redact
    [field, value]
  end].reject { |_k, v| v.nil? }
end

#delete(query = {}) ⇒ Object



19
20
21
# File 'lib/passwordstate/resource.rb', line 19

def delete(query = {})
  self.class.delete(client, send(self.class.index_field), query)
end

#get(query = {}) ⇒ Object



6
7
8
# File 'lib/passwordstate/resource.rb', line 6

def get(query = {})
  set! self.class.get(client, send(self.class.index_field), query)
end

#inspectObject



95
96
97
# File 'lib/passwordstate/resource.rb', line 95

def inspect
  "#{to_s[0..-2]} #{attributes(false).reject { |_k, v| v.nil? }.map { |k, v| "@#{k}=#{v.inspect}" }.join(', ')}>"
end

#post(body = {}, query = {}) ⇒ Object



15
16
17
# File 'lib/passwordstate/resource.rb', line 15

def post(body = {}, query = {})
  set! self.class.post(client, attributes.merge(body), query)
end

#put(body = {}, query = {}) ⇒ Object



10
11
12
13
# File 'lib/passwordstate/resource.rb', line 10

def put(body = {}, query = {})
  to_send = modified.merge(self.class.index_field => send(self.class.index_field))
  set! self.class.put(client, to_send.merge(body), query).first
end

#stored?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/passwordstate/resource.rb', line 29

def stored?
  !send(self.class.index_field).nil?
end