ncmb-ruby-client 0.1.2 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,34 +1,37 @@
1
- $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
- $:.unshift(File.dirname(__FILE__))
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 = NCMB.init(application_key: yaml['application_key'],
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 = @ncmb.data_store 'Venues'
14
+ venues_class = NCMB::DataStore.new 'Venues'
12
15
  json['response']['venues'].each do |venue|
13
16
  params = {
14
17
  name: venue['name'],
15
18
  location: {
16
- "__type" => "GeoPoint",
17
- "latitude" => venue['location']['lat'],
18
- "longitude" => venue['location']['lng']
19
+ '__type' => 'GeoPoint',
20
+ 'latitude' => venue['location']['lat'],
21
+ 'longitude' => venue['location']['lng']
19
22
  }
20
23
  }
21
24
  puts venues_class.post(params).body
22
25
  end
23
26
  params = {}
24
27
  params[:where] = {
25
- "location" => {
26
- "$nearSphere" => {
27
- "__type" => "GeoPoint",
28
- "longitude" => 139.745433,
29
- "latitude" => 35.691152
28
+ 'location' => {
29
+ '$nearSphere' => {
30
+ '__type' => 'GeoPoint',
31
+ 'longitude' => 139.745433,
32
+ 'latitude' => 35.691152
30
33
  },
31
- "$maxDistanceInKilometers" => 10
34
+ '$maxDistanceInKilometers' => 10
32
35
  }
33
36
  }
34
37
  #
data/lib/ncmb.rb CHANGED
@@ -1,26 +1,29 @@
1
- $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
- $:.unshift(File.dirname(__FILE__))
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 "net/http"
8
- require "uri"
9
- require "erb"
10
- require "json"
9
+ require 'net/http'
10
+ require 'uri'
11
+ require 'erb'
12
+ require 'json'
11
13
  require 'securerandom'
12
14
 
13
- require "ncmb/version"
14
- require "ncmb/client"
15
- require "ncmb/query"
16
- require "ncmb/data_store"
17
- require "ncmb/object"
18
- require "ncmb/file"
19
- require "ncmb/user"
20
- require "ncmb/push"
21
- require "ncmb/geo_point"
22
- require "ncmb/increment"
23
- require "ncmb/acl"
24
- require "ncmb/role"
25
- require "ncmb/relation"
26
- require "ncmb/error"
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, value = true)
28
- @fields['*'.to_sym][read_or_write.to_sym] = value
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 = 'mb.api.cloud.nifty.com'
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.encode(v.is_a?(Hash) ? v.to_json : v.to_s).gsub("[", "%5B").gsub(":", "%3A").gsub("]", "%5D")
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 << @domain
120
+ signature_base << get_domain(path)
116
121
  signature_base << path
117
122
  signature_base << params.collect{|k,v| "#{k}=#{v}"}.join("&")
118
- signature = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), @client_key, signature_base.join("\n"))).strip()
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 request(method, path, queries = {})
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
- http = Net::HTTP.new(@domain, 443)
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.sessionToken
165
+ headers['X-NCMB-Apps-Session-Token'] = NCMB.CurrentUser[:sessionToken]
150
166
  end
151
167
  # queries = hash2query(queries)
152
168
  json = nil
@@ -157,10 +173,15 @@ module NCMB
157
173
  "#{key}=#{value}"
158
174
  end.join("&")
159
175
  path = path + (query == '' ? "" : "?"+query)
160
- json = JSON.parse(http.get(path, headers).body, symbolize_names: true)
176
+ rp = Regexp.new "/#{NCMB::API_VERSION}/files/.*"
177
+ if path =~ rp
178
+ json = http.get(path, headers).body
179
+ else
180
+ json = JSON.parse(http.get(path, headers).body, symbolize_names: true)
181
+ end
161
182
  when :post
162
183
  req = Net::HTTP::Post.new(path)
163
- if queries[:file].is_a?(File) || queries[:file].is_a?(StringIO)
184
+ if path.include? "/2013-09-01/files"
164
185
  boundary = SecureRandom.uuid
165
186
  req.body = make_boundary(boundary, queries)
166
187
  headers["Content-Type"] = "multipart/form-data; boundary=#{boundary}"
@@ -195,7 +216,7 @@ module NCMB
195
216
  @@last_error = e
196
217
  raise NCMB::APIError.new(e.to_s)
197
218
  end
198
- if json[:error] != nil
219
+ if json.is_a?(Hash) && json[:error] != nil
199
220
  raise NCMB::APIError.new(json[:error])
