-
Notifications
You must be signed in to change notification settings - Fork 3
/
corona_firebase.lua
151 lines (111 loc) · 3.63 KB
/
corona_firebase.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
-- Corona Firebase, a library to access firebaseio.com DBs from Corona SDK
-- based applications
-- Copyright (C) 2015 Volodymyr Serheiev
local mime = require("mime")
local json = require("json")
local lfs = require "lfs"
-- Usage:
-- firebase = require('corona_firebase')
-- local db = firebase('https://<your-firebase>.firebaseio.com/')
-- db.get(ref, function(event) ... end)
-- https://www.firebase.com/docs/rest/api/#section-streaming
-- https://github.com/CoronaGeek/Corona-SDK-JSON-Example/blob/master/main.lua
local Firebase = {
url = '',
authToken = ''
}
function Firebase:request(ref, method, data, query, callback)
local url = self.url .. ref .. ".json"
local params = {}
params.body = data
if query then
url = url .. query
end
print(url)
-- https://docs.coronalabs.com/api/library/network/request.html
network.request(url, method, callback, params)
end
function Firebase:on(ref, query, callback)
local url = self.url .. ref .. ".json"
local headers = {}
local params = {}
if query then
url = url .. query
end
-- Check for data file and retun it to caller
local streamed_get = function(event)
-- Check for newest file with data
local temp_path = system.pathForFile( "", system.TemporaryDirectory )
local data_last_mod = 0
local data_file
for file in lfs.dir(temp_path) do
-- file is the current file or directory name
-- print( "Found file: " .. file )
if file ~= "." then
local path = system.pathForFile( file, system.TemporaryDirectory )
local file_attr = lfs.attributes( path )
if file_attr.modification > data_last_mod then
data_last_mod = file_attr.modification
data_file = file
end
end
end
if data_file then
-- Read Firebase events file
local path = system.pathForFile( data_file, system.TemporaryDirectory )
local fh, reason = io.open( path, "r" )
if fh then
-- read all contents of file into a string
local contents = fh:read( "*a" )
-- print( "Contents of " .. path )
-- print( contents )
-- Finally calling callback function :)
callback( contents )
else
-- print( "Reason open failed: " .. reason ) -- display failure message in terminal
end
io.close( fh )
end
end
-- Let handle network timeout errors gracefully
inner = function(event)
if ( event.isError ) then
-- print( "Network error! Reconnecting..." )
return self:on(ref, inner)
else
streamed_get(event)
end
end
headers["Accept"] = "text/event-stream"
-- params.body = data
params.headers = headers
params.handleRedirects = true
params.progress = "download"
params.timeout = -1
params.response = {
filename = "data",
baseDirectory = system.TemporaryDirectory
}
-- https://docs.coronalabs.com/api/library/network/request.html
network.request(url, "GET", inner, params)
end
function Firebase:get(ref, query, callback)
self:request(ref, "GET", nil, query, callback)
end
function Firebase:put(ref, data, query, callback)
self:request(ref, "PUT", data, query, callback)
end
function Firebase:post(ref, data, query, callback)
self:request(ref, "POST", data, query, callback)
end
function Firebase:patch(ref, data, query, callback)
self:request(ref, "PATCH", data, query, callback)
end
function Firebase:delete(ref, query, callback)
self:request(ref, "DELETE", nil, query, callback)
end
return function(url)
local firebase = Firebase
firebase.url = url
return firebase
end