ncmb-ruby-client 0.1.0 → 0.1.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5070337bf5759d8e8612db60a27a99fbf846071c
4
- data.tar.gz: 8e3cc58ab2a7076b00a1ae606cf7d8916e0ff1e9
3
+ metadata.gz: 435261c7423b1f8fa49d777e11fc616934b12dc0
4
+ data.tar.gz: 3da4facc221ed5665cf74f04c9d36d9a78fc0ed3
5
5
  SHA512:
6
- metadata.gz: 2ef149bd509da1aa9453ca2a7f19a5a736a9857f9ed0880286f0276040ade20484bf19f1cb151466802bb6c9f5dd816fa437a0c122de6d53b93f80c4438c88e8
7
- data.tar.gz: c19ea27d7910c912e68fde7b9be91bbbaf53659a3a7f89e469026377ac888cc63fa326ac54619c8d07ba507dd0682e9be64ef707bc1d62e1533590c468686aa2
6
+ metadata.gz: 626fd1949667d4fa32bbf1821612da685b3f31b8128d980f86bd69aa97d43dc4eb2c110aaaf0576537acac4a1270baa0ec5456b8445aa46d41113eba630dd41b
7
+ data.tar.gz: 4aff5281bb913f54c5d1ffb5b9aa2e9649570ed88aa91fc2767882f34c158dca8fac680fda2d2917742d11b58bd63f7fd1b55d3472dafffddfa562f44c006936
data/Gemfile CHANGED
@@ -1,5 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
  gem 'simplecov'
3
3
  gem 'simplecov-gem-profile'
4
+ gem 'mime-types'
4
5
  # Specify your gem's dependencies in ncmb-ruby-client.gemspec
5
6
  gemspec
@@ -21,6 +21,7 @@ example.delete_all
21
21
  item.set('Increment', NCMB::Increment.new(i + 1))
22
22
  item.set('Date', Time.now)
23
23
  item.save
24
+ puts "#{item.objectId} saved."
24
25
  sleep(2)
25
26
  end
26
27
 
@@ -0,0 +1,20 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $:.unshift(File.dirname(__FILE__))
3
+ require 'rubygems'
4
+ require 'ncmb'
5
+ require 'yaml'
6
+ require 'open-uri'
7
+ yaml = YAML.load_file(File.join(File.dirname(__FILE__), '..', 'setting.yml'))
8
+ NCMB.initialize application_key: yaml['application_key'], client_key: yaml['client_key']
9
+
10
+ f = NCMB::NFile.new('http://mb.cloud.nifty.com/assets/images/logo.png')
11
+ f.acl.public('read', true)
12
+ f.acl.public('write', true)
13
+ f.fileName = "test.png"
14
+ f.save()
15
+ puts "Uploaded"
16
+ f.file = 'http://k.yimg.jp/images/top/sp2/cmn/logo-ns_d_131205.png'
17
+ f.update()
18
+ puts "Updated"
19
+ f.delete()
20
+ puts "Deleted"
data/lib/ncmb/client.rb CHANGED
@@ -118,6 +118,22 @@ module NCMB
118
118
  signature = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), @client_key, signature_base.join("\n"))).strip()
119
119
  end
120
120
 
121
+ def make_boundary(boundary, queries)
122
+ post_body = []
123
+ post_body << "--#{boundary}"
124
+ post_body << "Content-Disposition: form-data; name=\"file\"; filename=\"#{queries[:fileName]}\""
125
+ post_body << "Content-Type: #{queries['mime-type'.to_sym]}"
126
+ post_body << ""
127
+ post_body << queries[:file].read
128
+ post_body << ""
129
+ post_body << "--#{boundary}"
130
+ post_body << "Content-Disposition: form-data; name=\"acl\""
131
+ post_body << ""
132
+ post_body << queries[:acl].to_json
133
+ post_body << "--#{boundary}--"
134
+ post_body.join("\r\n")
135
+ end
136
+
121
137
  def request(method, path, queries = {})
122
138
  now = Time.now.utc.iso8601
123
139
  signature = generate_signature(method, path, now, queries)
@@ -134,6 +150,7 @@ module NCMB
134
150
  end
135
151
  # queries = hash2query(queries)
