Class: LivePaper::BaseObject
- Inherits:
-
Object
- Object
- LivePaper::BaseObject
show all
- Extended by:
- HttpClient
- Defined in:
- lib/live_paper/base_object.rb
Constant Summary
Constants included
from HttpClient
HttpClient::AUTH_URL, HttpClient::LP_API_HOST
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from HttpClient
check_response, http_request, request_access_token, request_handling_auth, send_request
Constructor Details
#initialize(data = {}) ⇒ BaseObject
Returns a new instance of BaseObject.
16
17
18
|
# File 'lib/live_paper/base_object.rb', line 16
def initialize(data={})
assign_attributes data
end
|
Instance Attribute Details
#date_created ⇒ Object
Returns the value of attribute date_created.
7
8
9
|
# File 'lib/live_paper/base_object.rb', line 7
def date_created
@date_created
end
|
#date_modified ⇒ Object
Returns the value of attribute date_modified.
7
8
9
|
# File 'lib/live_paper/base_object.rb', line 7
def date_modified
@date_modified
end
|
#id ⇒ Object
Returns the value of attribute id.
7
8
9
|
# File 'lib/live_paper/base_object.rb', line 7
def id
@id
end
|
#link ⇒ Object
Returns the value of attribute link.
7
8
9
|
# File 'lib/live_paper/base_object.rb', line 7
def link
@link
end
|
#name ⇒ Object
Returns the value of attribute name.
7
8
9
|
# File 'lib/live_paper/base_object.rb', line 7
def name
@name
end
|
Class Method Details
.api_url ⇒ Object
116
117
118
|
# File 'lib/live_paper/base_object.rb', line 116
def self.api_url
raise NotImplementedError
end
|
.create(data) ⇒ Object
20
21
22
|
# File 'lib/live_paper/base_object.rb', line 20
def self.create(data)
self.new(data).save
end
|
.get(id) ⇒ Object
33
34
35
36
37
38
|
# File 'lib/live_paper/base_object.rb', line 33
def self.get(id)
request_handling_auth("#{api_url}/#{id}", 'GET') do |request|
response = send_request(request, content_type: 'application/json')
parse response.body
end rescue nil
end
|
.item_key ⇒ Object
124
125
126
|
# File 'lib/live_paper/base_object.rb', line 124
def self.item_key
raise NotImplementedError
end
|
.list ⇒ Object
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/live_paper/base_object.rb', line 40
def self.list
objects=[]
request_handling_auth("#{api_url}", 'GET') do |request|
response = send_request(request, content_type: 'application/json')
JSON.parse(response.body, symbolize_names: true)[list_key].each do |linkdata|
objects << self.parse({item_key => linkdata}.to_json)
end
end objects
end
|
.list_key ⇒ Object
120
121
122
|
# File 'lib/live_paper/base_object.rb', line 120
def self.list_key
raise NotImplementedError
end
|
.parse(data) ⇒ Object
108
109
110
|
# File 'lib/live_paper/base_object.rb', line 108
def self.parse(data)
self.new().parse(data)
end
|
Instance Method Details
#assign_attributes(data) ⇒ Object
9
10
11
12
13
14
|
# File 'lib/live_paper/base_object.rb', line 9
def assign_attributes(data)
data.each do |key, value|
method = "#{underscore key.to_s}="
public_send(method, value) if respond_to?(method)
end unless data.empty?
end
|
#delete ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/live_paper/base_object.rb', line 79
def delete
response_code = nil
if self.id
BaseObject.request_handling_auth("#{self.class.api_url}/#{id}", 'DELETE') do |request|
response = BaseObject.send_request(request, content_type: 'application/json', allow_codes: [200, 204, 409])
response_code = case response.code.to_i
when 200
'OK'
when 204
'OK'
when 409
@errors=response.body
'Conflict'
end
end
else
response_code = "Object Invalid"
end
response_code
end
|
#errors ⇒ Object
100
101
102
103
104
105
106
|
# File 'lib/live_paper/base_object.rb', line 100
def errors
begin
JSON.parse(@errors)
rescue
@errors
end
end
|
#parse(data) ⇒ Object
112
113
114
|
# File 'lib/live_paper/base_object.rb', line 112
def parse(data)
raise NotImplementedError
end
|
#rel(key) ⇒ Object
128
129
130
|
# File 'lib/live_paper/base_object.rb', line 128
def rel(key)
link.find { |obj| obj[:rel] == key }[:href] rescue nil
end
|
#save ⇒ Object
24
25
26
27
28
29
30
31
|
# File 'lib/live_paper/base_object.rb', line 24
def save
validate_attributes!
BaseObject.request_handling_auth(self.class.api_url, 'POST') do |request|
response = BaseObject.send_request(request, content_type: 'application/json', body: create_body.to_json)
parse(response.body)
end unless present? @id
self
end
|
#update ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/live_paper/base_object.rb', line 51
def update
puts 'update called'
response_code = 'Object Invalid'
if self.id
BaseObject.request_handling_auth("#{self.class.api_url}/#{id}", 'PUT') do |request|
response = BaseObject.send_request(request,
content_type: 'application/json',
body: update_body.to_json,
allow_codes: [200, 400, 404, 409])
response_code = case response.code.to_i
when 200
puts "update response 200"
parse(response.body)
'OK'
when 400
@errors=response.body
'Bad Request'
when 409
@errors=response.body
'Conflict'
else
'Object Invalid'
end
end
end
response_code
end
|