Skip to content

Commit 4e5f52a

Browse files
committed
Merge branch 'release-1.2.1'
2 parents 12db3a4 + 61e9ddd commit 4e5f52a

5 files changed

Lines changed: 74 additions & 37 deletions

File tree

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
GIT
22
remote: git://github.com/openpearl/PearlEngine.git
3-
revision: 162a546aae6d1f8e331f737ebe81279f05c14175
3+
revision: 978fa99ebdc30e8379a961b31908f66e7fb73653
44
branch: master
55
specs:
66
pearl_engine (1.2.0)

app/controllers/api/v1/conversations_controller.rb

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ class Api::V1::ConversationsController < ApplicationController
44
before_action :authenticate_user!
55

66

7+
# For testing/debugging
8+
def test
9+
10+
end
11+
712
# Returns a storyboard card at the cardID given if a plugin is loaded and initialized.
813
def converse
914
card = @plugin.getCard(context_params["cardID"], current_user.id)
@@ -23,7 +28,8 @@ def converse
2328
def getContextUpdateRequirements
2429
contextRequirements = @plugin.getContextRequirements.with_indifferent_access
2530
contextUpdateRequirements = {}
26-
defaultEndDate = Time.new(2000).to_s(:db)
31+
defaultStartDate = Time.new(2000).to_i
32+
defaultEndDate = Time.now.to_i
2733

2834
# Strip out any custom Pearl context requirements, since the client does not have any knowledge of them.
2935
contextRequirements.keys.each do |context|
@@ -40,10 +46,11 @@ def getContextUpdateRequirements
4046
# first time), then populate it with a default endDate from the year 2000.
4147
contextUpdateRequirements.keys.each do |key|
4248
if data.key?(key) and not data[key].nil?
43-
contextUpdateRequirements[key]["endDate"] = data[key]["endDate"]
49+
contextUpdateRequirements[key]["startDate"] = data[key]["endDate"]
4450
else
45-
contextUpdateRequirements[key]["endDate"] = defaultEndDate
51+
contextUpdateRequirements[key]["startDate"] = defaultStartDate
4652
end
53+
contextUpdateRequirements[key]["endDate"] = defaultEndDate
4754
end
4855

4956
# Return the final hash which should include all the information that the client will need to retrieve the
@@ -73,7 +80,23 @@ def syncContext
7380
end
7481
end
7582

76-
83+
# Returns neatly formatted context data for graphing
84+
def showGraphData
85+
if not @document.nil?
86+
contextRequirements = @plugin.getContextRequirements.with_indifferent_access
87+
contextData = @document.read_tv_document(@@tvVaultID, @@tvAdminAPI, contextRequirements).with_indifferent_access
88+
dataPointsArray = @document.getGraphData(contextData)
89+
render json: {
90+
status: 'success',
91+
data: dataPointsArray
92+
}
93+
else
94+
render json: {
95+
status: 'error',
96+
message: 'Document does not exist!'
97+
}
98+
end
99+
end
77100

78101

79102
private

