Class: NCMB::Client
Constant Summary
Constants included from NCMB
API_VERSION, DOMAIN, SCRIPT_API_VERSION, SCRIPT_DOMAIN
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.
-
#script_api_version ⇒ Object
Returns the value of attribute script_api_version.
Instance Method Summary collapse
- #array2hash(ary) ⇒ Object
- #change_query(queries = {}) ⇒ Object
- #delete(path, params = {}, headers = {}) ⇒ Object
- #encode_query(queries = {}) ⇒ Object
- #generate_signature(method, path, now = nil, queries = {}) ⇒ Object
- #get(path, params = {}, headers = {}) ⇒ Object
- #get_domain(path) ⇒ Object
- #hash2query(queries = {}) ⇒ Object
-
#initialize(params = {}) ⇒ Client
constructor
A new instance of Client.
- #make_boundary(boundary, queries) ⇒ Object
- #post(path, params = {}, headers = {}) ⇒ Object
- #put(path, params = {}, headers = {}) ⇒ Object
- #request(method, path, queries = {}, addHeaders = {}) ⇒ Object
Methods included from NCMB
Constructor Details
#initialize(params = {}) ⇒ Client
Returns a new instance of Client.
24 25 26 27 28 29 30 31 |
# File 'lib/ncmb/client.rb', line 24 def initialize(params = {}) @domain = NCMB::DOMAIN @api_version = NCMB::API_VERSION @script_domain = NCMB::SCRIPT_DOMAIN @script_api_version = NCMB::SCRIPT_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.
23 24 25 |
# File 'lib/ncmb/client.rb', line 23 def api_version @api_version end |
#application_key ⇒ Object
Returns the value of attribute application_key.
23 24 25 |
# File 'lib/ncmb/client.rb', line 23 def application_key @application_key end |
#client_key ⇒ Object
Returns the value of attribute client_key.
23 24 25 |
# File 'lib/ncmb/client.rb', line 23 def client_key @client_key end |
#domain ⇒ Object
Returns the value of attribute domain.
23 24 25 |
# File 'lib/ncmb/client.rb', line 23 def domain @domain end |
#script_api_version ⇒ Object
Returns the value of attribute script_api_version.
23 24 25 |
# File 'lib/ncmb/client.rb', line 23 def script_api_version @script_api_version end |
Instance Method Details
#array2hash(ary) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/ncmb/client.rb', line 49 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
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/ncmb/client.rb', line 73 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 = {}, headers = {}) ⇒ Object
45 46 47 |
# File 'lib/ncmb/client.rb', line 45 def delete(path, params = {}, headers = {}) request :delete, path, params, headers end |
#encode_query(queries = {}) ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/ncmb/client.rb', line 63 def encode_query(queries = {}) results = {} queries.each do |k, v| v = array2hash(v) if v.is_a? Array value = URI.encode_www_form_component(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
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 |
# File 'lib/ncmb/client.rb', line 100 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 << get_domain(path) signature_base << path signature_base << params.collect{|k,v| "#{k}=#{v}"}.join("&") Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), @client_key, signature_base.join("\n"))).strip() end |
#get(path, params = {}, headers = {}) ⇒ Object
33 34 35 |
# File 'lib/ncmb/client.rb', line 33 def get(path, params = {}, headers = {}) request :get, path, params, headers end |
#get_domain(path) ⇒ Object
141 142 143 144 145 146 147 |
# File 'lib/ncmb/client.rb', line 141 def get_domain(path) domain = @domain if path.start_with?("/#{@script_api_version}/script\/") domain = @script_domain end domain end |
#hash2query(queries = {}) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/ncmb/client.rb', line 84 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 results end |
#make_boundary(boundary, queries) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/ncmb/client.rb', line 126 def make_boundary(boundary, queries) post_body = [] post_body << "--#{boundary}" post_body << "Content-Disposition: form-data; name=\"file\"; filename=\"#{queries[:fileName]}\"" post_body << "Content-Type: #{queries['mime-type'.to_sym]}" post_body << queries[:file].read post_body << "" post_body << "--#{boundary}" post_body << "Content-Disposition: form-data; name=\"acl\"" post_body << "" post_body << queries[:acl].to_json post_body << "--#{boundary}--" post_body.join("\r\n") end |
#post(path, params = {}, headers = {}) ⇒ Object
37 38 39 |
# File 'lib/ncmb/client.rb', line 37 def post(path, params = {}, headers = {}) request :post, path, params, headers end |
#put(path, params = {}, headers = {}) ⇒ Object
41 42 43 |
# File 'lib/ncmb/client.rb', line 41 def put(path, params = {}, headers = {}) request :put, path, params, headers end |
#request(method, path, queries = {}, addHeaders = {}) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/ncmb/client.rb', line 149 def request(method, path, queries = {}, addHeaders = {}) now = Time.now.utc.iso8601 signature = generate_signature(method, path, now, queries) domain = get_domain(path) 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' } Array(addHeaders || {}).each do |name, value| headers[name] = value end if NCMB.CurrentUser headers['X-NCMB-Apps-Session-Token'] = NCMB.CurrentUser[:sessionToken] 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) rp = Regexp.new "/#{NCMB::API_VERSION}/files/.*" if path =~ rp json = http.get(path, headers).body else json = JSON.parse(http.get(path, headers).body, symbolize_names: true) end when :post req = Net::HTTP::Post.new(path) if path.include? "/2013-09-01/files" boundary = SecureRandom.uuid req.body = make_boundary(boundary, queries) headers["Content-Type"] = "multipart/form-data; boundary=#{boundary}" else queries = change_query(queries) req.body = queries.to_json end headers.each do |key, value| req[key] = value end json = JSON.parse(http.request(req).body, symbolize_names: true) when :put req = Net::HTTP::Put.new(path) if queries[:file].is_a?(File) || queries[:file].is_a?(StringIO) boundary = SecureRandom.uuid req.body = make_boundary(boundary, queries) headers["Content-Type"] = "multipart/form-data; boundary=#{boundary}" else queries = change_query(queries) req.body = queries.to_json end headers.each do |key, value| req[key] = value end json = JSON.parse(http.request(req).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.is_a?(Hash) && json[:error] != nil raise NCMB::APIError.new(json[:error]) end json end |