ncmb-ruby-client 0.1.3 → 0.2.0

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.
data/examples/relation.rb CHANGED
@@ -1,18 +1,24 @@
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.initialize application_key: yaml['application_key'], client_key: yaml['client_key']
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: "banana", type: "fruit")
15
- basket.foods << Food.new(name: "pear", type: "fruit")
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")
@@ -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
@@ -1,11 +1,25 @@
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
- @client = NCMB.initialize application_key: "6145f91061916580c742f806bab67649d10f45920246ff459404c46f00ff3e56", client_key: "1343d198b510a0315db1c03f3aa0e32418b7a743f8e4b47cbff670601345cf75"
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 :get, "/2013-09-01/classes/TestClass", "2013-12-02T02:44:35.452Z", {where: {testKey: "testValue"}}
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
 
@@ -1,10 +1,16 @@
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.initialize application_key: yaml['application_key'], client_key: yaml['client_key']
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 "User create successful."
24
- @user.set("Hello", "World")
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
@@ -1,36 +1,49 @@
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'
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
- "__type" => "GeoPoint",
17
- "latitude" => venue['location']['lat'],
18
- "longitude" => venue['location']['lng']
21
+ '__type' => 'GeoPoint',
22
+ 'latitude' => venue['location']['lat'],
23
+ 'longitude' => venue['location']['lng']
19
24
  }
20
25
  }
21
- puts venues_class.post(params).body
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
- "location" => {
26
- "$nearSphere" => {
27
- "__type" => "GeoPoint",
28
- "longitude" => 139.745433,
29
- "latitude" => 35.691152
33
+ 'location' => {
34
+ '$nearSphere' => {
35
+ '__type' => 'GeoPoint',
36
+ 'longitude' => 139.745433,
37
+ 'latitude' => 35.691152
30
38
  },
31
- "$maxDistanceInKilometers" => 10
39
+ '$maxDistanceInKilometers' => 10
32
40
  }
33
41
  }
34
- #
35
- puts venues_class.get params
36
- #puts venues_class.get queries
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
- $:.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
@@ -165,7 +181,7 @@ module NCMB
165
181
  end
166
182
  when :post
167
183
  req = Net::HTTP::Post.new(path)
168
- if queries[:file].is_a?(File) || queries[:file].is_a?(StringIO)
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}"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module NCMB
2
4
  class DataStore
3
5
  include NCMB
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
@@ -12,7 +14,7 @@ module NCMB
12
14
  end
13
15
 
14
16
  def save
15
- @fields[:file] = open(self.file)
17
+ @fields[:file] = ::OpenURI.open_uri(self.file)
16
18
  super
17
19
  end
18
20
  alias :update :save