200
221
  end
201
222
  json
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module NCMB
2
4
  class DataStore
3
5
  include NCMB
@@ -74,11 +76,19 @@ module NCMB
74
76
 
75
77
  def path
76
78
  return @path if @path
77
- path = "/#{@@client.api_version}/classes/#{@name}"
79
+ if ["file", "user", "push", "installation"].include? @name
80
+ if @name == "push"
81
+ "/#{@@client.api_version}/#{@name}"
82
+ else
83
+ "/#{@@client.api_version}/#{@name}s"
84
+ end
85
+ else
86
+ "/#{@@client.api_version}/classes/#{@name}"
87
+ end
78
88
  end
79
89
 
80
90
  def get
81
- return @items unless @items.nil?
91
+ # return @items unless @items.nil?
82
92
  results = @@client.get path, @queries
83
93
  return [] unless results
84
94
  if results[:error] && results[:error] != ""
data/lib/ncmb/device.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module NCMB
2
4
  class Devise < NCMB::DataStore
3
5
  attr_accessor :client, :name
@@ -7,7 +9,7 @@ module NCMB
7
9
  end
8
10
 
9
11
  def push
10
- return NCMB::Push.new(client
12
+ return NCMB::Push.new(client)
11
13
  end
12
14
  end
13
15
  end
data/lib/ncmb/error.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module NCMB
2
4
  class FetchError < StandardError
3
5
  end
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
@@ -8,14 +10,19 @@ module NCMB
8
10
  @fields[:fileName] = File.basename(file_path)
9
11
  @fields['mime-type'.to_sym] = MIME::Types.type_for(file_path)[0]
10
12
  end
13
+ @content = nil
11
14
  end
12
15
 
13
16
  def save
14
- @fields[:file] = open(self.file)
17
+ @fields[:file] = ::OpenURI.open_uri(self.file)
15
18
  super
16
19
  end
17
20
  alias :update :save
18
21
 
22
+ def get
23
+ @content = @@client.get path
24
+ end
25
+
19
26
  def path
20
27
  "#{base_path}/#{@fields[:fileName]}"
21
28
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module NCMB
2
4
  class GeoPoint
3
5
  include NCMB
@@ -7,11 +9,11 @@ module NCMB
7
9
  @longitude = longitude
8
10
  end
9
11
 
10
- def to_json(a = "")
12
+ def to_json(a = '')
11
13
  {
12
- "__type": "GeoPoint",
13
- "longitude": @longitude,
14
- "latitude": @latitude
14
+ '__type': 'GeoPoint',
15
+ 'longitude': @longitude,
16
+ 'latitude': @latitude
15
17
  }.to_json
16
18
  end
17
19
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module NCMB
2
4
  class Increment
3
5
  include NCMB
data/lib/ncmb/object.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module NCMB
2
4
  class Object
3
5
  include NCMB
@@ -43,7 +45,7 @@ module NCMB
43
45
  end
44
46
 
45
47
  def deletable?
46
- if self.acl['*'.to_sym][:write] == true
48
+ if self.acl.fields[:*][:write] == true
47
49
  return true
48
50
  end
49
51
  return false unless NCMB.CurrentUser
@@ -64,12 +66,12 @@ module NCMB
64
66
  @fields[:objectId] != nil
65
67
  end
66
68
 
67
- def post
69
+ def convert_params
68
70
  @fields.each do |key, field|
69
71
  if field.is_a?(NCMB::Object)
70
72
  field.save unless field.saved?
71
73
  @fields[key] = {
72
- __type: "Pointer",
74
+ __type: 'Pointer',
73
75
  className: field.ClassName,
74
76
  objectId: field.objectId
75
77
  }
@@ -83,6 +85,11 @@ module NCMB
83
85
  @fields[key] = relation
84
86
  end
85
87
  end
88
+ end
89
+
90
+ def post
91
+ return self.put if saved?
92
+ convert_params
86
93
  result = @@client.post path, @fields
87
94
  @fields.merge!(result)
88
95
  self
@@ -90,6 +97,8 @@ module NCMB
90
97
  alias :save :post
91
98
 
92
99
  def put
100
+ return self.post unless saved?
101
+ convert_params
93
102
  put_path = path
94
103
  params = @fields
95
104
  params.delete :objectId
data/lib/ncmb/push.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module NCMB
2
4
  class Push < NCMB::Object
3
5
  include NCMB