Module: Jaysus::Remote

Defined in:
lib/jaysus/remote.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.base_urlObject



3
4
5
# File 'lib/jaysus/remote.rb', line 3

def self.base_url
  @base_url ||= ''
end

.base_url=(url) ⇒ Object



7
8
9
# File 'lib/jaysus/remote.rb', line 7

def self.base_url=(url)
  @base_url = url
end

.included(base) ⇒ Object



11
12
13
# File 'lib/jaysus/remote.rb', line 11

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#destroyObject



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

def destroy
  super do
    RestClient.delete(remote_url,{
      'Accept' => 'application/json'
    })
  end
end

#remote_urlObject



44
45
46
47
48
# File 'lib/jaysus/remote.rb', line 44

def remote_url
  pk = self.send(self.class.model_base.primary_key)
  return if pk.blank?
  "#{self.class.model_url}/#{pk}"
end

#saveObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/jaysus/remote.rb', line 23

def save
  super do
    response = if pk = self.send(self.class.model_base.primary_key)
      RestClient.put("#{self.class.model_url}/#{pk}", self.to_json, {
        'Content-Type' => 'application/json',
        'Accept' => 'application/json'
      })
    else
      RestClient.post(self.class.model_url, self.to_json, {
        'Content-Type' => 'application/json',
        'Accept' => 'application/json'
      })
    end
    decoded_response = ActiveSupport::JSON.decode(
      response
    )[self.class.singular_object_name]
    self.set_attributes(decoded_response)
    self
  end
end