Class: The86::Client::Resource

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Validations, Virtus
Defined in:
lib/the86-client/resource.rb,
lib/the86-client/active_model.rb

Direct Known Subclasses

AccessToken, Conversation, Group, Like, Metadatum, Post, User

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#oauth_tokenObject

Assigned by Virtus constructor.



10
11
12
# File 'lib/the86-client/resource.rb', line 10

def oauth_token
  @oauth_token
end

#parentObject

Parent resource, see belongs_to.



13
14
15
# File 'lib/the86-client/resource.rb', line 13

def parent
  @parent
end

Class Method Details

.accepts_nested_attributes_for(name) ⇒ Object

Allow nested attribute creation



52
53
54
# File 'lib/the86-client/resource.rb', line 52

def accepts_nested_attributes_for(name)
  attribute name, Array
end

.belongs_to(name) ⇒ Object

The name of the parent resource attribute. e.g: belongs_to :group



27
28
29
30
# File 'lib/the86-client/resource.rb', line 27

def belongs_to(name)
  alias_method "#{name}=", :parent=
  alias_method name, :parent
end

.collection_path(parent) ⇒ Object

Class methods.



61
62
63
# File 'lib/the86-client/resource.rb', line 61

def self.collection_path(parent)
  [parent && parent.resource_path, @path].compact.join("/")
end

.has_many(name, class_proc) ⇒ Object

The name of a child collection.



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

def has_many(name, class_proc)
  define_method "#{name}=" do |items|
    (@_has_many ||= {})[name] = items
  end
  define_method name do
    has_many_reader(name, class_proc)
  end
end

.has_one(name, class_proc) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/the86-client/resource.rb', line 42

def has_one(name, class_proc)
  define_method "#{name}=" do |instance|
    (@_has_one ||= {})[name] = instance
  end
  define_method name do
    has_one_reader(name, class_proc)
  end
end

.model_nameObject

Model name relative to The86::Client namespace. Names form fields like post instead of the86_client_post.



15
16
17
# File 'lib/the86-client/active_model.rb', line 15

def self.model_name
  ActiveModel::Name.new(self, The86::Client)
end

.path(path) ⇒ Object

The path component for the collection, e.g. “discussions”



21
22
23
# File 'lib/the86-client/resource.rb', line 21

def path(path)
  @path = path
end

Instance Method Details

#==(other) ⇒ Object



114
115
116
117
118
119
# File 'lib/the86-client/resource.rb', line 114

def ==(other)
  other.respond_to?(:attributes) &&
    other.respond_to?(:parent) &&
    attributes == other.attributes &&
    parent == other.parent
end

#delete!Object

TODO: The parent resource should be appropriately updated that the resource no longer exists



79
80
81
82
83
84
# File 'lib/the86-client/resource.rb', line 79

def delete!
  connection.delete(
    path: resource_path,
    status: 204
  ).data
end

#loadObject



90
91
92
93
94
95
96
# File 'lib/the86-client/resource.rb', line 90

def load
  self.attributes = connection.get(
    path: resource_path,
    status: 200
  ).data
  self
end

#patch(attributes) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/the86-client/resource.rb', line 98

def patch(attributes)
  self.attributes = connection.patch(
    path: resource_path,
    data: attributes,
    status: 200
  ).data
end

#persisted?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/the86-client/active_model.rb', line 23

def persisted?
  !!id
end

#resource_pathObject



74
75
76
# File 'lib/the86-client/resource.rb', line 74

def resource_path
  "%s/%s" % [ self.class.collection_path(@parent), url_id ]
end

#saveObject



86
87
88
# File 'lib/the86-client/resource.rb', line 86

def save
  id ? save_existing : save_new
end

#sendable_attributesObject



106
107
108
109
110
111
112
# File 'lib/the86-client/resource.rb', line 106

def sendable_attributes
  attributes.reject do |key, value|
    [:id, :created_at, :updated_at].include?(key) ||
      value.kind_of?(Resource) ||
      value.nil?
  end
end

#to_keyObject



27
28
29
# File 'lib/the86-client/active_model.rb', line 27

def to_key
  nil
end

#to_modelObject



19
20
21
# File 'lib/the86-client/active_model.rb', line 19

def to_model
  self
end

#to_paramObject



31
32
33
# File 'lib/the86-client/active_model.rb', line 31

def to_param
  url_id
end

#url_idObject

The value of the identifier in the URL; numeric ID or string slug. TODO: see ResourceCollection#find.



70
71
72
# File 'lib/the86-client/resource.rb', line 70

def url_id
  id
end