136
152
  json = nil
153
+ begin
137
154
  case method
138
155
  when :get
139
156
  query = encode_query(queries).map do |key, value|
@@ -142,16 +159,38 @@ module NCMB
142
159
  path = path + (query == '' ? "" : "?"+query)
143
160
  json = JSON.parse(http.get(path, headers).body, symbolize_names: true)
144
161
  when :post
145
- queries = change_query(queries)
146
- json = JSON.parse(http.post(path, queries.to_json, headers).body, symbolize_names: true)
162
+ req = Net::HTTP::Post.new(path)
163
+ if queries[:file].is_a?(File) || queries[:file].is_a?(StringIO)
164
+ boundary = SecureRandom.uuid
165
+ req.body = make_boundary(boundary, queries)
166
+ headers["Content-Type"] = "multipart/form-data; boundary=#{boundary}"
167
+ else
168
+ queries = change_query(queries)
169
+ req.body = queries.to_json
170
+ end
171
+ headers.each do |key, value|
172
+ req[key] = value
173
+ end
174
+ json = JSON.parse(http.request(req).body, symbolize_names: true)
147
175
  when :put
148
- json = JSON.parse(http.put(path, queries.to_json, headers).body, symbolize_names: true)
176
+ req = Net::HTTP::Put.new(path)
177
+ if queries[:file].is_a?(File) || queries[:file].is_a?(StringIO)
178
+ boundary = SecureRandom.uuid
179
+ req.body = make_boundary(boundary, queries)
180
+ headers["Content-Type"] = "multipart/form-data; boundary=#{boundary}"
181
+ else
182
+ queries = change_query(queries)
183
+ req.body = queries.to_json
184
+ end
185
+ headers.each do |key, value|
186
+ req[key] = value
187
+ end
188
+ json = JSON.parse(http.request(req).body, symbolize_names: true)
149
189
  when :delete
150
190
  response = http.delete(path, headers).body
151
191
  return true if response == ""
152
192
  json = JSON.parse(response, symbolize_names: true)
153
193
  end
154
- begin
155
194
  rescue => e
156
195
  @@last_error = e
157
196
  raise NCMB::APIError.new(e.to_s)
@@ -2,11 +2,15 @@ module NCMB
2
2
  class DataStore
3
3
  include NCMB
4
4
 
5
+ include NCMB::Query
6
+
5
7
  def initialize(name, fields = {}, alc = "")
6
8
  @name = name
7
9
  @alc = alc
8
10
  @fields = fields
9
- @queries = {where: []}
11
+ @search_key = :where
12
+ @queries = {}
13
+ @queries[@search_key] = []
10
14
  @items = nil
11
15
  @path = nil
12
16
  end
@@ -64,71 +68,6 @@ module NCMB
64
68
  self
65
69
  end
66
70
 
67
- [
68
- {greaterThan: "$gt"},
69
- {notEqualTo: "$ne"},
70
- {equalTo: nil},
71
- {lessThan: "$lt"},
72
- {lessThanOrEqualTo: "$lte"},
73
- {greaterThanOrEqualTo: "$gte"},
74
- {in: "$in"},
75
- {notIn: "$nin"},
76
- {exists: "$exists"},
77
- {regex: "$regex"},
78
- {inArray: "$inArray"},
79
- {notInArray: "$ninArray"},
80
- {allInArray: "$all"},
81
- ].each do |m|
82
- define_method m.keys.first do |name, value|
83
- params = {}
84
- if m.values.first.nil?
85
- params[name] = value
86
- else
87
- params[name] = {}
88
- params[name][m.values.first] = value
89
- end
90
- @queries[:where] << params
91
- self
92
- end
93
- end
94
-
95
- [
96
- {withinKilometers: "$maxDistanceInKilometers"},
97
- {withinMiles: "$maxDistanceInMiles"},
98
- {withinRadians: "$maxDistanceInRadians"}
99
- ].each do |m|
100
- define_method m.keys.first do |name, geo, value|
101
- params = {}
102
- params[name] = {
103
- "$nearSphere": geo,
104
- }
105
- params[name][m.values.first] = value
106
- @queries[:where] << params
107
- self
108
- end
109
- end
110
-
111
- def withinSquare(name, geo1, geo2)
112
- params = {}
113
- params[name] = {
114
- "$within": {
115
- "$box": [
116
- geo1,
117
- geo2
118
- ]
119
- }
120
- }
121
- @queries[:where] << params
122
- self
123
- end
124
-
125
- def where(name, value)
126
- params = {}
127
- params[name] = value
128
- @queries[:where] << params
129
- self
130
- end
131
-
132
71
  def [](count)
