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.
@@ -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,12 +45,12 @@ 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
50
- return false unless self.acl[NCMB.CurrentUser.objectId.to_sym]
51
- return false unless self.acl[NCMB.CurrentUser.objectId.to_sym][:write]
52
+ return false unless self.acl.fields[NCMB.CurrentUser.objectId.to_sym]
53
+ return false unless self.acl.fields[NCMB.CurrentUser.objectId.to_sym][:write]
52
54
  true
53
55
  end
54
56
 
@@ -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
data/lib/ncmb/query.rb CHANGED
@@ -1,19 +1,21 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module NCMB
2
4
  module Query
3
5
  [
4
- {greaterThan: "$gt"},
5
- {notEqualTo: "$ne"},
6
+ {greaterThan: '$gt'},
7
+ {notEqualTo: '$ne'},
6
8
  {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"},
9
+ {lessThan: '$lt'},
10
+ {lessThanOrEqualTo: '$lte'},
11
+ {greaterThanOrEqualTo: '$gte'},
12
+ {in: '$in'},
13
+ {notIn: '$nin'},
14
+ {exists: '$exists'},
15
+ {regex: '$regex'},
16
+ {inArray: '$inArray'},
17
+ {notInArray: '$ninArray'},
18
+ {allInArray: '$all'},
17
19
  ].each do |m|
18
20
  define_method m.keys.first do |name, value|
19
21
  params = {}
@@ -29,14 +31,14 @@ module NCMB
29
31
  end
30
32
 
31
33
  [
32
- {withinKilometers: "$maxDistanceInKilometers"},
33
- {withinMiles: "$maxDistanceInMiles"},
34
- {withinRadians: "$maxDistanceInRadians"}
34
+ {withinKilometers: '$maxDistanceInKilometers'},
35
+ {withinMiles: '$maxDistanceInMiles'},
36
+ {withinRadians: '$maxDistanceInRadians'}
35
37
  ].each do |m|
36
38
  define_method m.keys.first do |name, geo, value|
37
39
  params = {}
38
40
  params[name] = {
39
- "$nearSphere": geo,
41
+ '$nearSphere': geo,
40
42
  }
41
43
  params[name][m.values.first] = value
42
44
  @queries[@search_key] << params
@@ -47,8 +49,8 @@ module NCMB
47
49
  def withinSquare(name, geo1, geo2)
48
50
  params = {}
49
51
  params[name] = {
50
- "$within": {
51
- "$box": [
52
+ '$within': {
53
+ '$box': [
52
54
  geo1,
53
55
  geo2
54
56
  ]
data/lib/ncmb/relation.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module NCMB
2
4
  class Relation < Array
3
5
  include NCMB
@@ -6,16 +8,16 @@ module NCMB
6
8
 
7
9
  end
8
10
 
9
- def to_json(a = "")
11
+ def to_json(a = '')
10
12
  params = {
11
- "__op": "AddRelation",
13
+ '__op': 'AddRelation'
12
14
  }
13
- params["objects"] = []
15
+ params['objects'] = []
14
16
  self.each do |obj|
15
- params["objects"] << {
16
- "__type": "Pointer",
17
- "className": obj.ClassName,
18
- "objectId": obj.objectId
17
+ params['objects'] << {
18
+ '__type': 'Pointer',
19
+ 'className': obj.ClassName,
20
+ 'objectId': obj.objectId
19
21
  }
20
22
  end
21
23
  params.to_json
data/lib/ncmb/role.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module NCMB
2
4
  class Role < NCMB::Object
3
5
  include NCMB
@@ -10,7 +12,7 @@ module NCMB
10
12
  roleName: name
11
13
  }
12
14
  end
13
- @fields[:acl] = NCMB::Acl.new(@fields[:acl])
15
+ @fields[:acl] = NCMB::Acl.new
14
16
  end
15
17
 
16
18
  def self.find_or_create(name)
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NCMB
4
+ class Script
5
+ include NCMB
6
+ def initialize(name)
7
+ @name = name
8
+ @params = {}
9
+ end
10
+
11
+ def get(params = {})
12
+ self.set(params).execute('get')
13
+ end
14
+
15
+ def post(params = {})
16
+ self.set(params).execute('post')
17
+ end
18
+
19
+ def put(params = {})
20
+ self.set(params).execute('put')
21
+ end
22
+
23
+ def delete(params = {})
24
+ self.set(params)
25
+ @@client.delete
26
+ end
27
+
28
+ def set(params)
29
+ params = Hash[ params.map{ |k, v| [k.to_sym, v] } ]
30
+ self
31
+ .header(params[:header])
32
+ .body(params[:body])
33
+ .query(params[:query])
34
+ self
35
+ end
36
+
37
+ def header(params)
38
+ @params[:header] = params
39
+ self
40
+ end
41
+
42
+ def body(params)
43
+ @params[:body] = params
44
+ self
45
+ end
46
+
47
+ def query(params)
48
+ @params[:query] = params
49
+ self
50
+ end
51
+
52
+ def execute(method)
53
+ @@client.send(method, "/#{@@client.script_api_version}/script/#{@name}", (@params[:query] || {}).merge(@params[:body] || {}), @params[:header])
54
+ end
55
+ end
56
+ end
data/lib/ncmb/user.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module NCMB
2
4
  class User < NCMB::Object
3
5
  include NCMB
@@ -23,7 +25,7 @@ module NCMB
23
25
  end
24
26
 
25
27
  def put
26
- params = @fields
28
+ params = @fields.clone
27
29
  session_key = params[:sessionToken]
28
30
  [:objectId, :createDate, :updateDate, :sessionToken, :password].each do |name|
29
31
  params.delete name
data/lib/ncmb/version.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Ncmb
2
- VERSION = "0.1.3"
4
+ VERSION = '0.2.0'
3
5
  end
@@ -1,24 +1,24 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  lib = File.expand_path('../lib', __FILE__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
5
  require 'ncmb/version'
5
6
 
6
7
  Gem::Specification.new do |spec|
7
- spec.name = "ncmb-ruby-client"
8
+ spec.name = 'ncmb-ruby-client'
8
9
  spec.version = Ncmb::VERSION
9
- spec.authors = ["Atsushi Nakatsugawa"]
10
- spec.email = ["atsushi@moongift.jp"]
11
- spec.description = %q{A simple Ruby client for the nifty cloud mobile backend REST API}
12
- spec.summary = %q{A simple Ruby client for the nifty cloud mobile backend REST API}
13
- spec.homepage = "http://mb.cloud.nifty.com/"
14
- spec.license = "MIT License"
10
+ spec.authors = ['Atsushi Nakatsugawa']
11
+ spec.email = ['atsushi@moongift.jp']
12
+ spec.description = %q(A simple Ruby client for the nifty cloud mobile backend REST API)
13
+ spec.summary = %q(A simple Ruby client for the nifty cloud mobile backend REST API)
14
+ spec.homepage = 'http://mb.cloud.nifty.com/'
15
+ spec.license = 'MIT License'
15
16
 
16
17
  spec.files = `git ls-files`.split($/)
17
18
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
19
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
20
-
21
- spec.add_development_dependency "bundler", "~> 1.3"
22
- spec.add_development_dependency "rake"
23
- spec.add_development_dependency "rspec"
20
+ spec.require_paths = ['lib']
21
+ spec.add_development_dependency 'bundler', '>= 2.2.10'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'rspec'
24
24
  end
data/spec/delete_spec.rb CHANGED
@@ -1,20 +1,24 @@
1
- require "spec_helper"
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
2
4
  describe NCMB do
3
5
  before do
4
6
  yaml = YAML.load_file(File.join(File.dirname(__FILE__), '..', 'setting.yml'))
5
- NCMB.initialize application_key: yaml['application_key'], client_key: yaml['client_key']
7
+ NCMB.initialize(
8
+ application_key: yaml['application_key'],
9
+ client_key: yaml['client_key']
10
+ )
6
11
  @Todo = NCMB::DataStore.new('TODO')
7
- @todo = @Todo.new(text: "Test task")
12
+ @todo = @Todo.new(text: 'Test task')
8
13
  @object_id = @todo.save().objectId
9
14
  end
10
15
 
11
- it "Delete #1" do
12
- @todo.delete.should == true
16
+ it 'Delete #1' do
17
+ expect(@todo.delete).to eql(true)
13
18
  end
14
19
 
15
- it "Delete again" do
16
- @todo.delete.should == true
17
- @todo.delete.should == false
18
- @todo.error[:code].should == 'E404001'
20
+ it 'Delete again' do
21
+ expect(@todo.delete).to eql(true)
22
+ expect { @todo.delete }.to raise_error(NCMB::APIError, "No data available.")
19
23
  end
20
24
  end
data/spec/get_spec.rb CHANGED
@@ -1,9 +1,13 @@
1
- # -*- coding: utf-8 -*-
2
- require "spec_helper"
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
3
4
  describe NCMB do
4
5
  before do
5
6
  yaml = YAML.load_file(File.join(File.dirname(__FILE__), '..', 'setting.yml'))
6
- NCMB.initialize application_key: yaml['application_key'], client_key: yaml['client_key']
7
+ NCMB.initialize(
8
+ application_key: yaml['application_key'],
9
+ client_key: yaml['client_key']
10
+ )
7
11
  @todoClass = todoClass = NCMB::DataStore.new 'GET_TODO'
8
12
  @todoClass.delete_all
9
13
  10.times do |i|
@@ -11,9 +15,9 @@ describe NCMB do
11
15
  end
12
16
  end
13
17
 
14
- it "Get #1" do
18
+ it 'Get #1' do
15
19
  @items = @todoClass.order('-createDate').skip(0).all
16
- @items.length.should == 10
20
+ expect(@items.length).to eql(10)
17
21
  end
18
22
 
19
23
  end
data/spec/post_spec.rb CHANGED
@@ -1,19 +1,24 @@
1
- require "spec_helper"
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
2
4
  describe NCMB do
3
5
  before do
4
6
  yaml = YAML.load_file(File.join(File.dirname(__FILE__), '..', 'setting.yml'))
5
- NCMB.initialize application_key: yaml['application_key'], client_key: yaml['client_key']
7
+ NCMB.initialize(
8
+ application_key: yaml['application_key'],
9
+ client_key: yaml['client_key']
10
+ )
6
11
  end
7
12
 
8
- it "Post #1" do
9
- text = "Test task"
13
+ it 'Post #1' do
14
+ text = 'Test task'
10
15
  queries = {todo: text}
11
16
  todo_class = NCMB::DataStore.new 'POST_TODO'
12
17
  todo = todo_class.new(queries).save
13
- todo.todo.should == text
18
+ expect(todo.todo).to eql(text)
14
19
  end
15
20
 
16
- it "Post with location #1" do
21
+ it 'Post with location #1' do
17
22
 
18
23
  end
19
24
  end
data/spec/put_spec.rb CHANGED
@@ -1,15 +1,24 @@
1
- require "spec_helper"
1
+ require 'spec_helper'
2
2
  describe NCMB do
3
3
  before do
4
- yaml = YAML.load_file(File.join(File.dirname(__FILE__), '..', 'setting.yml'))
5
- @ncmb = NCMB::Client.new(application_key: yaml['application_key'],
6
- client_key: yaml['client_key']
7
- )
8
- @object_id = @ncmb.post('/2013-09-01/classes/TODO', todo: "Test task")[:objectId]
4
+ yaml = YAML.load_file(
5
+ File.join(File.dirname(__FILE__), '..', 'setting.yml')
6
+ )
7
+ @ncmb = NCMB::Client.new(
8
+ application_key: yaml['application_key'],
9
+ client_key: yaml['client_key']
10
+ )
11
+ @object_id = @ncmb.post(
12
+ '/2013-09-01/classes/TODO',
13
+ todo: 'Test task'
14
+ )[:objectId]
9
15
  end
10
-
11
- it "Put #1" do
12
- res = @ncmb.put("/2013-09-01/classes/TODO/#{@object_id}", todo: "Test task updated")
13
- res[:updateDate].should_not be_nil
16
+
17
+ it 'Put #1' do
18
+ res = @ncmb.put(
19
+ "/2013-09-01/classes/TODO/#{@object_id}",
20
+ todo: 'Test task updated'
21
+ )
22
+ expect(res[:updateDate]).not_to be_nil
14
23
  end
15
24
  end