ncmb-ruby-client 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +1 -0
- data/.rubocop.yml +2 -0
- data/Gemfile +4 -1
- data/README.md +107 -2
- data/Rakefile +6 -0
- data/examples/acl_test.rb +12 -6
- data/examples/csv_test.rb +17 -8
- data/examples/data_store.rb +13 -5
- data/examples/file_test.rb +18 -12
- data/examples/performance.rb +12 -6
- data/examples/pointer_test.rb +11 -5
- data/examples/push.rb +13 -7
- data/examples/relation.rb +11 -5
- data/examples/script.rb +25 -0
- data/examples/signature.rb +18 -4
- data/examples/user_test.rb +12 -5
- data/examples/venue_search.rb +30 -17
- data/lib/ncmb.rb +23 -20
- data/lib/ncmb/acl.rb +10 -2
- data/lib/ncmb/client.rb +35 -19
- data/lib/ncmb/data_store.rb +2 -0
- data/lib/ncmb/device.rb +3 -1
- data/lib/ncmb/error.rb +2 -0
- data/lib/ncmb/file.rb +3 -1
- data/lib/ncmb/geo_point.rb +6 -4
- data/lib/ncmb/increment.rb +2 -0
- data/lib/ncmb/object.rb +14 -5
- data/lib/ncmb/push.rb +2 -0
- data/lib/ncmb/query.rb +20 -18
- data/lib/ncmb/relation.rb +9 -7
- data/lib/ncmb/role.rb +3 -1
- data/lib/ncmb/script.rb +56 -0
- data/lib/ncmb/user.rb +3 -1
- data/lib/ncmb/version.rb +3 -1
- data/ncmb-ruby-client.gemspec +13 -13
- data/spec/delete_spec.rb +13 -9
- data/spec/get_spec.rb +9 -5
- data/spec/post_spec.rb +11 -6
- data/spec/put_spec.rb +19 -10
- data/spec/spec_helper.rb +6 -3
- metadata +10 -8
data/examples/relation.rb
CHANGED
@@ -1,18 +1,24 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
4
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
5
|
+
|
3
6
|
require 'rubygems'
|
4
7
|
require 'ncmb'
|
5
8
|
require 'yaml'
|
6
9
|
yaml = YAML.load_file(File.join(File.dirname(__FILE__), '..', 'setting.yml'))
|
7
|
-
NCMB.initialize
|
10
|
+
NCMB.initialize(
|
11
|
+
application_key: yaml['application_key'],
|
12
|
+
client_key: yaml['client_key']
|
13
|
+
)
|
8
14
|
|
9
15
|
Food = NCMB::DataStore.new 'Food'
|
10
16
|
|
11
17
|
Basket = NCMB::DataStore.new('Basket')
|
12
18
|
basket = Basket.new
|
13
19
|
basket.foods = []
|
14
|
-
basket.foods << Food.new(name:
|
15
|
-
basket.foods << Food.new(name:
|
20
|
+
basket.foods << Food.new(name: 'banana', type: 'fruit')
|
21
|
+
basket.foods << Food.new(name: 'pear', type: 'fruit')
|
16
22
|
|
17
23
|
# relation = NCMB::Relation.new
|
18
24
|
# relation << Food.new(name: "banana", type: "fruit", objectId: "test1")
|
data/examples/script.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
4
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
5
|
+
|
6
|
+
require 'rubygems'
|
7
|
+
require 'ncmb'
|
8
|
+
require 'yaml'
|
9
|
+
yaml = YAML.load_file(File.join(File.dirname(__FILE__), '..', 'setting.yml'))
|
10
|
+
NCMB.initialize(
|
11
|
+
application_key: yaml['application_key'],
|
12
|
+
client_key: yaml['client_key']
|
13
|
+
)
|
14
|
+
|
15
|
+
script = NCMB::Script.new 'helloworld.js'
|
16
|
+
results = script.get
|
17
|
+
puts results
|
18
|
+
|
19
|
+
script = NCMB::Script.new 'helloworld2.js'
|
20
|
+
results = script.get(query: {a: 'b'})
|
21
|
+
puts results
|
22
|
+
|
23
|
+
script = NCMB::Script.new 'email.js'
|
24
|
+
results = script.post(body: {email: 'atsushi@moongift.jp', option: 'Test', body: 'message'})
|
25
|
+
puts results
|
data/examples/signature.rb
CHANGED
@@ -1,11 +1,25 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
4
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
5
|
+
|
3
6
|
require 'rubygems'
|
4
7
|
require 'ncmb'
|
5
8
|
require 'yaml'
|
6
9
|
yaml = YAML.load_file(File.join(File.dirname(__FILE__), '..', 'setting.yml'))
|
7
|
-
@client = NCMB.initialize
|
10
|
+
@client = NCMB.initialize(
|
11
|
+
application_key: yaml['application_key'],
|
12
|
+
client_key: yaml['client_key']
|
13
|
+
)
|
8
14
|
|
9
15
|
# puts @client.application_key
|
10
|
-
puts @client.generate_signature
|
16
|
+
puts @client.generate_signature(:get,
|
17
|
+
'/2013-09-01/classes/TestClass',
|
18
|
+
'2013-12-02T02:44:35.452Z',
|
19
|
+
{
|
20
|
+
where: {
|
21
|
+
testKey: 'testValue'
|
22
|
+
}
|
23
|
+
}
|
24
|
+
)
|
11
25
|
|
data/examples/user_test.rb
CHANGED
@@ -1,10 +1,16 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
4
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
5
|
+
|
3
6
|
require 'rubygems'
|
4
7
|
require 'ncmb'
|
5
8
|
require 'yaml'
|
6
9
|
yaml = YAML.load_file(File.join(File.dirname(__FILE__), '..', 'setting.yml'))
|
7
|
-
NCMB.initialize
|
10
|
+
NCMB.initialize(
|
11
|
+
application_key: yaml['application_key'],
|
12
|
+
client_key: yaml['client_key']
|
13
|
+
)
|
8
14
|
|
9
15
|
if @user = NCMB::User.login('testUser2', 'testPassword2')
|
10
16
|
puts NCMB.CurrentUser.userName
|
@@ -20,9 +26,10 @@ end
|
|
20
26
|
@user.set('userName', 'testUser2')
|
21
27
|
@user.set('password', 'testPassword2')
|
22
28
|
if @user.signUp
|
23
|
-
puts
|
24
|
-
@user.set(
|
29
|
+
puts 'User create successful.'
|
30
|
+
@user.set('Hello', 'World')
|
25
31
|
@user.update
|
32
|
+
puts 'User update successful.'
|
26
33
|
else
|
27
34
|
puts "User create failed. #{@user.error.message}"
|
28
35
|
end
|
data/examples/venue_search.rb
CHANGED
@@ -1,36 +1,49 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
4
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
5
|
+
|
3
6
|
require 'rubygems'
|
4
7
|
require 'ncmb'
|
5
8
|
require 'yaml'
|
6
9
|
yaml = YAML.load_file(File.join(File.dirname(__FILE__), '..', 'setting.yml'))
|
7
|
-
|
10
|
+
NCMB.initialize(application_key: yaml['application_key'],
|
8
11
|
client_key: yaml['client_key']
|
9
12
|
)
|
10
13
|
json = JSON.parse(open(File.join(File.dirname(__FILE__), 'venues.json'), 'r').read)
|
11
|
-
venues_class =
|
14
|
+
venues_class = NCMB::DataStore.new 'Venues'
|
15
|
+
venues_class.delete_all
|
12
16
|
json['response']['venues'].each do |venue|
|
17
|
+
item = venues_class.new
|
13
18
|
params = {
|
14
19
|
name: venue['name'],
|
15
20
|
location: {
|
16
|
-
|
17
|
-
|
18
|
-
|
21
|
+
'__type' => 'GeoPoint',
|
22
|
+
'latitude' => venue['location']['lat'],
|
23
|
+
'longitude' => venue['location']['lng']
|
19
24
|
}
|
20
25
|
}
|
21
|
-
|
26
|
+
item.set('name', params[:name])
|
27
|
+
item.set('location', params[:location])
|
28
|
+
item.save
|
29
|
+
puts "#{item.objectId} saved."
|
22
30
|
end
|
23
31
|
params = {}
|
24
32
|
params[:where] = {
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
33
|
+
'location' => {
|
34
|
+
'$nearSphere' => {
|
35
|
+
'__type' => 'GeoPoint',
|
36
|
+
'longitude' => 139.745433,
|
37
|
+
'latitude' => 35.691152
|
30
38
|
},
|
31
|
-
|
39
|
+
'$maxDistanceInKilometers' => 10
|
32
40
|
}
|
33
41
|
}
|
34
|
-
|
35
|
-
|
36
|
-
|
42
|
+
venues_class = venues_class.where("location", params[:where]['location'])
|
43
|
+
results = venues_class.get
|
44
|
+
|
45
|
+
puts results.length
|
46
|
+
|
47
|
+
results.each do |result|
|
48
|
+
puts result.objectId
|
49
|
+
end
|
data/lib/ncmb.rb
CHANGED
@@ -1,26 +1,29 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
4
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
5
|
|
4
6
|
require 'time'
|
5
7
|
require 'openssl'
|
6
8
|
require 'base64'
|
7
|
-
require
|
8
|
-
require
|
9
|
-
require
|
10
|
-
require
|
9
|
+
require 'net/http'
|
10
|
+
require 'uri'
|
11
|
+
require 'erb'
|
12
|
+
require 'json'
|
11
13
|
require 'securerandom'
|
12
14
|
|
13
|
-
require
|
14
|
-
require
|
15
|
-
require
|
16
|
-
require
|
17
|
-
require
|
18
|
-
require
|
19
|
-
require
|
20
|
-
require
|
21
|
-
require
|
22
|
-
require
|
23
|
-
require
|
24
|
-
require
|
25
|
-
require
|
26
|
-
require
|
15
|
+
require 'ncmb/version'
|
16
|
+
require 'ncmb/client'
|
17
|
+
require 'ncmb/query'
|
18
|
+
require 'ncmb/data_store'
|
19
|
+
require 'ncmb/object'
|
20
|
+
require 'ncmb/file'
|
21
|
+
require 'ncmb/user'
|
22
|
+
require 'ncmb/push'
|
23
|
+
require 'ncmb/geo_point'
|
24
|
+
require 'ncmb/increment'
|
25
|
+
require 'ncmb/acl'
|
26
|
+
require 'ncmb/role'
|
27
|
+
require 'ncmb/relation'
|
28
|
+
require 'ncmb/script'
|
29
|
+
require 'ncmb/error'
|
data/lib/ncmb/acl.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module NCMB
|
2
4
|
class Acl
|
3
5
|
include NCMB
|
@@ -23,16 +25,22 @@ module NCMB
|
|
23
25
|
end
|
24
26
|
params.to_json
|
25
27
|
end
|
28
|
+
|
29
|
+
def fields
|
30
|
+
@fields
|
31
|
+
end
|
26
32
|
|
27
|
-
def public(read_or_write,
|
28
|
-
@fields['*'.to_sym][read_or_write.to_sym] =
|
33
|
+
def public(read_or_write, bol = true)
|
34
|
+
@fields['*'.to_sym][read_or_write.to_sym] = bol
|
29
35
|
end
|
30
36
|
|
37
|
+
# :reek:DuplicateMethodCall { max_calls: 2 }
|
31
38
|
def user(user, read_or_write, value = true)
|
32
39
|
@fields[user.objectId.to_sym] = {read: true, write: true} unless @fields[user.objectId.to_sym]
|
33
40
|
@fields[user.objectId.to_sym][read_or_write.to_sym] = value
|
34
41
|
end
|
35
42
|
|
43
|
+
# :reek:DuplicateMethodCall { max_calls: 2 }
|
36
44
|
def role(role, read_or_write, value = true)
|
37
45
|
@fields[role.name.to_sym] = {read: true, write: true} unless @fields[role.name.to_sym]
|
38
46
|
@fields[role.name.to_sym][read_or_write.to_sym] = value
|
data/lib/ncmb/client.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class Time
|
2
4
|
def to_json(a)
|
3
5
|
v = self.getgm
|
@@ -6,7 +8,9 @@ class Time
|
|
6
8
|
end
|
7
9
|
|
8
10
|
module NCMB
|
9
|
-
DOMAIN = '
|
11
|
+
DOMAIN = 'mbaas.api.nifcloud.com'
|
12
|
+
SCRIPT_DOMAIN = 'script.mbaas.api.nifcloud.com'
|
13
|
+
SCRIPT_API_VERSION = '2015-09-01'
|
10
14
|
API_VERSION = '2013-09-01'
|
11
15
|
@application_key = nil
|
12
16
|
@client_key = nil
|
@@ -16,28 +20,30 @@ module NCMB
|
|
16
20
|
|
17
21
|
class Client
|
18
22
|
include NCMB
|
19
|
-
attr_accessor :application_key, :client_key, :domain, :api_version
|
23
|
+
attr_accessor :application_key, :client_key, :domain, :api_version, :script_api_version
|
20
24
|
def initialize(params = {})
|
21
25
|
@domain = NCMB::DOMAIN
|
22
26
|
@api_version = NCMB::API_VERSION
|
27
|
+
@script_domain = NCMB::SCRIPT_DOMAIN
|
28
|
+
@script_api_version = NCMB::SCRIPT_API_VERSION
|
23
29
|
@application_key = params[:application_key]
|
24
30
|
@client_key = params[:client_key]
|
25
31
|
end
|
26
32
|
|
27
|
-
def get(path, params = {})
|
28
|
-
request :get, path, params
|
33
|
+
def get(path, params = {}, headers = {})
|
34
|
+
request :get, path, params, headers
|
29
35
|
end
|
30
36
|
|
31
|
-
def post(path, params = {})
|
32
|
-
request :post, path, params
|
37
|
+
def post(path, params = {}, headers = {})
|
38
|
+
request :post, path, params, headers
|
33
39
|
end
|
34
40
|
|
35
|
-
def put(path, params = {})
|
36
|
-
request :put, path, params
|
41
|
+
def put(path, params = {}, headers = {})
|
42
|
+
request :put, path, params, headers
|
37
43
|
end
|
38
44
|
|
39
|
-
def delete(path, params = {})
|
40
|
-
request :delete, path, params
|
45
|
+
def delete(path, params = {}, headers = {})
|
46
|
+
request :delete, path, params, headers
|
41
47
|
end
|
42
48
|
|
43
49
|
def array2hash(ary)
|
@@ -58,7 +64,7 @@ module NCMB
|
|
58
64
|
results = {}
|
59
65
|
queries.each do |k, v|
|
60
66
|
v = array2hash(v) if v.is_a? Array
|
61
|
-
value = URI.
|
67
|
+
value = URI.encode_www_form_component(v.is_a?(Hash) ? v.to_json : v.to_s).gsub("[", "%5B").gsub(":", "%3A").gsub("]", "%5D")
|
62
68
|
results[k.to_s] = value
|
63
69
|
end
|
64
70
|
results
|
@@ -88,7 +94,6 @@ module NCMB
|
|
88
94
|
results[k.to_s] = v.to_s
|
89
95
|
end
|
90
96
|
end
|
91
|
-
puts results
|
92
97
|
results
|
93
98
|
end
|
94
99
|
|
@@ -112,10 +117,10 @@ module NCMB
|
|
112
117
|
end
|
113
118
|
signature_base = []
|
114
119
|
signature_base << method.upcase
|
115
|
-
signature_base <<
|
120
|
+
signature_base << get_domain(path)
|
116
121
|
signature_base << path
|
117
122
|
signature_base << params.collect{|k,v| "#{k}=#{v}"}.join("&")
|
118
|
-
|
123
|
+
Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), @client_key, signature_base.join("\n"))).strip()
|
119
124
|
end
|
120
125
|
|
121
126
|
def make_boundary(boundary, queries)
|
@@ -123,7 +128,6 @@ module NCMB
|
|
123
128
|
post_body << "--#{boundary}"
|
124
129
|
post_body << "Content-Disposition: form-data; name=\"file\"; filename=\"#{queries[:fileName]}\""
|
125
130
|
post_body << "Content-Type: #{queries['mime-type'.to_sym]}"
|
126
|
-
post_body << ""
|
127
131
|
post_body << queries[:file].read
|
128
132
|
post_body << ""
|
129
133
|
post_body << "--#{boundary}"
|
@@ -134,10 +138,19 @@ module NCMB
|
|
134
138
|
post_body.join("\r\n")
|
135
139
|
end
|
136
140
|
|
137
|
-
def
|
141
|
+
def get_domain(path)
|
142
|
+
domain = @domain
|
143
|
+
if path.start_with?("/#{@script_api_version}/script\/")
|
144
|
+
domain = @script_domain
|
145
|
+
end
|
146
|
+
domain
|
147
|
+
end
|
148
|
+
|
149
|
+
def request(method, path, queries = {}, addHeaders = {})
|
138
150
|
now = Time.now.utc.iso8601
|
139
151
|
signature = generate_signature(method, path, now, queries)
|
140
|
-
|
152
|
+
domain = get_domain(path)
|
153
|
+
http = Net::HTTP.new(domain, 443)
|
141
154
|
http.use_ssl=true
|
142
155
|
headers = {
|
143
156
|
"X-NCMB-Application-Key" => @application_key,
|
@@ -145,8 +158,11 @@ module NCMB
|
|
145
158
|
"X-NCMB-Timestamp" => now,
|
146
159
|
"Content-Type" => 'application/json'
|
147
160
|
}
|
161
|
+
Array(addHeaders || {}).each do |name, value|
|
162
|
+
headers[name] = value
|
163
|
+
end
|
148
164
|
if NCMB.CurrentUser
|
149
|
-
headers['X-NCMB-Apps-Session-Token'] = NCMB.CurrentUser
|
165
|
+
headers['X-NCMB-Apps-Session-Token'] = NCMB.CurrentUser[:sessionToken]
|
150
166
|
end
|
151
167
|
# queries = hash2query(queries)
|
152
168
|
json = nil
|
@@ -165,7 +181,7 @@ module NCMB
|
|
165
181
|
end
|
166
182
|
when :post
|
167
183
|
req = Net::HTTP::Post.new(path)
|
168
|
-
if
|
184
|
+
if path.include? "/2013-09-01/files"
|
169
185
|
boundary = SecureRandom.uuid
|
170
186
|
req.body = make_boundary(boundary, queries)
|
171
187
|
headers["Content-Type"] = "multipart/form-data; boundary=#{boundary}"
|
data/lib/ncmb/data_store.rb
CHANGED
data/lib/ncmb/device.rb
CHANGED
data/lib/ncmb/error.rb
CHANGED
data/lib/ncmb/file.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'mime/types'
|
2
4
|
module NCMB
|
3
5
|
class NFile < NCMB::Object
|
@@ -12,7 +14,7 @@ module NCMB
|
|
12
14
|
end
|
13
15
|
|
14
16
|
def save
|
15
|
-
@fields[:file] =
|
17
|
+
@fields[:file] = ::OpenURI.open_uri(self.file)
|
16
18
|
super
|
17
19
|
end
|
18
20
|
alias :update :save
|