Class: NCMB::Client
Constant Summary
Constants included from NCMB
Instance Attribute Summary collapse
-
#api_version ⇒ Object
Returns the value of attribute api_version.
-
#application_key ⇒ Object
Returns the value of attribute application_key.
-
#client_key ⇒ Object
Returns the value of attribute client_key.
-
#domain ⇒ Object
Returns the value of attribute domain.
Instance Method Summary collapse
- #array2hash(ary) ⇒ Object
- #change_query(queries = {}) ⇒ Object
- #delete(path, params = {}, session_key = nil) ⇒ Object
- #encode_query(queries = {}) ⇒ Object
- #generate_signature(method, path, now = nil, queries = {}) ⇒ Object
- #get(path, params = {}, session_key = nil) ⇒ Object
- #hash2query(queries = {}) ⇒ Object
-
#initialize(params = {}) ⇒ Client
constructor
A new instance of Client.
- #post(path, params = {}, session_key = nil) ⇒ Object
- #put(path, params = {}, session_key = nil) ⇒ Object
- #request(method, path, queries = {}, session_key = nil) ⇒ Object
Methods included from NCMB
Constructor Details
#initialize(params = {}) ⇒ Client
Returns a new instance of Client.
20 21 22 23 24 25 |
# File 'lib/ncmb/client.rb', line 20 def initialize(params = {}) @domain = NCMB::DOMAIN @api_version = NCMB::API_VERSION @application_key = params[:application_key] @client_key = params[:client_key] end |
Instance Attribute Details
#api_version ⇒ Object
Returns the value of attribute api_version.
19 20 21 |
# File 'lib/ncmb/client.rb', line 19 def api_version @api_version end |
#application_key ⇒ Object
Returns the value of attribute application_key.
19 20 21 |
# File 'lib/ncmb/client.rb', line 19 def application_key @application_key end |
#client_key ⇒ Object
Returns the value of attribute client_key.
19 20 21 |
# File 'lib/ncmb/client.rb', line 19 def client_key @client_key end |
#domain ⇒ Object
Returns the value of attribute domain.
19 20 21 |
# File 'lib/ncmb/client.rb', line 19 def domain @domain end |
Instance Method Details
#array2hash(ary) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/ncmb/client.rb', line 43 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 |
#change_query(queries = {}) ⇒ Object
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/ncmb/client.rb', line 67 def change_query(queries = {}) results = {} queries.each do |k, v| case v when NCMB::Increment queries[k] = v.amount end end queries end |
#delete(path, params = {}, session_key = nil) ⇒ Object
39 40 41 |
# File 'lib/ncmb/client.rb', line 39 def delete(path, params = {}, session_key = nil) request :delete, path, params, session_key end |
#encode_query(queries = {}) ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'lib/ncmb/client.rb', line 57 def encode_query(queries = {}) results = {} queries.each do |k, v| v = array2hash(v) if v.is_a? Array 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 |
#generate_signature(method, path, now = nil, queries = {}) ⇒ Object
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 |
# File 'lib/ncmb/client.rb', line 95 def generate_signature(method, path, now = nil, queries = {}) params_base = { "SignatureMethod" => "HmacSHA256", "SignatureVersion" => "2", "X-NCMB-Application-Key" => @application_key } params = method == :get ? params_base.merge(encode_query(queries)) : params_base now ||= Time.now.utc.iso8601 params = params.merge "X-NCMB-Timestamp" => now if [].respond_to?("to_h") # Array#to_h inpremented over ruby 2.1 params = params.sort_by{|a, b| a.to_s}.to_h else sorted_params = {} params = params.sort_by{|a, b| a.to_s}.each {|kv| sorted_params[kv[0]] = kv[1] } params = sorted_params end signature_base = [] signature_base << method.upcase signature_base << @domain signature_base << path signature_base << params.collect{|k,v| "#{k}=#{v}"}.join("&") signature = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), @client_key, signature_base.join("\n"))).strip() end |
#get(path, params = {}, session_key = nil) ⇒ Object
27 28 29 |
# File 'lib/ncmb/client.rb', line 27 def get(path, params = {}, session_key = nil) request :get, path, params, session_key end |
#hash2query(queries = {}) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/ncmb/client.rb', line 78 def hash2query(queries = {}) results = {} queries.each do |k, v| # 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 puts results results end |
#post(path, params = {}, session_key = nil) ⇒ Object
31 32 33 |
# File 'lib/ncmb/client.rb', line 31 def post(path, params = {}, session_key = nil) request :post, path, params, session_key end |
#put(path, params = {}, session_key = nil) ⇒ Object
35 36 37 |
# File 'lib/ncmb/client.rb', line 35 def put(path, params = {}, session_key = nil) request :put, path, params, session_key end |
#request(method, path, queries = {}, session_key = nil) ⇒ Object
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 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/ncmb/client.rb', line 121 def request(method, path, queries = {}, session_key = nil) now = Time.now.utc.iso8601 signature = generate_signature(method, path, now, queries) 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 session_key headers['X-NCMB-Apps-Session-Token'] = session_key end # queries = hash2query(queries) json = nil begin case method when :get query = encode_query(queries).map do |key, value| "#{key}=#{value}" end.join("&") path = path + (query == '' ? "" : "?"+query) json = JSON.parse(http.get(path, headers).body, symbolize_names: true) when :post queries = change_query(queries) json = JSON.parse(http.post(path, queries.to_json, headers).body, symbolize_names: true) when :put json = JSON.parse(http.put(path, queries.to_json, headers).body, symbolize_names: true) when :delete response = http.delete(path, headers).body return true if response == "" json = JSON.parse(response, symbolize_names: true) end rescue => e @@last_error = e raise NCMB::APIError.new(e.to_s) end if json[:error] != nil raise NCMB::APIError.new(json[:error]) end json end |