lib/ncmb/client.rb in ncmb-ruby-client-0.0.6 vs lib/ncmb/client.rb in ncmb-ruby-client-0.0.8
- old
+ new
@@ -1,11 +1,12 @@
-require 'time'
-require 'openssl'
-require 'base64'
-require "net/http"
-require "uri"
-require 'json'
+class Time
+ def to_json(a)
+ v = self.getgm
+ "{\"__type\": \"Date\", \"iso\": \"#{v.iso8601(3)}\"}"
+ end
+end
+
module NCMB
DOMAIN = 'mb.api.cloud.nifty.com'
API_VERSION = '2013-09-01'
@application_key = nil
@client_key = nil
@@ -26,50 +27,69 @@
end
def post(path, params)
request :post, path, params
end
+
+ def put(path, params)
+ request :put, path, params
+ end
+ def delete(path, params)
+ request :delete, path
+ end
+
def array2hash(ary)
new_v = {}
ary.each do |hash|
if hash.is_a? Hash
key = hash.keys[0]
new_v[key] = hash[key]
else
new_v = [hash]
end
end
+ new_v = new_v.sort_by{|a, b| a.to_s}.to_h
new_v
end
def encode_query(queries = {})
results = {}
queries.each do |k, v|
v = array2hash(v) if v.is_a? Array
- if v.is_a? Hash
- results[k.to_s] = URI.escape(v.to_json.to_s, /[^-_.!~*'()a-zA-Z\d;\/?@&=+$,#]/)
- else
- results[k.to_s] = URI.escape(v.to_s, /[^-_.!~*'()a-zA-Z\d;\/?@&=+$,#]/)
- end
+ value = URI.encode(v.is_a?(Hash) ? v.to_json : v.to_s).gsub("[", "%5B").gsub(":", "%3A").gsub("]", "%5D")
+ results[k.to_s] = value
end
results
end
-
+
+ def change_query(queries = {})
+ results = {}
+ queries.each do |k, v|
+ case v
+ when NCMB::Increment
+ queries[k] = v.amount
+ end
+ end
+ queries
+ end
+
def hash2query(queries = {})
results = {}
queries.each do |k, v|
- v = array2hash(v) if v.is_a? Array
- if v.is_a? Hash
- results[k.to_s] = v.to_json.to_s
+ # v = array2hash(v) if v.is_a? Array
+ puts "#{k} -> #{v.class}"
+ case v
+ when Hash, TrueClass, FalseClass, Array then
+ results[k.to_s] = v
+ when Time then
else
results[k.to_s] = v.to_s
end
end
- results.collect do |key, value|
- "#{key}=#{value}"
- end
+ puts results
+ results
end
def generate_signature(method, path, now = nil, queries = {})
params_base = {
"SignatureMethod" => "HmacSHA256",
@@ -89,23 +109,33 @@
end
def request(method, path, queries = {})
now = Time.now.utc.iso8601
signature = generate_signature(method, path, now, queries)
- query = hash2query(queries).join("&")
http = Net::HTTP.new(@domain, 443)
http.use_ssl=true
headers = {
"X-NCMB-Application-Key" => @application_key,
"X-NCMB-Signature" => signature,
"X-NCMB-Timestamp" => now,
"Content-Type" => 'application/json'
}
- if method == :get
- path = path + URI.escape((query == '' ? "" : "?"+query), /[^-_.!~*'()a-zA-Z\d;\/?@&=+$,#]/)
+ # queries = hash2query(queries)
+ case method
+ when :get
+ query = encode_query(queries).map do |key, value|
+ "#{key}=#{value}"
+ end.join("&")
+ path = path + (query == '' ? "" : "?"+query)
return JSON.parse(http.get(path, headers).body, symbolize_names: true)
- else
+ when :post
+ queries = change_query(queries)
return JSON.parse(http.post(path, queries.to_json, headers).body, symbolize_names: true)
+ when :put
+ return JSON.parse(http.put(path, queries.to_json, headers).body, symbolize_names: true)
+ when :delete
+ http.delete(path, headers)
+ return {}
end
end
end
def NCMB.initialize(params = {})