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.
44
45
46
|
# File 'lib/grape_client/base.rb', line 44
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
69
70
71
72
73
74
75
76
77
|
# File 'lib/grape_client/base.rb', line 69
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.
5
6
7
|
# File 'lib/grape_client/base.rb', line 5
def attributes
@attributes
end
|
Class Method Details
.attr_accessor(*names) ⇒ Object
21
22
23
24
25
|
# File 'lib/grape_client/base.rb', line 21
def attr_accessor(*names)
names.each do |name|
attributes << name.to_sym
end
end
|
.cache ⇒ Object
31
32
33
|
# File 'lib/grape_client/base.rb', line 31
def cache
Cache.instance
end
|
.connection ⇒ Object
27
28
29
|
# File 'lib/grape_client/base.rb', line 27
def connection
@connection ||= Connection.new(user, password)
end
|
.endpoint ⇒ Object
39
40
41
|
# File 'lib/grape_client/base.rb', line 39
def endpoint
site + prefix + entity_name.pluralize
end
|
.entity_name ⇒ Object
35
36
37
|
# File 'lib/grape_client/base.rb', line 35
def entity_name
name.split('::').last.underscore
end
|
Instance Method Details
#[](name) ⇒ Object
57
58
59
60
61
|
# File 'lib/grape_client/base.rb', line 57
def [](name)
name = name.to_sym
raise NameError, name unless self.class.attributes.include? name
@attributes[name]
end
|
#[]=(name, value) ⇒ Object
63
64
65
66
67
|
# File 'lib/grape_client/base.rb', line 63
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
79
80
81
82
83
|
# File 'lib/grape_client/base.rb', line 79
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
85
86
87
88
89
90
|
# File 'lib/grape_client/base.rb', line 85
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
|