diograph-store 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/diograph_store.rb +228 -0
- data/lib/diograph_store/diograph_api_request.rb +67 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: aba6dfca1e4d5a51a0262d4e109a83b9801df278
|
4
|
+
data.tar.gz: 434e5083fa5bbb9c255281016a54d50ec1f7eafb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e3b17ea5b9c55bc4060420bd3715af5d4b56a7d3ea1ff5a5b2e8b704fc3382fff37bccf3a72fbd7a5aa1cd4db1675e445e8be45e36791f1a192839cd0825fca8
|
7
|
+
data.tar.gz: 6d1aef7470a44c1fd30b801289cb7f7ea2e6fe16f0d164c971ccbb5bf0f44c0b394b677c33c0c26ca75d0ac111edfda78c1ef7af60a28f0e087248911fe9fcc3
|
@@ -0,0 +1,228 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'diograph_store/store'
|
4
|
+
require 'diograph_store/diograph_api_request'
|
5
|
+
require 'models/room'
|
6
|
+
require_relative 'diograph'
|
7
|
+
|
8
|
+
### DOKUMENTAATIO
|
9
|
+
module DiographStore
|
10
|
+
class ErrorObject < StandardError; end
|
11
|
+
class DiographStore
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
@diories = {}
|
15
|
+
@connections = {}
|
16
|
+
end
|
17
|
+
|
18
|
+
def get_diory(diory_id)
|
19
|
+
# Validate ID
|
20
|
+
return nil if diory_id.nil?
|
21
|
+
raise 'Given diory_id is not an UUID!' if diory_id.class != String
|
22
|
+
|
23
|
+
# Look up from object store
|
24
|
+
diory_object = @diories[diory_id]
|
25
|
+
return diory_object unless diory_object.nil?
|
26
|
+
|
27
|
+
# Look up from JSON store
|
28
|
+
diory = Store.new(type: 'diories', object_store: self).get(diory_id)
|
29
|
+
# If not found or doesn't have connections => make request to retrieve it from server
|
30
|
+
# - if doesn't have connections => its connections may have not been retrieved
|
31
|
+
unless diory.nil? || diory.connections.empty?
|
32
|
+
@diories[diory_id] = diory
|
33
|
+
return diory
|
34
|
+
end
|
35
|
+
|
36
|
+
# Request from server
|
37
|
+
request = DiographApiRequest.new(
|
38
|
+
method: 'GET',
|
39
|
+
endpoint: "/diories/#{diory_id}",
|
40
|
+
payload: {diory_id: diory_id}
|
41
|
+
)
|
42
|
+
response = request.execute
|
43
|
+
|
44
|
+
return if response.code == 404
|
45
|
+
raise ErrorObject.new(response) unless response.code == 200
|
46
|
+
|
47
|
+
# Save to JSON store
|
48
|
+
Store.store_data(response)
|
49
|
+
|
50
|
+
# Save to object store
|
51
|
+
diory = Store.new(type: 'diories', object_store: self).get(diory_id)
|
52
|
+
@diories[diory_id] = diory
|
53
|
+
diory
|
54
|
+
end
|
55
|
+
|
56
|
+
def get_connection(connection_id)
|
57
|
+
# Validate ID
|
58
|
+
return nil if connection_id.nil?
|
59
|
+
raise 'Given diory_id is not an UUID!' if connection_id.class != String
|
60
|
+
|
61
|
+
# Look up from object store
|
62
|
+
connection_object = @connections[connection_id]
|
63
|
+
return connection_object unless connection_object.nil?
|
64
|
+
|
65
|
+
# Look up from JSON store
|
66
|
+
connection = Store.new(type: 'connections', object_store: self).get(connection_id)
|
67
|
+
|
68
|
+
unless connection.nil?
|
69
|
+
@connections[connection_id] = connection
|
70
|
+
return connection
|
71
|
+
end
|
72
|
+
|
73
|
+
# Request from server
|
74
|
+
request = DiographApiRequest.new(
|
75
|
+
method: 'GET',
|
76
|
+
endpoint: "/connections/#{connection_id}",
|
77
|
+
payload: {connection_id: connection_id}
|
78
|
+
)
|
79
|
+
response = request.execute
|
80
|
+
|
81
|
+
return if response.code == 404
|
82
|
+
raise ErrorObject.new(response) unless response.code == 200
|
83
|
+
|
84
|
+
# Save to JSON store
|
85
|
+
Store.store_data(response)
|
86
|
+
|
87
|
+
# Save to object store
|
88
|
+
connection = Store.new(type: 'connections', object_store: self).get(connection_id)
|
89
|
+
@connections[connection_id] = connection
|
90
|
+
connection
|
91
|
+
end
|
92
|
+
|
93
|
+
def find_or_create_diory(diory_hash)
|
94
|
+
diory_hash['diory-id'] = diory_hash['diory_id'] if diory_hash['diory_id']
|
95
|
+
created_diory_diory_id = diory_hash['diory-id']
|
96
|
+
diory = Store.new(type: 'diories', object_store: self).get(created_diory_diory_id)
|
97
|
+
if diory.nil?
|
98
|
+
diory = create_diory(diory_hash)
|
99
|
+
else
|
100
|
+
@diories[created_diory_diory_id] = diory
|
101
|
+
diory
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def create_diory(diory_hash)
|
106
|
+
request = DiographApiRequest.new(
|
107
|
+
method: 'POST',
|
108
|
+
endpoint: '/diories',
|
109
|
+
payload: diory_hash
|
110
|
+
)
|
111
|
+
response = request.execute
|
112
|
+
|
113
|
+
raise ErrorObject.new(response) unless response.code == 201
|
114
|
+
|
115
|
+
# Save to JSON store
|
116
|
+
Store.store_data(response)
|
117
|
+
|
118
|
+
# Save to object store
|
119
|
+
created_diory_diory_id = response['data']['attributes']['diory-id']
|
120
|
+
diory = Store.new(type: 'diories', object_store: self).get(created_diory_diory_id)
|
121
|
+
@diories[created_diory_diory_id] = diory
|
122
|
+
diory
|
123
|
+
end
|
124
|
+
|
125
|
+
def create_connection(from_diory_id:, to_diory_id:, connection_type:)
|
126
|
+
request = DiographApiRequest.new(
|
127
|
+
method: 'POST',
|
128
|
+
endpoint: '/connections',
|
129
|
+
type: 'connections',
|
130
|
+
payload: {
|
131
|
+
'from-diory-diory-id' => from_diory_id,
|
132
|
+
'to-diory-diory-id' => to_diory_id,
|
133
|
+
'connection-type' => connection_type,
|
134
|
+
# 'connection-id' => connection_id
|
135
|
+
}
|
136
|
+
)
|
137
|
+
response = request.execute
|
138
|
+
|
139
|
+
raise ErrorObject.new(response) unless response.code == 201
|
140
|
+
|
141
|
+
# Save to JSON store
|
142
|
+
Store.store_data(response)
|
143
|
+
|
144
|
+
# Save to object store
|
145
|
+
created_connection_connection_id = response['data']['attributes']['connection-id']
|
146
|
+
connection = Store.new(type: 'connections', object_store: self).get(created_connection_connection_id)
|
147
|
+
@connections[created_connection_connection_id] = connection
|
148
|
+
connection
|
149
|
+
end
|
150
|
+
|
151
|
+
def update_diory_attribute(diory:, attribute_name:, value:)
|
152
|
+
request = DiographApiRequest.new(
|
153
|
+
method: 'PUT',
|
154
|
+
endpoint: "/diories/#{diory.api_id}",
|
155
|
+
type: 'diories',
|
156
|
+
payload: {
|
157
|
+
attribute_name.gsub('_', '-') => value
|
158
|
+
}
|
159
|
+
)
|
160
|
+
response = request.execute
|
161
|
+
|
162
|
+
raise ErrorObject.new(response) unless response.code == 200
|
163
|
+
|
164
|
+
# Save to JSON store
|
165
|
+
Store.store_data(response)
|
166
|
+
|
167
|
+
# Save to object store
|
168
|
+
updated_diory_diory_id = response['data']['attributes']['diory-id']
|
169
|
+
diory = Store.new(type: 'diories', object_store: self).get(updated_diory_diory_id)
|
170
|
+
@diories[updated_diory_diory_id] = diory
|
171
|
+
diory
|
172
|
+
end
|
173
|
+
|
174
|
+
def update_connection_attribute(connection:, attribute_name:, value:)
|
175
|
+
request = DiographApiRequest.new(
|
176
|
+
method: 'PUT',
|
177
|
+
endpoint: "/connections/#{connection.api_id}",
|
178
|
+
type: 'connections',
|
179
|
+
payload: {
|
180
|
+
attribute_name.gsub('_', '-') => value
|
181
|
+
}
|
182
|
+
)
|
183
|
+
response = request.execute
|
184
|
+
|
185
|
+
raise ErrorObject.new(response) unless response.code == 200
|
186
|
+
|
187
|
+
# Save to JSON store
|
188
|
+
Store.store_data(response)
|
189
|
+
|
190
|
+
# Save to object store
|
191
|
+
updated_connection_connection_id = response['data']['attributes']['connection-id']
|
192
|
+
connection = Store.new(type: 'connections', object_store: self).get(updated_connection_connection_id)
|
193
|
+
@connections[updated_connection_connection_id] = connection
|
194
|
+
connection
|
195
|
+
end
|
196
|
+
|
197
|
+
def create_room(room_id, name="Newly created room")
|
198
|
+
request = DiographApiRequest.new(
|
199
|
+
method: 'POST',
|
200
|
+
endpoint: "/rooms",
|
201
|
+
type: 'rooms',
|
202
|
+
payload: {
|
203
|
+
'name' => name,
|
204
|
+
'room-id' => room_id
|
205
|
+
}
|
206
|
+
)
|
207
|
+
response = request.execute
|
208
|
+
|
209
|
+
raise ErrorObject.new(response) unless response.code == 201
|
210
|
+
|
211
|
+
Room.new(response['data']['attributes'])
|
212
|
+
end
|
213
|
+
|
214
|
+
def get_room(room_id)
|
215
|
+
request = DiographApiRequest.new(
|
216
|
+
method: 'GET',
|
217
|
+
endpoint: "/rooms/#{room_id}",
|
218
|
+
)
|
219
|
+
response = request.execute
|
220
|
+
return if response.code == 404
|
221
|
+
|
222
|
+
raise ErrorObject.new(response) unless response.code == 200
|
223
|
+
|
224
|
+
Room.new(response['data']['attributes'])
|
225
|
+
end
|
226
|
+
|
227
|
+
end
|
228
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'httparty'
|
4
|
+
|
5
|
+
module DiographStore
|
6
|
+
class DiographApiRequest
|
7
|
+
def initialize(method:, endpoint:, type: 'diories', payload: nil)
|
8
|
+
if ENV['DIOGRAPH_STORE_HOST'].nil? || ENV['DIOGRAPH_STORE_TOKEN'].nil?
|
9
|
+
raise 'DIOGRAPH_STORE_HOST or DIOGRAPH_STORE_TOKEN environmental variables not defined'
|
10
|
+
end
|
11
|
+
|
12
|
+
raise "Endpoint should start with / (you gave: #{endpoint})" unless endpoint[0] == '/'
|
13
|
+
raise "Unknown HTTP method: #{method}" unless ["GET", "POST", "PUT"].include?(method)
|
14
|
+
|
15
|
+
@host = ENV['DIOGRAPH_STORE_HOST']
|
16
|
+
@room_token = ENV['DIOGRAPH_STORE_TOKEN']
|
17
|
+
@user_token = ENV['DIOGRAPH_USER_TOKEN']
|
18
|
+
|
19
|
+
@method = method
|
20
|
+
@endpoint = endpoint
|
21
|
+
@diory_hash = payload
|
22
|
+
@type = type
|
23
|
+
end
|
24
|
+
|
25
|
+
def execute
|
26
|
+
case @method.upcase
|
27
|
+
when 'GET'
|
28
|
+
HTTParty.get(endpoint, options)
|
29
|
+
when 'POST'
|
30
|
+
HTTParty.post(endpoint, options)
|
31
|
+
when 'PUT'
|
32
|
+
HTTParty.put(endpoint, options)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def endpoint
|
37
|
+
"#{@host}/v1#{@endpoint}"
|
38
|
+
end
|
39
|
+
|
40
|
+
def options
|
41
|
+
{
|
42
|
+
headers: headers,
|
43
|
+
body: body(@diory_hash, @type)
|
44
|
+
}
|
45
|
+
end
|
46
|
+
|
47
|
+
# private
|
48
|
+
|
49
|
+
def headers
|
50
|
+
token = @endpoint.match(/^\/rooms/) ? @user_token : @room_token
|
51
|
+
{
|
52
|
+
'Content-Type' => 'application/vnd.api+json',
|
53
|
+
'Authorization' => token
|
54
|
+
}
|
55
|
+
end
|
56
|
+
|
57
|
+
def body(attributes, type)
|
58
|
+
{
|
59
|
+
'data' => {
|
60
|
+
'id' => endpoint.split('/').last.match('^\d*$') ? endpoint.split('/').last : nil,
|
61
|
+
'type' => type,
|
62
|
+
'attributes' => attributes
|
63
|
+
}.compact
|
64
|
+
}.to_json
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: diograph-store
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jouni Alanen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-02-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: httparty
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.16.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.16.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pry
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: SDK for using Diograph API
|
70
|
+
email: jouni@diory.me
|
71
|
+
executables: []
|
72
|
+
extensions: []
|
73
|
+
extra_rdoc_files: []
|
74
|
+
files:
|
75
|
+
- lib/diograph_store.rb
|
76
|
+
- lib/diograph_store/diograph_api_request.rb
|
77
|
+
homepage: http://dioryme.github.io
|
78
|
+
licenses: []
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.6.11
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: SDK for using Diograph API
|
100
|
+
test_files: []
|