Class: FORCAST::Firebase::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/forcast/utils/firebase_database.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDatabase



13
14
15
# File 'lib/forcast/utils/firebase_database.rb', line 13

def initialize
  self.headers = {}
end

Instance Attribute Details

#_methodObject

Returns the value of attribute _method.



8
9
10
# File 'lib/forcast/utils/firebase_database.rb', line 8

def _method
  @_method
end

#dataObject

Returns the value of attribute data.



10
11
12
# File 'lib/forcast/utils/firebase_database.rb', line 10

def data
  @data
end

#headersObject

Returns the value of attribute headers.



7
8
9
# File 'lib/forcast/utils/firebase_database.rb', line 7

def headers
  @headers
end

#pathObject

Returns the value of attribute path.



9
10
11
# File 'lib/forcast/utils/firebase_database.rb', line 9

def path
  @path
end

#query_optionsObject

Returns the value of attribute query_options.



11
12
13
# File 'lib/forcast/utils/firebase_database.rb', line 11

def query_options
  @query_options
end

Instance Method Details

#constructorObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/forcast/utils/firebase_database.rb', line 60

def constructor 
    yield
    if self._method == :get || self._method == :delete
    	resp = SERVER_FIREBASE_DATABASE.send(self._method, self.path)
    else
    	if self._method == :update
    		self.data.merge!({:updated_at => Time.now.to_i})
    	else
    		self.data.merge!({:created_at => Time.now.to_i})
    	end
    	resp = SERVER_FIREBASE_DATABASE.send(self._method, self.path, self.data, self.query_options)
    end
    puts "[FIREBASE] URI:"+resp.response.http_header.request_uri.to_s
    puts "[FIREBASE] METHOD:"+resp.response.http_header.request_method.to_s
    puts "[FIREBASE] BODY:"+resp.body.to_s
    resp = resp.body.to_json
    if self.valid_json?(resp) 
      json_recived = JSON.parse(resp)
      return  json_recived
    else
      return false
    end
end

#delete(path, query_options: {}) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/forcast/utils/firebase_database.rb', line 44

def delete(path,query_options: {})
  json_recived = constructor do
      self._method = :delete
      self.path = path
      self.query_options = query_options
  end
end

#get(path, query_options: {}) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/forcast/utils/firebase_database.rb', line 52

def get(path,query_options: {})
  json_recived = constructor do
      self._method = :get
      self.path = path
      self.query_options = query_options
  end
end

#parser(variable, key1 = '') ⇒ Object



94
95
96
97
98
99
100
# File 'lib/forcast/utils/firebase_database.rb', line 94

def parser(variable, key1 = '')
  if key1 == ''
    return JSON.pretty_generate(FORCAST::Server.response_to_json(variable))
  else
    return FORCAST::Server.response_to_json(variable).map {|i| i[key1]}
  end
end

#push(path, data, query_options: {}) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/forcast/utils/firebase_database.rb', line 17

def push(path,data,query_options: {})
  json_recived = constructor do
      self._method = :push
      self.path = path
      self.data = data
      self.query_options = query_options
  end
end

#set(path, data, query_options: {}) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/forcast/utils/firebase_database.rb', line 26

def set(path,data,query_options: {})
  json_recived = constructor do
      self._method = :set
      self.path = path
      self.data = data
      self.query_options = query_options
  end
end

#update(path, data, query_options: {}) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/forcast/utils/firebase_database.rb', line 35

def update(path,data,query_options: {})
  json_recived = constructor do
      self._method = :update
      self.path = path
      self.data = data
      self.query_options = query_options
  end
end

#valid_json?(json) ⇒ Boolean



84
85
86
87
88
89
90
91
92
# File 'lib/forcast/utils/firebase_database.rb', line 84

def valid_json?(json)
  begin
    JSON.parse(json)
    return true
  rescue JSON::ParserError => e
    puts e
    return false
  end
end