ncmb-ruby-client 0.0.1 → 0.0.2

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: 6e7f0e9ef529e5930000b966931ba6522e661aaf
4
- data.tar.gz: 673f21de25458af52ba1b9521f9fdfd77851967b
3
+ metadata.gz: d9310e6ab9d7f85770fed68a0941b6b22b2749b1
4
+ data.tar.gz: 2facc9f3eca84ce6d4b77b584c3186fc8332b6ec
5
5
  SHA512:
6
- metadata.gz: 282a25d8c282a343e01b912548e95e9f8dd7611de0505760f77ab2227eeeee77e5df51a95e35b1e2652e345abae7824c04a3d917e593839d8c7ccf4151815e24
7
- data.tar.gz: 69b1587e7fae7f688dba7a3d50a647e34fec746d421d5abbfaf00b112280fe577ede38046f6f84f4885f7de131bee6a18e18f131120c5d6c7f23daba90f20c5f
6
+ metadata.gz: 45e242c50b1ebdbae4cc60ebf7712fb1857d86c04fd8621432257a7564286ae066474552d30cafbb4e03799697bbca317247a9d3bf2df2c7de91728441b6120a
7
+ data.tar.gz: 3e90aea38e0f1fb457fd10d1072aaf7efc61eeedf7ebde913198e266e8aa208ae1accdee6a974bba1b37b4f953f84d3055166c7bd5c48cfaab4e111b7104a344
data/.gitignore CHANGED
@@ -16,3 +16,4 @@ test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
18
  setting.yml
19
+ .DS_Store
data/README.md CHANGED
@@ -3,15 +3,32 @@ ncmb-ruby-client
3
3
 
4
4
  A simple Ruby client for the nifty cloud mobile backend REST API
5
5
 
6
-
7
6
  Basic Usage
8
7
  -----------
9
8
 
10
9
  ```
11
- @ncmb = NCMB.init(application_key: application_key,
12
- client_key: client_key
13
- )
14
- queries = {:count => "1", :limit => "20", :order => "-createDate", :skip => "0"}
15
- todo_class = @ncmb.data_store 'TODO'
16
- todo_class.get queries
10
+ NCMB.initialize application_key: application_key, client_key: client_key
11
+
12
+ @todo = NCMB::DataStore.new 'Todo'
13
+ @todo = @todo.limit(20).count(1).skip(0)
14
+ puts "@todo[0].name #{@todo[0].name}"
15
+ ```
16
+
17
+ ### Register push notification
18
+
19
+ ```
20
+ NCMB.initialize application_key: application_key, client_key: client_key
21
+
22
+ @push = NCMB::Push.new
23
+ @push.immediateDeliveryFlag = true
24
+ @push.target = ['ios']
25
+ @push.message = "This is test message"
26
+ @push.deliveryExpirationTime = "3 day"
27
+ if @push.save
28
+ puts "Push save successful."
29
+ else
30
+ puts "Push save faild."
31
+ end
17
32
  ```
