Class: GrapeClient::Base

Inherits:
Object
  • Object
show all
Extended by:
BelongsTo, HasMany, RestMethodsCollection
Includes:
RestMethodsMember
Defined in:
lib/grape_client/base.rb

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

Methods included from RestMethodsCollection

all, create, find, where

Methods included from RestMethodsMember

#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

.attributesObject (readonly)

Returns the value of attribute attributes.



12
13
14
# File 'lib/grape_client/base.rb', line 12

def attributes
  @attributes
end

.passwordObject

Returns the value of attribute password.



13
14
15
# File 'lib/grape_client/base.rb', line 13

def password
  @password
end

.prefixObject

Returns the value of attribute prefix.



13
14
15
# File 'lib/grape_client/base.rb', line 13

def prefix
  @prefix
end

.siteObject

Returns the value of attribute site.



13
14
15
# File 'lib/grape_client/base.rb', line 13

def site
  @site
end

.userObject

Returns the value of attribute user.



13
14
15
# File 'lib/grape_client/base.rb', line 13

def user
  @user
end

Instance Attribute Details

#attributesObject

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

.cacheObject



33
34
35
# File 'lib/grape_client/base.rb', line 33

def cache
  Cache.instance
end

.connectionObject



29
30
31
# File 'lib/grape_client/base.rb', line 29

def connection
  @connection ||= Connection.new(user, password)
end

.endpointObject



41
42
43
# File 'lib/grape_client/base.rb', line 41

def endpoint
  site + prefix + entity_name.pluralize
end

.entity_nameObject



37
38
39
# File 'lib/grape_client/base.rb', line 37

def entity_name
  name.split('::').last.underscore
end

.inherited(child) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/grape_client/base.rb', line 15

def inherited(child)
  child.instance_variable_set('@attributes', attributes.try(:dup) || [])
  child.instance_variable_set('@site',       GrapeClient.configuration.site)
  child.instance_variable_set('@user',       GrapeClient.configuration.user)
  child.instance_variable_set('@password',   GrapeClient.configuration.password)
  child.instance_variable_set('@prefix',     GrapeClient.configuration.prefix)
end

Instance Method Details

#[](name) ⇒ Object

Raises:

  • (NameError)


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

Raises:

  • (NameError)


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

Returns:

  • (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_postObject



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