Class: Amber::Storage::RemoteJson
- Inherits:
-
Amber::Storage
- Object
- Amber::Storage
- Amber::Storage::RemoteJson
- Defined in:
- lib/amber/storage/remote_json.rb
Instance Attribute Summary
Attributes inherited from Amber::Storage
Instance Method Summary collapse
- #deserialize(data) ⇒ Object
-
#initialize(url) ⇒ RemoteJson
constructor
A new instance of RemoteJson.
- #pack(map) ⇒ Object
- #query(data) ⇒ Object
- #request(body = nil) ⇒ Object
- #serialize(data) ⇒ Object
- #unpack(data, map) ⇒ Object
Constructor Details
#initialize(url) ⇒ RemoteJson
Returns a new instance of RemoteJson.
2 3 4 5 6 |
# File 'lib/amber/storage/remote_json.rb', line 2 def initialize(url) @uri = URI.parse(url) @status = false @data = {} end |
Instance Method Details
#deserialize(data) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/amber/storage/remote_json.rb', line 51 def deserialize(data) data_struct = nil begin data_struct = JSON.parse(data) rescue Exception => e p e end data_struct end |
#pack(map) ⇒ Object
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 |
# File 'lib/amber/storage/remote_json.rb', line 61 def pack(map) data = nil if map.is_a? Hash if @data.is_a? Hash data = {} map.each do |key, value| if @data.has_key? key data[value] = @data[key] else data[value] = nil end end elsif @data.is_a? Array data = [] @data.each do |result_data_item| if result_data_item.is_a? Hash data_item = {} map.each do |key, value| if @result_data_item.has_key? key data_item[value] = @result_data_item[key] else data_item[value] = nil end end data.push data_item end end end end data end |
#query(data) ⇒ Object
8 9 10 |
# File 'lib/amber/storage/remote_json.rb', line 8 def query(data) self.request({"token" => "", "data" => data}) end |
#request(body = nil) ⇒ Object
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 |
# File 'lib/amber/storage/remote_json.rb', line 12 def request(body = nil) request = Net::HTTP::Post.new(@uri) # request.body = body if body response = nil @status = false @data = {} result = Net::HTTP.start(@uri.hostname, @uri.port) do |http| response = http.request(request) end case result when Net::HTTPSuccess info = self.deserialize(result.body) if info.is_a? Hash if info.has_key?("status") && info["status"] == 1 @status = true info_data = {} info_data = info["data"] if info.has_key?("data") if info_data.is_a?(Array) || info_data.is_a?(Hash) @data = info_data end else @status = false end end end end |
#serialize(data) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/amber/storage/remote_json.rb', line 40 def serialize(data) data_string = "" begin data = [data] unless data.is_a? Array data_string = JSON.generate(data) rescue Exception => e p e end data_string end |
#unpack(data, map) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/amber/storage/remote_json.rb', line 93 def unpack(data, map) # map.each do |key, value| # if data.is_a? Hash # end # end end |