ncmb_rb_wrapper 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: eb46478908205c729ce5b4170c9f86e14dbc791c
4
+ data.tar.gz: e2c326396b9a13dbfec20d362d37dc883c9732d0
5
+ SHA512:
6
+ metadata.gz: 98b16d1c5b9261d3e4c8a3e55ea3058448c6780b60bb37ca96e57b065c1f30cdb9d59e35c5d7c37dc3ec58f9c8af55538e67679943752210c875132a1dfd6a5a
7
+ data.tar.gz: 7b7587e86443f0dc4737ee7b96c5ba1fad9ec21a81900ee2be7d21cc4138c89d472c9a39d5e76d89bdf7ebfd36706d39b6e62df6c9d9c04f43f959514174570f
data/.DS_Store ADDED
Binary file
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /.idea/
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ncmb_rb_wrapper.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Yuta Suzuki
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # ncmb_rb_wrapper
2
+ Ruby wrapper for NIFTY Cloud mobile backend REST API.
3
+
4
+ #### [NIFTY Cloud mobile backend](http://mb.cloud.nifty.com/)
5
+ - Free for Basic Plan
6
+ - 2,000,000 API requests/month
7
+ - 2,000,000 Push Notifications/month
8
+ - 5GB Object Storage
9
+ - [And more...](http://mb.cloud.nifty.com/price.htm)
10
+
11
+
12
+ ## Installation
13
+ Add to Gemlist
14
+ ```ruby:Gemfile
15
+ gem 'ncmb_rb_wrapper', :github => 'yutaszk/ncmb_rb_wrapper'
16
+ ```
17
+
18
+
19
+ ## Usage
20
+ ### RestClient
21
+ Client class wrap API request.
22
+ Please refer to [REST API Reference](http://mb.cloud.nifty.com/doc/current/rest/common/format.html)
23
+ ```ruby
24
+ @ncmb = Ncmb::Client.new(application_key, client_key)
25
+ @ncmb.get("/classes/app/") #=> all object in 'App' class
26
+ ```
27
+
28
+ ### DataStore
29
+ ```ruby
30
+ @app = Ncmb::DataStore.new(application_key, client_key, 'App')
31
+ @app.post({foo: :bar})
32
+ @app.get(id}
33
+ @app.put(id, {foo: :baz})
34
+ @app.delete(id)
35
+ @app.all()
36
+ @app.search(where: {foo: :bar})
37
+ @app.where({foo: :bar})
38
+ @app.batch_post([{foo: :bar}, {one: 1, two:2}])
39
+ @app.batch_put([{id: ID, query: {foo: baz}, {id: ID, query: {one: :first}])
40
+ @app.batch_delete([ID, ID])
41
+ ```
42
+
43
+ or call method with class name
44
+ ```ruby
45
+ @ds = Ncmb::DataStore.new(pplication_key, client_key)
46
+ @ds.post({foo: :bar}, 'App)
47
+ ```
48
+
49
+ ### FileStore
50
+ lease refer to [REST API File Store Reference](http://mb.cloud.nifty.com/doc/current/rest/filestore/fileRegistration.html)
51
+ ```ruby
52
+ @fs = Ncmb::FileStore.new(application_key, client_key)
53
+ @fs.post(file, acl)
54
+ @fs.get(name) #=> binary
55
+ @fs.delete(name)
56
+ @fs.all()
57
+ @fs.where({fileName: :hoge)
58
+
59
+ #TODO: @fs.put(name, acl)
60
+ #TODO: @fs.public_file(name, application_id)
61
+ ```
62
+
63
+ or call file method by Ncmb::Client object
64
+ ```ruby
65
+ @ncmb.file.post(file, acl)
66
+ ```
67
+
68
+ ### Ruby Friendly Sugar Syntax
69
+ ```ruby
70
+ @app.find #=> search
71
+ @app.save #=> post
72
+ @app.update #=> put
73
+ @app.destroy #=> delete
74
+ ```
75
+
76
+ ## Supported
77
+ ruby 2.0+
78
+
79
+ ## License
80
+
81
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
82
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ncmb_rb_wrapper"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ module Ncmb
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,314 @@
1
+ require 'time'
2
+ require 'uri'
3
+ require 'openssl'
4
+ require 'base64'
5
+ require 'net/http'
6
+ require 'net/http/post/multipart'
7
+ require 'json'
8
+ require 'mimemagic'
9
+
10
+ module Ncmb
11
+ class Client
12
+ def initialize(application_key, client_key)
13
+ @application_key = application_key
14
+ @client_key = client_key
15
+ @api_version = '2013-09-01'
16
+ @domain = 'mb.api.cloud.nifty.com'
17
+ end
18
+
19
+ def get(path, hash_query = {})
20
+ request(:GET, path, hash_query)
21
+ end
22
+
23
+ def post(path, hash_query = {})
24
+ request(:POST, path, hash_query)
25
+ end
26
+
27
+ def put(path, hash_query = {})
28
+ request(:PUT, path, hash_query)
29
+ end
30
+
31
+ def delete(path)
32
+ request(:DELETE, path)
33
+ end
34
+
35
+ def batch(array_request)
36
+ request(:POST, '/batch', requests: array_request)
37
+ end
38
+
39
+ def file
40
+ FileStore.new(@application_key, @client_key)
41
+ end
42
+
43
+ def data(klass = nil)
44
+ if klass.nil?
45
+ DataStore.new(@application_key, @client_key)
46
+ else
47
+ DataStore.new(@application_key, @client_key, klass)
48
+ end
49
+ end
50
+
51
+ private
52
+ def request(method, path, hash_query = {})
53
+ http = Net::HTTP.new(@domain, 443)
54
+ http.use_ssl = true
55
+
56
+ path = "/#{@api_version}#{path}"
57
+ current_time = Time.now.utc.iso8601
58
+ signature = sign(method, hash_query, path, current_time)
59
+
60
+ if block_given?
61
+ yield(http, path, current_time, signature)
62
+ else
63
+ case method
64
+ when :GET
65
+ query = URI.encode_www_form(hash_query.map { |k, v| [k, v.to_json] })
66
+ path = "#{path}?#{query}"
67
+ res = http.get(path, header(signature, current_time))
68
+ JSON.parse(res.body)
69
+ when :POST
70
+ res = http.post(path, hash_query.to_json, header(signature, current_time))
71
+ JSON.parse(res.body)
72
+ when :PUT
73
+ res = http.put(path, hash_query.to_json, header(signature, current_time))
74
+ JSON.parse(res.body)
75
+ when :DELETE
76
+ res = http.delete(path, header(signature, current_time))
77
+ 'successfully deleted.' if res.code == "200"
78
+ else
79
+ raise Exception 'undefined method.'
80
+ end
81
+ end
82
+ # rescue => e
83
+ # puts e
84
+ end
85
+
86
+ def sign(method, hash_query, path, current_time)
87
+ hash = {
88
+ 'SignatureMethod' => 'HmacSHA256',
89
+ 'SignatureVersion' => 2,
90
+ 'X-NCMB-Application-Key' => @application_key,
91
+ 'X-NCMB-Timestamp' => current_time
92
+ }
93
+
94
+ if method == :GET
95
+ hash_query.each do |key, value|
96
+ hash.store(key.to_s, URI.encode_www_form_component(value.to_json))
97
+ end
98
+ end
99
+ hash = Hash[hash.sort]
100
+ chained_param = hash.map { |k, v| "#{k}=#{v}" }.join('&')
101
+ base_param = [method, @domain, path, chained_param].join("\n")
102
+ signature = Base64.encode64(OpenSSL::HMAC::digest('sha256', @client_key, base_param))
103
+ end
104
+
105
+ def header(signature, current_time, content_type = 'application/json')
106
+ {
107
+ 'X-NCMB-Application-Key' => @application_key,
108
+ 'X-NCMB-Signature' => signature,
109
+ 'X-NCMB-Timestamp' => current_time,
110
+ 'Content-Type' => content_type,
111
+ }
112
+ end
113
+ end
114
+
115
+ class DataStore < Client
116
+ alias :super_get :get
117
+
118
+ def initialize(application_key, client_key, klass = nil)
119
+ @klass = klass
120
+ super(application_key, client_key)
121
+ end
122
+
123
+ # # #
124
+ # DataStore method
125
+
126
+ def post(hash_query = {}, klass = @klass)
127
+ validate_klass klass
128
+ super("/classes/#{klass}", hash_query)
129
+ end
130
+
131
+ def get(id, klass = @klass)
132
+ validate_klass klass
133
+ super("/classes/#{klass}/#{id}")
134
+ end
135
+
136
+ def put(id, hash_query, klass = @klass)
137
+ validate_klass klass
138
+ super("/classes/#{klass}/#{id}", hash_query)
139
+ end
140
+
141
+ def delete(id, klass = @klass)
142
+ validate_klass klass
143
+ super("/classes/#{klass}/#{id}")
144
+ end
145
+
146
+ def all(klass = @klass)
147
+ validate_klass klass
148
+ super_get("/classes/#{klass}")
149
+ end
150
+
151
+ def where(hash_query, klass = @klass)
152
+ validate_klass klass
153
+ super_get("/classes/#{klass}", where: hash_query)
154
+ end
155
+
156
+ def search(hash_query = {}, klass = @klass)
157
+ validate_klass klass
158
+ super_get("/classes/#{klass}", hash_query)
159
+ end
160
+
161
+ # ##
162
+ # syntax sugar
163
+
164
+ # syntax sugar of search
165
+ def find(hash_query = {}, klass = @klass)
166
+ search(hash_query, klass)
167
+ end
168
+
169
+ # syntax sugar of post
170
+ def save(hash_query = {}, klass = @klass)
171
+ post(hash_query, klass)
172
+ end
173
+
174
+ # syntax sugar of put
175
+ def update(id, hash_query, klass = @klass)
176
+ put(id, hash_query, klass)
177
+ end
178
+
179
+ # syntax sugar of delete
180
+ def destroy(id, klass = @klass)
181
+ delete(id, klass)
182
+ end
183
+
184
+ # # #
185
+ # batch method
186
+
187
+ # array_query = [
188
+ # {foo: :bar},
189
+ # {hoge: :fuga}
190
+ # ]
191
+ def batch_post(array_query)
192
+ validate_klass @klass
193
+ array_request = array_query.map do |query|
194
+ {
195
+ method: :POST,
196
+ path: "#{@api_version}/classes/#{@klass}",
197
+ body: query
198
+ }
199
+ end
200
+ batch_response batch(array_request)
201
+ end
202
+
203
+ # array_query = [
204
+ # {id: 'XXXXXXXX', query: {foo: :baz}},
205
+ # {id: 'YYYYYYYY', query: {hoge: :piyo}}
206
+ # ]
207
+ # array_query = [
208
+ # ['XXXXXXXX', {foo: :baz}],
209
+ # ['YYYYYYYY', {hoge: :piyo}}
210
+ # ]
211
+ def batch_put(array_query)
212
+ validate_klass @klass
213
+ array_request = array_query.map do |obj|
214
+ case obj
215
+ when Array
216
+ {
217
+ method: :PUT,
218
+ path: "#{@api_version}/classes/#{@klass}/#{obj[0]}",
219
+ body: obj[1]
220
+ }
221
+ when Hash
222
+ {
223
+ method: :PUT,
224
+ path: "#{@api_version}/classes/#{@klass}/#{obj[:id]}",
225
+ body: obj[:query]
226
+ }
227
+ else
228
+ {}
229
+ end
230
+ end
231
+ batch_response batch(array_request)
232
+ end
233
+
234
+ # array_query = [
235
+ # 'XXXXXXXX',
236
+ # 'YYYYYYYY'
237
+ # ]
238
+ def batch_delete(array_query)
239
+ validate_klass @klass
240
+ array_request = array_query.map do |id|
241
+ {
242
+ method: :DELETE,
243
+ path: "#{@api_version}/classes/#{@klass}/#{id}",
244
+ body: {}
245
+ }
246
+ end
247
+ batch_response batch(array_request)
248
+ end
249
+
250
+ private
251
+ def validate_klass(klass)
252
+ raise 'NCMB DataStore Class Name is undefined.' if klass.nil?
253
+ end
254
+
255
+ def batch_response(res)
256
+ def res.success
257
+ self.map { |r| r['success'] }.compact
258
+ end
259
+
260
+ def res.error
261
+ self.map { |r| r['error'] }.compact
262
+ end
263
+
264
+ return res
265
+ end
266
+ end
267
+
268
+ class FileStore < Client
269
+ alias :super_get :get
270
+
271
+ def post(file, acl = {})
272
+ name = File.basename(file)
273
+ path = "/files/#{name}"
274
+ request(:POST, path, acl) { |http, path, current_time, signature|
275
+ req = Net::HTTP::Post::Multipart.new(
276
+ path, {file: UploadIO.new(file, MimeMagic.by_path(name).to_s)},
277
+ header(signature, current_time, 'multipart/form-data')
278
+ )
279
+ res = http.request(req)
280
+ JSON.parse(res.body)
281
+ }
282
+ end
283
+
284
+ def get(name)
285
+ path = "/files/#{name}"
286
+ request(:GET, path) { |http, path, current_time, signature|
287
+ res = http.get(path, header(signature, current_time))
288
+ res.body
289
+ }
290
+ end
291
+
292
+ def put(name, acl)
293
+ super("/files/#{name}/", acl: acl)
294
+ end
295
+
296
+ def delete(name)
297
+ super("/files/#{name}")
298
+ end
299
+
300
+ def all
301
+ super_get('/files')
302
+ end
303
+
304
+ # noinspection RubyArgCount
305
+ def where(hash_query)
306
+ super_get('/files', where: hash_query)
307
+ end
308
+
309
+ # noinspection RubyArgCount
310
+ def search(hash_query = {})
311
+ super_get('/files', hash_query)
312
+ end
313
+ end
314
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ncmb_rb_wrapper/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ncmb_rb_wrapper"
8
+ spec.version = Ncmb::VERSION
9
+ spec.authors = ['yutaszk']
10
+ spec.email = ['yutaszk@gmail.com']
11
+
12
+ spec.description = 'API wrapper for NIFTY Cloud mobile backend'
13
+ spec.summary = spec.description
14
+ spec.homepage = "https://github.com/yutaszk"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "bin"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_dependency "rspec", "~> 3.0"
25
+ spec.add_dependency "multipart-post", "~> 2.0"
26
+ spec.add_dependency "mimemagic", "~> 0.3.0"
27
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ncmb_rb_wrapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - yutaszk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: multipart-post
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mimemagic
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.3.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.3.0
83
+ description: API wrapper for NIFTY Cloud mobile backend
84
+ email:
85
+ - yutaszk@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".DS_Store"
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - lib/ncmb_rb_wrapper.rb
101
+ - lib/ncmb_rb_wrapper/version.rb
102
+ - ncmb_rb_wrapper.gemspec
103
+ homepage: https://github.com/yutaszk
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.4.5
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: API wrapper for NIFTY Cloud mobile backend
127
+ test_files: []