Class: Lettr::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/lettr/base.rb

Constant Summary collapse

DEFAULT_HEADERS =
{ :accept => :json }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



7
8
9
# File 'lib/lettr/base.rb', line 7

def initialize
  @client = RestClient::Resource.new self.class.site_url, self.class.user, self.class.pass
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



5
6
7
# File 'lib/lettr/base.rb', line 5

def client
  @client
end

Class Method Details

.site_urlObject



11
12
13
# File 'lib/lettr/base.rb', line 11

def self.site_url
  "#{Lettr.protocol}://#{Lettr.host}/"
end

Instance Method Details

#[](path) ⇒ Object



30
31
32
# File 'lib/lettr/base.rb', line 30

def [] path
  client[path]
end

#destroy(object) ⇒ Object



22
23
24
# File 'lib/lettr/base.rb', line 22

def destroy object
  client[object.path].delete DEFAULT_HEADERS
end

#find(path) ⇒ Object



26
27
28
# File 'lib/lettr/base.rb', line 26

def find path
  ActiveSupport::JSON.decode(client[path].get DEFAULT_HEADERS)
end

#save(object) ⇒ Object



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

def save object
  path = object.collection_path
  payload = object.to_payload
  payload.merge! :files => object.files if object.respond_to?(:files) && object.files
  client[path].post(payload, DEFAULT_HEADERS)
end