133
72
  get[count]
134
73
  end
data/lib/ncmb/file.rb ADDED
@@ -0,0 +1,27 @@
1
+ require 'mime/types'
2
+ module NCMB
3
+ class NFile < NCMB::Object
4
+ include NCMB
5
+ def initialize(file_path = nil)
6
+ @fields = {acl: NCMB::Acl.new, file: file_path}
7
+ if file_path
8
+ @fields[:fileName] = File.basename(file_path)
9
+ @fields['mime-type'.to_sym] = MIME::Types.type_for(file_path)[0]
10
+ end
11
+ end
12
+
13
+ def save
14
+ @fields[:file] = open(self.file)
15
+ super
16
+ end
17
+ alias :update :save
18
+
19
+ def path
20
+ "#{base_path}/#{@fields[:fileName]}"
21
+ end
22
+
23
+ def base_path
24
+ "/#{@@client.api_version}/files"
25
+ end
26
+ end
27
+ end
data/lib/ncmb/object.rb CHANGED
@@ -64,11 +64,12 @@ module NCMB
64
64
  alias :save :post
65
65
 
66
66
  def put
67
+ put_path = path
67
68
  params = @fields
68
69
  params.delete :objectId
69
70
  params.delete :createDate
70
71
  params.delete :updateDate
71
- result = @@client.put path, params
72
+ result = @@client.put put_path, params
72
73
  @fields[:updateDate] = result[:updateDate]
73
74
  self
74
75
  end
data/lib/ncmb/push.rb CHANGED
@@ -1,26 +1,22 @@
1
1
  module NCMB
2
- class Push
2
+ class Push < NCMB::Object
3
3
  include NCMB
4
- attr_accessor :deliveryTime, :immediateDeliveryFlag, :target, :searchCondition, :message,
5
- :userSettingValue, :deliveryExpirationDate, :deliveryExpirationTime, :action, :title, :dialog,
6
- :badgeIncrementFlag, :badgeSetting, :sound, :contentAvailable, :richUrl, :acl, :objectId, :createDate, :errors
7
4
 
8
- def save
9
- path = "/#{@@client.api_version}/push"
10
- queries = {}
5
+ def initialize(params = {})
11
6
  [:deliveryTime, :immediateDeliveryFlag, :target, :searchCondition, :message,
12
- :userSettingValue, :deliveryExpirationDate, :deliveryExpirationTime, :action, :title, :dialog,
13
- :badgeIncrementFlag, :badgeSetting, :sound, :contentAvailable, :richUrl, :acl].each do |name|
14
- queries[name] = send(name) unless send(name).nil?
7
+ :userSettingValue, :deliveryExpirationDate, :deliveryExpirationTime, :action, :title, :dialog,
8
+ :badgeIncrementFlag, :badgeSetting, :sound, :contentAvailable, :richUrl].each do |name|
9
+ params[name] = nil unless params[name]
15
10
  end
16
- results = @@client.post path, queries
17
- if results[:objectId].nil?
18
- self.errors = results
19
- return false
20
- end
21
- self.objectId = results[:objectId]
22
- self.createDate = results[:createDate]
23
- return true
11
+ @search_key = :search_condition
12
+ @queries = {}
13
+ @queries[@search_key] = []
14
+ super('push', params)
15
+ end
16
+
17
+ def base_path
18
+ "/#{@@client.api_version}/#{@name}"
24
19
  end
20
+
25
21
  end
26
22
  end