33
+
34
+ [ニフティクラウド mobile backend](http://mb.cloud.nifty.com/)
@@ -0,0 +1,13 @@
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
+ @todo = NCMB::DataStore.new 'Todo'
9
+ @todo = @todo.limit(20).count(1).skip(0)
10
+ # @todo = @todo.where(testKey: "testValue")
11
+ # puts "@todo[0] #{@todo[0]}"
12
+ puts "@todo[0].name #{@todo[0].name}"
13
+
data/examples/push.rb ADDED
@@ -0,0 +1,18 @@
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
+ @push = NCMB::Push.new
10
+ @push.immediateDeliveryFlag = true
11
+ @push.target = ['ios']
12
+ @push.message = "This is test message"
13
+ @push.deliveryExpirationTime = "3 day"
14
+ if @push.save
15
+ puts "Push save successful."
16
+ else
17
+ puts "Push save faild."
18
+ end
@@ -0,0 +1,11 @@
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
+ @client = NCMB.initialize application_key: "6145f91061916580c742f806bab67649d10f45920246ff459404c46f00ff3e56", client_key: "1343d198b510a0315db1c03f3aa0e32418b7a743f8e4b47cbff670601345cf75"
8
+
9
+ # puts @client.application_key
10
+ puts @client.generate_signature :get, "/2013-09-01/classes/TestClass", "2013-12-02T02:44:35.452Z", {where: {testKey: "testValue"}}
11
+
@@ -1,4 +1,3 @@
1
- # -*- coding: utf-8 -*-
2
1
  $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
2
  $:.unshift(File.dirname(__FILE__))
4
3
  require 'rubygems'
@@ -22,24 +21,6 @@ json['response']['venues'].each do |venue|
22
21
  puts venues_class.post(params).body
23
22
  end
24
23
  params = {}
25
- params[:where] = {
26
- "point" => {
27
- "$within" => {
28
- "$box" => [
29
- {
30
- "__type" => "GeoPoint",
31
- "latitude" => 35.690921,
32
- "longitude" => 139.700258
33
- },
34
- {
35
- "__type" => "GeoPoint",
36
- "latitude" => 35.728926,
37
- "longitude" => 139.71038
38
- }
39
- ]
40
- }
41
- }
42
- }
43
24
  params[:where] = {
44
25
  "location" => {
45
26
  "$nearSphere" => {
data/lib/ncmb.rb CHANGED
@@ -3,3 +3,4 @@ $:.unshift(File.dirname(__FILE__))
3
3
  require "ncmb/version"
4
4
  require "ncmb/client"
5
5
  require "ncmb/data_store"
6
+ require "ncmb/push"
data/lib/ncmb/client.rb CHANGED
@@ -7,7 +7,12 @@ require 'json'
7
7
  module NCMB
8
8
  DOMAIN = 'mb.api.cloud.nifty.com'
9
9
  API_VERSION = '2013-09-01'
10
+ @application_key = nil
11
+ @client_key = nil
12
+ @@client = nil
13
+
10
14
  class Client
15
+ include NCMB
11
16
  attr_accessor :application_key, :client_key, :domain, :api_version
12
17
  def initialize(params = {})
13
18
  @domain = NCMB::DOMAIN
@@ -16,10 +21,6 @@ module NCMB
16
21
  @client_key = params[:client_key]
17
22
  end
18
23
 
19
- def data_store(name)
20
- NCMB::DataStore.new self, name
21
- end
22
-
23
24
  def get(path, params)
24
25
  request :get, path, params
25
26
  end
@@ -28,15 +29,45 @@ module NCMB
28
29
  request :post, path, params
29
30
  end
30
31
 
32
+ def array2hash(ary)
33
+ new_v = {}
34
+ ary.each do |hash|
35
+ if hash.is_a? Hash
36
+ key = hash.keys[0]
37
+ new_v[key] = hash[key]
38
+ else
39
+ new_v = [hash]
40
+ end
41
+ end
42
+ new_v
43
+ end
44
+
31
45
  def encode_query(queries = {})
46
+ results = {}
32
47
  queries.each do |k, v|
48
+ v = array2hash(v) if v.is_a? Array
33
49
  if v.is_a? Hash
34
- queries[k] = URI.escape(v.to_json.to_s, /[^-_.!~*'()a-zA-Z\d;\/?:@&=+$,#]/)
50
+ results[k.to_s] = URI.escape(v.to_json.to_s, /[^-_.!~*'()a-zA-Z\d;\/?@&=+$,#]/)
35
51
  else
36
- queries[k] = URI.escape(v.to_s, /[^-_.!~*'()a-zA-Z\d;\/?:@&=+$,#]/)
52
+ results[k.to_s] = URI.escape(v.to_s, /[^-_.!~*'()a-zA-Z\d;\/?@&=+$,#]/)
37
53
  end
38
54
  end
39
- queries
55
+ results
56
+ end
57
+
58
+ def hash2query(queries = {})
59
+ results = {}
60
+ queries.each do |k, v|
61
+ v = array2hash(v) if v.is_a? Array
62
+ if v.is_a? Hash
63
+ results[k.to_s] = v.to_json.to_s
64
+ else
65
+ results[k.to_s] = v.to_s
66
+ end
67
+ end
68
+ results.collect do |key, value|
69
+ "#{key}=#{value}"
70
+ end
40
71
  end
41
72
 
42
73
  def generate_signature(method, path, now = nil, queries = {})
@@ -48,19 +79,19 @@ module NCMB
48
79
  params = method == :get ? params_base.merge(encode_query(queries)) : params_base
49
80
  now ||= Time.now.utc.iso8601
50
81
  params = params.merge "X-NCMB-Timestamp" => now
51
- params = Hash[params.sort{|a, b| a[0].to_s <=> b[0].to_s}]
82
+ params = params.sort_by{|a, b| a.to_s}.to_h
52
83
  signature_base = []
53
84
  signature_base << method.upcase
54
85
  signature_base << @domain
55
86
  signature_base << path
56
87
  signature_base << params.collect{|k,v| "#{k}=#{v}"}.join("&")
57
- signature = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha256'), @client_key, signature_base.join("\n"))).strip()
88
+ signature = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), @client_key, signature_base.join("\n"))).strip()
58
89
  end
59
90
 
60
91
  def request(method, path, queries = {})
61
92
  now = Time.now.utc.iso8601
62
93
  signature = generate_signature(method, path, now, queries)
63
- query = queries.collect{|k,v| "#{k}=#{v.is_a?(Hash) ? v.to_json.to_s : v}"}.join("&")
94
+ query = hash2query(queries).join("&")
64
95
  http = Net::HTTP.new(@domain, 443)
65
96
  http.use_ssl=true
66
97
  headers = {
@@ -70,16 +101,15 @@ module NCMB
70
101
  "Content-Type" => 'application/json'
71
102
  }
72
103
  if method == :get
73
- path = path + (query == '' ? "" : "?"+query)
74
- return http.get(path, headers).body
104
+ path = path + URI.escape((query == '' ? "" : "?"+query), /[^-_.!~*'()a-zA-Z\d;\/?@&=+$,#]/)
105
+ return JSON.parse(http.get(path, headers).body, symbolize_names: true)
75
106
  else
76
- return http.post(path, queries.to_json, headers)
107
+ return JSON.parse(http.post(path, queries.to_json, headers).body, symbolize_names: true)
77
108
  end
78
109
  end
79
110
  end
80
111
 
81
- @@client = nil
82
- def NCMB.init(params = {})
112
+ def NCMB.initialize(params = {})
83
113
  defaulted = {
84
114
  application_key: ENV["NCMB_APPLICATION_KEY"],
85
115
  client_key: ENV["NCMB_CLIENT_KEY"]
@@ -1,19 +1,77 @@
1
1
  module NCMB
2
2
  class DataStore
3
- attr_accessor :client, :name
4
- def initialize(client, name)
5
- @client = client
6
- @name = name
3
+ include NCMB
4
+
5
+ def initialize(name, fields = {}, alc = "")
6
+ @@name = name
7
+ @@alc = alc
8
+ @@fields = fields
9
+ @@queries = {}
10
+ @@items = nil
11
+ end
12
+
13
+ def method_missing(name)
14
+ return @@fields[name.to_sym] if @@fields[name.to_sym]
15
+ end
16
+
17
+ def order(field)
18
+ @@queries[:order] = field
19
+ self
20
+ end
21
+
22
+ def limit(count)
23
+ @@queries[:limit] = count
24
+ self
25
+ end
26
+
27
+ def count(count)
28
+ @@queries[:count] = count
29
+ self
30
+ end
31
+
32
+ def skip(count)
33
+ @@queries[:skip] = count
34
+ self
35
+ end
36
+
37
+ def where(params = {})
38
+ @@queries[:where] = [] unless @@queries[:where]
39
+ if params.size == 1
40
+ @@queries[:where] << params
41
+ else
42
+ params.each do |hash|
43
+ @@queries[:where] << hash
44
+ end
45
+ end
46
+ self
47
+ end
48
+
49
+ def [](count)
50
+ return @@items[count] unless @@items.nil?
51
+ get(@@queries)[count]
7
52
  end
8
53
 
9
54
  def get(queries = {})
10
- path = "/#{@client.api_version}/classes/#{@name}"
11
- @client.get path, queries
55
+ path = "/#{@@client.api_version}/classes/#{@@name}"
56
+ results = @@client.get path, queries
57
+ return [] unless results
58
+ if results[:error] && results[:error] != ""
59
+ @@error = results
60
+ raise 'error'
61
+ end
62
+ items = []
63
+ results[:results].each do |result|
64
+ alc = result[:acl]
65
+ result.delete(:acl)
66
+ items << NCMB::DataStore.new(@@name, result, alc)
67
+ end
68
+ @@items = items
12
69
  end
13
70
 
14
71
  def post(queries = {})
15
- path = "/#{@client.api_version}/classes/#{@name}"
16
- @client.post path, queries
72
+ path = "/#{@client.api_version}/classes/#{@@name}"
73
+ result = @client.post path, queries
74
+ NCMB::DataStore.new(client, name, result)
17
75
  end
18
76
  end
19
77
  end
@@ -0,0 +1,13 @@
1
+ module NCMB
2
+ class Devise < NCMB::DataStore
3
+ attr_accessor :client, :name
4
+ def initialize(client)
5
+ @client = client
6
+ @name = "Instration"
7
+ end
8
+
9
+ def push
10
+ return NCMB::Push.new(client
11
+ end
12
+ end
13
+ end
data/lib/ncmb/push.rb ADDED
@@ -0,0 +1,26 @@
1
+ module NCMB
2
+ class Push
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
+
8
+ def save
9
+ path = "/#{@@client.api_version}/push"
10
+ queries = {}
11
+ [: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?
15
+ 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
24
+ end
25
+ end
26
+ end
data/lib/ncmb/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ncmb
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/spec/get_spec.rb CHANGED
@@ -36,7 +36,7 @@ describe NCMB do
36
36
  }
37
37
  }
38
38
  }
39
- signature = @ncmb.generate_signature :get, "/#{@ncmb.api_version}/classes/Venue", "2014-05-20T04:55:16.395Z", params
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
40
  expect(signature).to eq("sqlhM3xxNPUxFWDHQ5CdDqBp6dInU/YkO2PzuY31Pbk=")
41
41
  end
42
42
  end
metadata CHANGED
@@ -1,55 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ncmb-ruby-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Atsushi Nakatsugawa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-20 00:00:00.000000000 Z
11
+ date: 2014-10-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.3'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.3'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  description: A simple Ruby client for the nifty cloud mobile backend REST API
@@ -59,16 +59,21 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
- - .gitignore
62
+ - ".gitignore"
63
63
  - Gemfile
64
64
  - LICENSE.txt
65
65
  - README.md
66
66
  - Rakefile
67
+ - examples/data_store.rb
68
+ - examples/push.rb
69
+ - examples/signature.rb
67
70
  - examples/venue_search.rb
68
71
  - examples/venues.json
69
72
  - lib/ncmb.rb
70
73
  - lib/ncmb/client.rb
71
74
  - lib/ncmb/data_store.rb
75
+ - lib/ncmb/device.rb
76
+ - lib/ncmb/push.rb
72
77
  - lib/ncmb/version.rb
73
78
  - ncmb-ruby-client.gemspec
74
79
  - setting_default.yml
@@ -85,17 +90,17 @@ require_paths:
85
90
  - lib
86
91
  required_ruby_version: !ruby/object:Gem::Requirement
87
92
  requirements:
88
- - - '>='
93
+ - - ">="
89
94
  - !ruby/object:Gem::Version
90
95
  version: '0'
91
96
  required_rubygems_version: !ruby/object:Gem::Requirement
92
97
  requirements:
93
- - - '>='
98
+ - - ">="
94
99
  - !ruby/object:Gem::Version
95
100
  version: '0'
96
101
  requirements: []
97
102
  rubyforge_project:
98
- rubygems_version: 2.0.7
103
+ rubygems_version: 2.2.2
99
104
  signing_key:
100
105
  specification_version: 4
101
106
  summary: A simple Ruby client for the nifty cloud mobile backend REST API