Class: GrapeClient::Base
Class Attribute Summary collapse
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.
46
47
48
|
# File 'lib/grape_client/base.rb', line 46
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
71
72
73
74
75
76
77
78
79
|
# File 'lib/grape_client/base.rb', line 71
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
|
Class Attribute Details
.attributes ⇒ Object
Returns the value of attribute attributes.
12
13
14
|
# File 'lib/grape_client/base.rb', line 12
def attributes
@attributes
end
|
.password ⇒ Object
Returns the value of attribute password.
13
14
15
|
# File 'lib/grape_client/base.rb', line 13
def password
@password
end
|
.prefix ⇒ Object
Returns the value of attribute prefix.
13
14
15
|
# File 'lib/grape_client/base.rb', line 13
def prefix
@prefix
end
|
.site ⇒ Object
Returns the value of attribute site.
13
14
15
|
# File 'lib/grape_client/base.rb', line 13
def site
@site
end
|
.user ⇒ Object
Returns the value of attribute user.
13
14
15
|
# File 'lib/grape_client/base.rb', line 13
def user
@user
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
23
24
25
26
27
|
# File 'lib/grape_client/base.rb', line 23
def attr_accessor(*names)
names.each do |name|
attributes << name.to_sym
end
end
|
.cache ⇒ Object
33
34
35
|
# File 'lib/grape_client/base.rb', line 33
def cache
Cache.instance
end
|
.connection ⇒ Object
29
30
31
|
# File 'lib/grape_client/base.rb', line 29
def connection
@connection ||= Connection.new(user, password)
end
|
.endpoint ⇒ Object
41
42
43
|
# File 'lib/grape_client/base.rb', line 41
def endpoint
site + prefix + entity_name.pluralize
end
|
.entity_name ⇒ Object
37
38
39
|
# File 'lib/grape_client/base.rb', line 37
def entity_name
name.split('::').last.underscore
end
|
.inherited(child) ⇒ Object
Instance Method Details
#[](name) ⇒ Object
59
60
61
62
63
|
# File 'lib/grape_client/base.rb', line 59
def [](name)
name = name.to_sym
raise NameError, name unless self.class.attributes.include? name
@attributes[name]
end
|
#[]=(name, value) ⇒ Object
65
66
67
68
69
|
# File 'lib/grape_client/base.rb', line 65
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
81
82
83
84
85
86
87
88
|
# File 'lib/grape_client/base.rb', line 81
def respond_to?(method_name, *args, &block)
super ||
begin
name = method_name.to_s
name = name[0..-2] if name.last == '='
self.class.attributes.include?(name.to_sym)
end
end
|
#to_post ⇒ Object
90
91
92
93
94
95
|
# File 'lib/grape_client/base.rb', line 90
def to_post
list = self.class.attributes
filtered_attributes = attributes.select { |key, _value| list.include? key }
entity_name = self.class.entity_name
filtered_attributes.transform_keys { |key| "#{entity_name}[#{key}]" }
end
|