data/lib/ncmb/query.rb ADDED
@@ -0,0 +1,68 @@
1
+ module NCMB
2
+ module Query
3
+ [
4
+ {greaterThan: "$gt"},
5
+ {notEqualTo: "$ne"},
6
+ {equalTo: nil},
7
+ {lessThan: "$lt"},
8
+ {lessThanOrEqualTo: "$lte"},
9
+ {greaterThanOrEqualTo: "$gte"},
10
+ {in: "$in"},
11
+ {notIn: "$nin"},
12
+ {exists: "$exists"},
13
+ {regex: "$regex"},
14
+ {inArray: "$inArray"},
15
+ {notInArray: "$ninArray"},
16
+ {allInArray: "$all"},
17
+ ].each do |m|
18
+ define_method m.keys.first do |name, value|
19
+ params = {}
20
+ if m.values.first.nil?
21
+ params[name] = value
22
+ else
23
+ params[name] = {}
24
+ params[name][m.values.first] = value
25
+ end
26
+ @queries[@search_key] << params
27
+ self
28
+ end
29
+ end
30
+
31
+ [
32
+ {withinKilometers: "$maxDistanceInKilometers"},
33
+ {withinMiles: "$maxDistanceInMiles"},
34
+ {withinRadians: "$maxDistanceInRadians"}
35
+ ].each do |m|
36
+ define_method m.keys.first do |name, geo, value|
37
+ params = {}
38
+ params[name] = {
39
+ "$nearSphere": geo,
40
+ }
41
+ params[name][m.values.first] = value
42
+ @queries[@search_key] << params
43
+ self
44
+ end
45
+ end
46
+
47
+ def withinSquare(name, geo1, geo2)
48
+ params = {}
49
+ params[name] = {
50
+ "$within": {
51
+ "$box": [
52
+ geo1,
53
+ geo2
54
+ ]
55
+ }
56
+ }
57
+ @queries[@search_key] << params
58
+ self
59
+ end
60
+
61
+ def where(name, value)
62
+ params = {}
63
+ params[name] = value
64
+ @queries[@search_key] << params
65
+ self
66
+ end
67
+ end
68
+ end
data/lib/ncmb/role.rb CHANGED
@@ -10,6 +10,7 @@ module NCMB
10
10
  roleName: name
11
11
  }
12
12
  end
13
+ @fields[:acl] = NCMB::Acl.new(@fields[:acl])
13
14
  end
14
15
 
15
16
  def self.find_or_create(name)
@@ -23,13 +24,8 @@ module NCMB
23
24
  "role:#{@fields[:roleName]}"
24
25
  end
25
26
 
26
- def to_json
27
-
28
- end
29
-
30
27
  def base_path
31
28
  path = "/#{@@client.api_version}/roles"
32
29
  end
33
-
34
30
  end
35
31
  end
data/lib/ncmb/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ncmb
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/ncmb.rb CHANGED
@@ -8,11 +8,14 @@ require "net/http"
8
8
  require "uri"
9
9
  require "erb"
10
10
  require "json"
11
+ require 'securerandom'
11
12
 
12
13
  require "ncmb/version"
13
14
  require "ncmb/client"
15
+ require "ncmb/query"
14
16
  require "ncmb/data_store"
15
17
  require "ncmb/object"
18
+ require "ncmb/file"
16
19
  require "ncmb/user"
17
20
  require "ncmb/push"
18
21
  require "ncmb/geo_point"
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.1.0
4
+ version: 0.1.1
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-10 00:00:00.000000000 Z
11
+ date: 2016-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -67,6 +67,7 @@ files:
67
67
  - examples/acl_test.rb
68
68
  - examples/csv_test.rb
69
69
  - examples/data_store.rb
70
+ - examples/file_test.rb
70
71
  - examples/push.rb
71
72
  - examples/signature.rb
72
73
  - examples/user_test.rb
@@ -78,10 +79,12 @@ files:
78
79
  - lib/ncmb/data_store.rb
79
80
  - lib/ncmb/device.rb
80
81
  - lib/ncmb/error.rb
82
+ - lib/ncmb/file.rb
81
83
  - lib/ncmb/geo_point.rb
82
84
  - lib/ncmb/increment.rb
83
85
  - lib/ncmb/object.rb
84
86
  - lib/ncmb/push.rb
87
+ - lib/ncmb/query.rb
85
88
  - lib/ncmb/role.rb
86
89
  - lib/ncmb/user.rb
87
90
  - lib/ncmb/version.rb