app/models/document.rb

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Document < ActiveRecord::Base
22
belongs_to :user
3-
3+
44
# Retuns a base 64 encoded json string which represents the default user document on Truevault
55
def get_base_truevault_doc
66
filePath = Rails.root.join("lib", "base_truevault_doc.json")
@@ -19,10 +19,10 @@ def create_tv_document(vault_id, api_key)
1919
-X POST \
2020
-d "document=#{baseTVDoc}"`
2121
# Parse and return a json hash of the TrueVault response
22-
tvResponseJSON = JSON.parse(tvResponse)
22+
tvResponseJSON = JSON.parse(tvResponse)
2323
end
24-
25-
24+
25+
2626
# Reads the TrueVault document belonging to the current user.
2727
# If a hash of context requirements for a plugin are given, returns
2828
# a json of all the values corresponding to each context requirement.
@@ -32,21 +32,21 @@ def read_tv_document(vault_id, api_key, context_requirements = false)
3232
tvDocBase64 = `curl https://api.truevault.com/v1/vaults/#{vault_id}/documents/#{self.documentID} \
3333
-X GET \
3434
-u #{api_key}:`
35-
36-
# Decode the encoded JSON string
37-
tvDocDecode = Base64.decode64(tvDocBase64)
38-
35+
36+
# Decode the encoded JSON string
37+
tvDocDecode = Base64.decode64(tvDocBase64)
38+
3939
# Parse the decoded string as a JSON hash
4040
tvDocDecodeJson = JSON.parse(tvDocDecode).with_indifferent_access
41-
41+
4242
if context_requirements
4343
queryData = {}
4444
context_requirements.keys.each do |key|
45-
if tvDocDecodeJson.key?(key)
45+
if tvDocDecodeJson.key?(key)
4646
queryData[key] = tvDocDecodeJson[key]
47-
end
47+
end
4848
end
49-
return queryData
49+
return queryData
5050
else
5151
return tvDocDecodeJson
5252
end
@@ -61,7 +61,7 @@ def read_tv_document(vault_id, api_key, context_requirements = false)
6161
# Returns a json hash of the updated document
6262
def update_tv_document(vault_id, api_key, document_params)
6363
tvDocDecodeJson = self.read_tv_document(vault_id, api_key)
64-
64+
6565
# For each key in the parameters, check if it already exists in the Truevault document.
6666
# If the key exists and its value is an array, append the new values.
6767
# Otherwise, update the key-value pair or add it if it does not exist.
@@ -75,63 +75,63 @@ def update_tv_document(vault_id, api_key, document_params)
7575
value.keys.each do |replace|
7676
check[replace] = value[replace]
7777
end
78-
exists = true
79-
end
78+
exists = true
79+
end
8080
end
8181
if !exists
8282
tvDocDecodeJson[key].append(value)
83-
end
83+
end
8484
end
8585
else
8686
tvDocDecodeJson[key] = document_params[key]
8787
end
8888
end
89-
90-
end
91-
89+
90+
end
91+
9292
# Encode the updated document as a base 64 encoded JSON string.
93-
tvDocEncode = Base64.encode64(tvDocDecodeJson.to_json)
94-
93+
tvDocEncode = Base64.encode64(tvDocDecodeJson.to_json)
94+
9595
# Update the document on truevualt.
9696
`curl https://api.truevault.com/v1/vaults/#{vault_id}/documents/#{self.documentID} \
9797
-u #{api_key}: \
9898
-X PUT \
9999
-d "document=#{tvDocEncode}"`
100-
100+
101101
# Return the updated document as a json hash
102102
return tvDocDecodeJson
103103
end
104-
105-
104+
105+
106106
# Destroy the TrueVault document
107107
# Returns a json representation of the TrueVault response.
108108
def destroy_tv_document(vault_id, api_key)
109109
tvResponse = `curl https://api.truevault.com/v1/vaults/#{vault_id}/documents/#{self.documentID} \
110110
-X DELETE \
111111
-u #{api_key}:`
112112
# Parse and return a json hash of the TrueVault response
113-
tvResponseJSON = JSON.parse(tvResponse)
113+
tvResponseJSON = JSON.parse(tvResponse)
114114
end
115-
116-
115+
116+
117117
# Reset the TrueVault document to an empty json hash
118118
def reset_tv_document(vault_id, api_key)
119119
baseTVDoc = self.get_base_truevault_doc
120120
tvResponse = `curl https://api.truevault.com/v1/vaults/#{vault_id}/documents/#{self.documentID} \
121121
-u #{api_key}: \
122122
-X PUT \
123123
-d "document=#{baseTVDoc}"`
124-
124+
125125
# Parse and return a json hash of the TrueVault response
126-
tvResponseJSON = JSON.parse(tvResponse)
126+
tvResponseJSON = JSON.parse(tvResponse)
127127
end
128128

129129

130130
# Given the context requirements, gets only the most recent datapoints stored in TrueVault for each context.
131131
def get_latest_datapoints(vault_id, api_key, context_requirements)
132132
queryData = self.read_tv_document(vault_id, api_key, context_requirements)
133133
queryData.keys.each do |key|
134-
if queryData[key].class == Array and not queryData[key].empty?
134+
if queryData[key].class == Array and not queryData[key].empty?
135135
latestPoint = queryData[key][0]
136136
queryData[key].each do |value|
137137
if value["endDate"] > latestPoint["endDate"]
@@ -144,4 +144,17 @@ def get_latest_datapoints(vault_id, api_key, context_requirements)
144144
return queryData
145145
end
146146

147+
148+
def getGraphData(contextData)
149+
dataPointsArray = []
150+
contextData.keys.each do |dataType|
151+
contextData[dataType].each do |dataPoint|
152+
hash = {}
153+
hash["timestamp"] = dataPoint["endDate"]
154+
hash["steps"] = dataPoint["quantity"]
155+
dataPointsArray.push(hash)
156+
end
157+
end
158+
return dataPointsArray
159+
end
147160
end

config/environments/development.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
config.cache_store = :memory_store
99

1010
# Do not eager load code on boot.
11-
config.eager_load = false
11+
config.eager_load = true
1212

1313
# Show full error reports and disable caching.
1414
config.consider_all_requests_local = true

config/routes.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
post 'converse' => 'conversations#converse', as: :converse
1818
get 'context' => 'conversations#getContextUpdateRequirements', as: :context
1919
post 'context' => 'conversations#syncContext', as: :sync
20-
get 'test' => 'conversations#test', as: :test
20+
get 'context/graphs' => 'conversations#showGraphData', as: :graph
21+
post 'test' => 'conversations#test', as: :test
2122
end
2223
end
2324

0 commit comments

Comments
 (0)