11class 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
147160end
0 commit comments