Class: Schatter::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/schatter/resource.rb

Direct Known Subclasses

Conversation, Message, Person, Session

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Resource

Returns a new instance of Resource.



5
6
7
8
9
# File 'lib/schatter/resource.rb', line 5

def initialize params
  @url = params[:url]
  @resource = params[:resource]
  @url = links[:self] if @resource
end

Instance Method Details

#delete(url, params = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/schatter/resource.rb', line 52

def delete url, params={}
  params[:auth_token] = ENV['SCHATTER_AUTH_TOKEN']
  full_url = "#{url}?#{params.map{ |k,v| "#{k}=#{CGI.escape v.to_s}" }.join('&')}"
  puts "DELETE #{full_url}" if ENV['DEBUG']
  response = HTTParty.delete full_url,
    headers: {'Accept' => 'application/json'}
  puts response if ENV['DEBUG']
  response
end

#destroyObject



11
12
13
# File 'lib/schatter/resource.rb', line 11

def destroy
  delete @url
end

#formatted_timestampObject



38
39
40
# File 'lib/schatter/resource.rb', line 38

def formatted_timestamp
  timestamp.strftime "%d/%m/%Y %H:%M:%S"
end

#get(url, params = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/schatter/resource.rb', line 42

def get url, params={}
  params[:auth_token] = ENV['SCHATTER_AUTH_TOKEN']
  full_url = "#{url}?#{params.map{ |k,v| "#{k}=#{CGI.escape v.to_s}" }.join('&')}"
  puts "GET #{full_url}" if ENV['DEBUG']
  response = HTTParty.get full_url,
    headers: {'Accept' => 'application/json'}
  puts response if ENV['DEBUG']
  response
end


20
21
22
23
24
25
26
27
28
# File 'lib/schatter/resource.rb', line 20

def links
  return @links if @links
  @links = {}
  resource['_links'].each do |k, v|
    @links[k.to_sym] = v['href']
  end
  puts "#{@links}" if ENV['DEBUG']
  @links
end

#post(url, body) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/schatter/resource.rb', line 62

def post url, body
  body[:auth_token] = ENV['SCHATTER_AUTH_TOKEN']
  puts "POST #{url} #{body.to_json}" if ENV['DEBUG']
  response = HTTParty.post url,
    headers: {'Accept' => 'application/json', 'Content-Type' => 'application/json'},
    body: body.to_json
  puts response if ENV['DEBUG']
  response
end

#resourceObject



15
16
17
18
# File 'lib/schatter/resource.rb', line 15

def resource
  return @resource if @resource
  @resource = get @url
end

#timestampObject



34
35
36
# File 'lib/schatter/resource.rb', line 34

def timestamp
  Time.at resource['timestamp']
end

#uuidObject



30
31
32
# File 'lib/schatter/resource.rb', line 30

def uuid
  resource['uuid']
end