Class: GrapeClient::Base
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from HasMany
has_many
Methods included from BelongsTo
belongs_to
all, create, find, where
#destroy, #reload, #save, #save!
Constructor Details
#initialize(attrs = {}) ⇒ Base
Returns a new instance of Base.
42
43
44
|
# File 'lib/grape_client/base.rb', line 42
def initialize(attrs = {})
self.attributes = attrs
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args) ⇒ Object
67
68
69
70
71
72
73
74
75
|
# File 'lib/grape_client/base.rb', line 67
def method_missing(m, *args)
m = m.to_s
if m.last == '='
name = m[0..-2]
self[name] = args.first
else
self[m]
end
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
6
7
8
|
# File 'lib/grape_client/base.rb', line 6
def attributes
@attributes
end
|
Class Method Details
.attr_accessor(*names) ⇒ Object
13
14
15
16
17
18
|
# File 'lib/grape_client/base.rb', line 13
def attr_accessor(*names)
attributes = self.attributes
names.each do |name|
attributes << name.to_sym
end
end
|
.attributes ⇒ Object
36
37
38
39
|
# File 'lib/grape_client/base.rb', line 36
def attributes
class_variable_set('@@attributes', []) unless class_variable_defined?('@@attributes')
class_variable_get('@@attributes')
end
|
.cache ⇒ Object
24
25
26
|
# File 'lib/grape_client/base.rb', line 24
def cache
Cache.instance
end
|
.connection ⇒ Object
20
21
22
|
# File 'lib/grape_client/base.rb', line 20
def connection
@connection ||= Connection.new(user, password)
end
|
.endpoint ⇒ Object
32
33
34
|
# File 'lib/grape_client/base.rb', line 32
def endpoint
site + prefix + entity_name.pluralize
end
|
.entity_name ⇒ Object
28
29
30
|
# File 'lib/grape_client/base.rb', line 28
def entity_name
name.split('::').last.underscore
end
|
Instance Method Details
#[](name) ⇒ Object
55
56
57
58
59
|
# File 'lib/grape_client/base.rb', line 55
def [](name)
name = name.to_sym
raise NameError, name unless self.class.attributes.include? name
@attributes[name]
end
|
#[]=(name, value) ⇒ Object
61
62
63
64
65
|
# File 'lib/grape_client/base.rb', line 61
def []=(name, value)
name = name.to_sym
raise NameError, name unless self.class.attributes.include? name
@attributes[name] = value
end
|
#respond_to?(method_name, *args, &block) ⇒ Boolean
77
78
79
80
81
|
# File 'lib/grape_client/base.rb', line 77
def respond_to?(method_name, *args, &block)
name = method_name.to_s
name = name[0..-2] if name.last == '='
self.class.attributes.include?(name.to_sym) || super
end
|
#to_post ⇒ Object
83
84
85
86
87
88
|
# File 'lib/grape_client/base.rb', line 83
def to_post
entity_name = self.class.entity_name
list = self.class.attributes
filtered_attributes = attributes.select { |key, _value| list.include? key }
filtered_attributes.transform_keys { |key| "#{entity_name}[#{key}]" }
end
|