ncmb-ruby-client 0.0.8 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +2 -1
- data/Rakefile +1 -1
- data/examples/user_test.rb +32 -0
- data/lib/ncmb.rb +1 -0
- data/lib/ncmb/client.rb +63 -25
- data/lib/ncmb/data_store.rb +9 -3
- data/lib/ncmb/object.rb +20 -6
- data/lib/ncmb/user.rb +67 -0
- data/lib/ncmb/version.rb +1 -1
- data/spec/delete_spec.rb +9 -9
- data/spec/get_spec.rb +8 -31
- data/spec/post_spec.rb +6 -8
- data/spec/spec_helper.rb +5 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a99591b3222464ef82634322a71414dfc6663465
|
4
|
+
data.tar.gz: 995375ed8c9a7ef1d3fce4cb4ae2304bfd0b9cea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d59a07ef9b8c75191ece14db9bfc5930017f4e706af983b56b245a4b9486ce9d22bb96147680ce076ec027499570aeaac338df71289e91dce0d49e2768149c0f
|
7
|
+
data.tar.gz: 7d66392148b933ebdeb52061a49122001d744c3fb98aae5d77d52bad2117bf8bb7156fac2a9bbcdee3130b90ecf703261f77c654a14ad7fc07b42b4887a929a1
|
data/Gemfile
CHANGED
data/Rakefile
CHANGED
@@ -0,0 +1,32 @@
|
|
1
|
+
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$:.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rubygems'
|
4
|
+
require 'ncmb'
|
5
|
+
require 'yaml'
|
6
|
+
yaml = YAML.load_file(File.join(File.dirname(__FILE__), '..', 'setting.yml'))
|
7
|
+
NCMB.initialize application_key: yaml['application_key'], client_key: yaml['client_key']
|
8
|
+
|
9
|
+
if @user = NCMB::User.login('testUser2', 'testPassword2')
|
10
|
+
puts NCMB.CurrentUser.userName
|
11
|
+
NCMB.CurrentUser.delete
|
12
|
+
begin
|
13
|
+
@user.delete
|
14
|
+
rescue => e
|
15
|
+
puts e.message
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
@user = NCMB::User.new
|
20
|
+
@user.set('userName', 'testUser2')
|
21
|
+
@user.set('password', 'testPassword2')
|
22
|
+
if @user.signUp
|
23
|
+
puts "User create successful."
|
24
|
+
@user.set("Hello", "World")
|
25
|
+
@user.update
|
26
|
+
else
|
27
|
+
puts "User create failed. #{@user.error.message}"
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
|
32
|
+
|
data/lib/ncmb.rb
CHANGED
data/lib/ncmb/client.rb
CHANGED
@@ -11,6 +11,8 @@ module NCMB
|
|
11
11
|
@application_key = nil
|
12
12
|
@client_key = nil
|
13
13
|
@@client = nil
|
14
|
+
@@current_user = nil
|
15
|
+
@@last_error = nil
|
14
16
|
|
15
17
|
class Client
|
16
18
|
include NCMB
|
@@ -22,20 +24,20 @@ module NCMB
|
|
22
24
|
@client_key = params[:client_key]
|
23
25
|
end
|
24
26
|
|
25
|
-
def get(path, params)
|
26
|
-
request :get, path, params
|
27
|
+
def get(path, params = {}, session_key = nil)
|
28
|
+
request :get, path, params, session_key
|
27
29
|
end
|
28
30
|
|
29
|
-
def post(path, params)
|
30
|
-
request :post, path, params
|
31
|
+
def post(path, params = {}, session_key = nil)
|
32
|
+
request :post, path, params, session_key
|
31
33
|
end
|
32
34
|
|
33
|
-
def put(path, params)
|
34
|
-
request :put, path, params
|
35
|
+
def put(path, params = {}, session_key = nil)
|
36
|
+
request :put, path, params, session_key
|
35
37
|
end
|
36
38
|
|
37
|
-
def delete(path, params)
|
38
|
-
request :delete, path
|
39
|
+
def delete(path, params = {}, session_key = nil)
|
40
|
+
request :delete, path, params, session_key
|
39
41
|
end
|
40
42
|
|
41
43
|
def array2hash(ary)
|
@@ -99,7 +101,15 @@ module NCMB
|
|
99
101
|
params = method == :get ? params_base.merge(encode_query(queries)) : params_base
|
100
102
|
now ||= Time.now.utc.iso8601
|
101
103
|
params = params.merge "X-NCMB-Timestamp" => now
|
102
|
-
|
104
|
+
if [].respond_to?("to_h") # Array#to_h inpremented over ruby 2.1
|
105
|
+
params = params.sort_by{|a, b| a.to_s}.to_h
|
106
|
+
else
|
107
|
+
sorted_params = {}
|
108
|
+
params = params.sort_by{|a, b| a.to_s}.each {|kv|
|
109
|
+
sorted_params[kv[0]] = kv[1]
|
110
|
+
}
|
111
|
+
params = sorted_params
|
112
|
+
end
|
103
113
|
signature_base = []
|
104
114
|
signature_base << method.upcase
|
105
115
|
signature_base << @domain
|
@@ -108,7 +118,7 @@ module NCMB
|
|
108
118
|
signature = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), @client_key, signature_base.join("\n"))).strip()
|
109
119
|
end
|
110
120
|
|
111
|
-
def request(method, path, queries = {})
|
121
|
+
def request(method, path, queries = {}, session_key = nil)
|
112
122
|
now = Time.now.utc.iso8601
|
113
123
|
signature = generate_signature(method, path, now, queries)
|
114
124
|
http = Net::HTTP.new(@domain, 443)
|
@@ -119,23 +129,37 @@ module NCMB
|
|
119
129
|
"X-NCMB-Timestamp" => now,
|
120
130
|
"Content-Type" => 'application/json'
|
121
131
|
}
|
132
|
+
if session_key
|
133
|
+
headers['X-NCMB-Apps-Session-Token'] = session_key
|
134
|
+
end
|
122
135
|
# queries = hash2query(queries)
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
136
|
+
json = nil
|
137
|
+
begin
|
138
|
+
case method
|
139
|
+
when :get
|
140
|
+
query = encode_query(queries).map do |key, value|
|
141
|
+
"#{key}=#{value}"
|
142
|
+
end.join("&")
|
143
|
+
path = path + (query == '' ? "" : "?"+query)
|
144
|
+
json = JSON.parse(http.get(path, headers).body, symbolize_names: true)
|
145
|
+
when :post
|
146
|
+
queries = change_query(queries)
|
147
|
+
json = JSON.parse(http.post(path, queries.to_json, headers).body, symbolize_names: true)
|
148
|
+
when :put
|
149
|
+
json = JSON.parse(http.put(path, queries.to_json, headers).body, symbolize_names: true)
|
150
|
+
when :delete
|
151
|
+
response = http.delete(path, headers).body
|
152
|
+
return true if response == ""
|
153
|
+
json = JSON.parse(response, symbolize_names: true)
|
154
|
+
end
|
155
|
+
rescue => e
|
156
|
+
@@last_error = e
|
157
|
+
raise NCMB::APIError.new(e.to_s)
|
138
158
|
end
|
159
|
+
if json[:error] != nil
|
160
|
+
raise NCMB::APIError.new(json[:error])
|
161
|
+
end
|
162
|
+
json
|
139
163
|
end
|
140
164
|
end
|
141
165
|
|
@@ -147,4 +171,18 @@ module NCMB
|
|
147
171
|
defaulted.merge!(params)
|
148
172
|
@@client = Client.new(defaulted)
|
149
173
|
end
|
174
|
+
|
175
|
+
def NCMB.CurrentUser
|
176
|
+
@@current_user
|
177
|
+
end
|
178
|
+
|
179
|
+
class APIError < StandardError
|
180
|
+
def initialize(msg)
|
181
|
+
@message = msg
|
182
|
+
end
|
183
|
+
|
184
|
+
def message
|
185
|
+
@message
|
186
|
+
end
|
187
|
+
end
|
150
188
|
end
|
data/lib/ncmb/data_store.rb
CHANGED
@@ -159,15 +159,21 @@ module NCMB
|
|
159
159
|
end
|
160
160
|
@items
|
161
161
|
end
|
162
|
+
alias :all :get
|
163
|
+
|
164
|
+
def queries
|
165
|
+
@queries
|
166
|
+
end
|
162
167
|
|
163
168
|
def delete_all
|
164
169
|
max = 1000
|
165
|
-
|
170
|
+
dataStore = NCMB::DataStore.new(@name)
|
171
|
+
count = dataStore.limit(max).count().all
|
166
172
|
if count == 0
|
167
173
|
return true
|
168
174
|
end
|
169
|
-
|
170
|
-
|
175
|
+
dataStore.queries.delete :count
|
176
|
+
dataStore.each do |item|
|
171
177
|
item.delete
|
172
178
|
end
|
173
179
|
if count > max
|
data/lib/ncmb/object.rb
CHANGED
@@ -7,7 +7,11 @@ module NCMB
|
|
7
7
|
@alc = alc
|
8
8
|
@fields = fields
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
|
+
def fields
|
12
|
+
@fields
|
13
|
+
end
|
14
|
+
|
11
15
|
def method_missing(name)
|
12
16
|
sym = name.to_sym
|
13
17
|
if @fields.has_key?(sym)
|
@@ -18,7 +22,7 @@ module NCMB
|
|
18
22
|
end
|
19
23
|
|
20
24
|
def set(name, value)
|
21
|
-
@fields[name] = value
|
25
|
+
@fields[name.to_sym] = value
|
22
26
|
end
|
23
27
|
|
24
28
|
def call(name)
|
@@ -32,9 +36,8 @@ module NCMB
|
|
32
36
|
def post
|
33
37
|
path = "/#{@@client.api_version}/classes/#{@name}"
|
34
38
|
result = @@client.post path, @fields
|
35
|
-
|
36
|
-
|
37
|
-
NCMB::Object.new(@name, result, alc)
|
39
|
+
@fields.merge!(result)
|
40
|
+
self
|
38
41
|
end
|
39
42
|
alias :save :post
|
40
43
|
|
@@ -46,12 +49,23 @@ module NCMB
|
|
46
49
|
params.delete :updateDate
|
47
50
|
result = @@client.put path, params
|
48
51
|
@fields[:updateDate] = result[:updateDate]
|
52
|
+
self
|
49
53
|
end
|
50
54
|
alias :update :put
|
51
55
|
|
52
56
|
def delete
|
53
57
|
path = "/#{@@client.api_version}/classes/#{@name}/#{@fields[:objectId]}"
|
54
|
-
@@client.delete path, {}
|
58
|
+
response = @@client.delete path, {}
|
59
|
+
if response == true
|
60
|
+
return true
|
61
|
+
else
|
62
|
+
@@last_error = response
|
63
|
+
return false
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def error
|
68
|
+
@@last_error
|
55
69
|
end
|
56
70
|
end
|
57
71
|
end
|
data/lib/ncmb/user.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
module NCMB
|
2
|
+
class User < NCMB::Object
|
3
|
+
include NCMB
|
4
|
+
|
5
|
+
def initialize(params = {}, alc = nil)
|
6
|
+
super('users', params, alc)
|
7
|
+
end
|
8
|
+
|
9
|
+
def signUp
|
10
|
+
path = "/#{@@client.api_version}/#{@name}"
|
11
|
+
begin
|
12
|
+
result = @@client.post path, @fields
|
13
|
+
rescue => e
|
14
|
+
@@last_error = e
|
15
|
+
return false
|
16
|
+
end
|
17
|
+
@fields.merge!(result)
|
18
|
+
@@current_user = self
|
19
|
+
self
|
20
|
+
end
|
21
|
+
|
22
|
+
def put
|
23
|
+
path = "/#{@@client.api_version}/#{@name}/#{@fields[:objectId]}"
|
24
|
+
params = @fields
|
25
|
+
session_key = params[:sessionToken]
|
26
|
+
[:objectId, :createDate, :updateDate, :sessionToken, :password].each do |name|
|
27
|
+
params.delete name
|
28
|
+
end
|
29
|
+
result = @@client.put path, params, session_key
|
30
|
+
@fields[:updateDate] = result[:updateDate]
|
31
|
+
self
|
32
|
+
end
|
33
|
+
alias :update :put
|
34
|
+
|
35
|
+
def delete
|
36
|
+
path = "/#{@@client.api_version}/#{@name}/#{@fields[:objectId]}"
|
37
|
+
response = @@client.delete path, {}, @fields[:sessionToken]
|
38
|
+
if response == true
|
39
|
+
@@current_user = nil
|
40
|
+
return true
|
41
|
+
else
|
42
|
+
@@last_error = response
|
43
|
+
return false
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.login(userid_or_email, password, authType = :id)
|
48
|
+
params = {password: password}
|
49
|
+
case authType.to_sym
|
50
|
+
when :id
|
51
|
+
params[:userName] = userid_or_email
|
52
|
+
when :email
|
53
|
+
params[:mailAddres] = userid_or_email
|
54
|
+
else
|
55
|
+
raise NCMB::APIError.new("No support #{authType} authentication. We support only id or email.")
|
56
|
+
end
|
57
|
+
begin
|
58
|
+
path = "/#{@@client.api_version}/login"
|
59
|
+
result = @@client.get path, params
|
60
|
+
rescue => e
|
61
|
+
@@last_error = e
|
62
|
+
return false
|
63
|
+
end
|
64
|
+
@@current_user = NCMB::User.new(result)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
data/lib/ncmb/version.rb
CHANGED
data/spec/delete_spec.rb
CHANGED
@@ -2,19 +2,19 @@ require "spec_helper"
|
|
2
2
|
describe NCMB do
|
3
3
|
before do
|
4
4
|
yaml = YAML.load_file(File.join(File.dirname(__FILE__), '..', 'setting.yml'))
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
@object_id = @
|
5
|
+
NCMB.initialize application_key: yaml['application_key'], client_key: yaml['client_key']
|
6
|
+
@Todo = NCMB::DataStore.new('TODO')
|
7
|
+
@todo = @Todo.new(text: "Test task")
|
8
|
+
@object_id = @todo.save().objectId
|
9
9
|
end
|
10
10
|
|
11
11
|
it "Delete #1" do
|
12
|
-
|
13
|
-
res.should == {}
|
12
|
+
@todo.delete.should == true
|
14
13
|
end
|
15
14
|
|
16
|
-
it "Delete
|
17
|
-
|
18
|
-
|
15
|
+
it "Delete again" do
|
16
|
+
@todo.delete.should == true
|
17
|
+
@todo.delete.should == false
|
18
|
+
@todo.error[:code].should == 'E404001'
|
19
19
|
end
|
20
20
|
end
|
data/spec/get_spec.rb
CHANGED
@@ -3,40 +3,17 @@ require "spec_helper"
|
|
3
3
|
describe NCMB do
|
4
4
|
before do
|
5
5
|
yaml = YAML.load_file(File.join(File.dirname(__FILE__), '..', 'setting.yml'))
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
NCMB.initialize application_key: yaml['application_key'], client_key: yaml['client_key']
|
7
|
+
@todoClass = todoClass = NCMB::DataStore.new 'GET_TODO'
|
8
|
+
@todoClass.delete_all
|
9
|
+
10.times do |i|
|
10
|
+
@todoClass.new(text: "Task ##{i + 1}").save
|
11
|
+
end
|
9
12
|
end
|
10
13
|
|
11
14
|
it "Get #1" do
|
12
|
-
|
13
|
-
|
14
|
-
# {"count":5,"results":[{"objectId":"VwswoCe7PEuSbGEU","createDate":"2014-05-07T12:03:37.428Z","updateDate":"2014-05-07T12:03:37.428Z","acl":{"*":{"read":true,"write":true}},"todo":"タスクの追加"},{"objectId":"pj9scMgCGQ3wyd5m","createDate":"2014-05-07T08:03:56.268Z","updateDate":"2014-05-07T08:03:56.268Z","acl":{"*":{"read":true,"write":true}},"todo":"タスクを追加"},{"objectId":"lq3CIoaSAcM5Du6I","createDate":"2014-05-07T08:03:44.181Z","updateDate":"2014-05-07T08:03:44.181Z","acl":{"*":{"read":true,"write":true}},"todo":"テストのタスク"},{"objectId":"jln8aWojgUDUnCYo","createDate":"2014-05-07T08:02:23.104Z","updateDate":"2014-05-07T08:02:23.104Z","acl":{"*":{"read":true,"write":true}},"todo":"レビューします"},{"objectId":"zhRGEjECBdaUBLLn","createDate":"2014-05-07T08:02:10.573Z","updateDate":"2014-05-07T08:02:10.573Z","acl":{"*":{"read":true,"write":true}},"todo":"記事を書きます"}]}
|
15
|
-
#puts todo_class.get queries
|
15
|
+
@items = @todoClass.order('-createDate').skip(0).all
|
16
|
+
@items.length.should == 10
|
16
17
|
end
|
17
18
|
|
18
|
-
it "Generate signature #3" do
|
19
|
-
params = {
|
20
|
-
:where => {
|
21
|
-
"point" => {
|
22
|
-
"$within" => {
|
23
|
-
"$box" => [
|
24
|
-
{
|
25
|
-
"__type" => "GeoPoint",
|
26
|
-
"latitude" => 35.690921,
|
27
|
-
"longitude" => 139.700258
|
28
|
-
},
|
29
|
-
{
|
30
|
-
"__type" => "GeoPoint",
|
31
|
-
"latitude" => 35.728926,
|
32
|
-
"longitude" => 139.71038
|
33
|
-
}
|
34
|
-
]
|
35
|
-
}
|
36
|
-
}
|
37
|
-
}
|
38
|
-
}
|
39
|
-
signature = @ncmb.generate_signature :get, "/#{@ncmb.api_version}/classes/Venue", URI.escape("2014-05-20T04:55:16.395Z", /[^-_.!~*'()a-zA-Z\d]/u), params
|
40
|
-
expect(signature).to eq("sqlhM3xxNPUxFWDHQ5CdDqBp6dInU/YkO2PzuY31Pbk=")
|
41
|
-
end
|
42
19
|
end
|
data/spec/post_spec.rb
CHANGED
@@ -2,17 +2,15 @@ require "spec_helper"
|
|
2
2
|
describe NCMB do
|
3
3
|
before do
|
4
4
|
yaml = YAML.load_file(File.join(File.dirname(__FILE__), '..', 'setting.yml'))
|
5
|
-
|
6
|
-
client_key: yaml['client_key']
|
7
|
-
)
|
5
|
+
NCMB.initialize application_key: yaml['application_key'], client_key: yaml['client_key']
|
8
6
|
end
|
9
7
|
|
10
8
|
it "Post #1" do
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
9
|
+
text = "Test task"
|
10
|
+
queries = {todo: text}
|
11
|
+
todo_class = NCMB::DataStore.new 'POST_TODO'
|
12
|
+
todo = todo_class.new(queries).save
|
13
|
+
todo.todo.should == text
|
16
14
|
end
|
17
15
|
|
18
16
|
it "Post with location #1" do
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ncmb-ruby-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Atsushi Nakatsugawa
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-09-
|
11
|
+
date: 2016-09-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -68,6 +68,7 @@ files:
|
|
68
68
|
- examples/data_store.rb
|
69
69
|
- examples/push.rb
|
70
70
|
- examples/signature.rb
|
71
|
+
- examples/user_test.rb
|
71
72
|
- examples/venue_search.rb
|
72
73
|
- examples/venues.json
|
73
74
|
- lib/ncmb.rb
|
@@ -79,6 +80,7 @@ files:
|
|
79
80
|
- lib/ncmb/increment.rb
|
80
81
|
- lib/ncmb/object.rb
|
81
82
|
- lib/ncmb/push.rb
|
83
|
+
- lib/ncmb/user.rb
|
82
84
|
- lib/ncmb/version.rb
|
83
85
|
- ncmb-ruby-client.gemspec
|
84
86
|
- setting_default.yml
|