ncmb-ruby-client 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 14fd45388ffb466c9a4137fab90f6ccff2b7a703
4
- data.tar.gz: 11edc994acc0356ac4c9d454fb95a4c7f32a7d2c
3
+ metadata.gz: 48bbbbec68144fff94c2a7e1656718803453c05e
4
+ data.tar.gz: 05d7af86eb4f65285015a6f392ec2682b624d0b2
5
5
  SHA512:
6
- metadata.gz: fb807c42c38fc82f591a5d61e9000c9472fa56ad77ce30d1e4636b889be9efdef1a68c4011a8213173501dfc7027768abc0839dc097c0dd6ceda27b2774fc760
7
- data.tar.gz: 5a5daaec4dc08b83608242ab6ca2ae535a26b2d5490f19635f070947cf2968517fad103a090a93cb70ca018e4f2dde641a14b557af54c635e0b1d32458478b36
6
+ metadata.gz: 15d6178f6738c97f646ebea562355c767a66440db59f8ab7ced4fd3276035dba92a82e856c7763c691e93e7d1125be6270bb389b91de4566de683d45d08df421
7
+ data.tar.gz: 801aa3ae319c262df4a975e5fd772ba7440f1b51a7d88663740e5bb32dd3e1d8cff93ef9e4469d6b09d1ae9eb5bb6c9cddcee30941bce29c242f3c76ed11b534
@@ -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
data/lib/ncmb.rb CHANGED
@@ -25,4 +25,5 @@ require 'ncmb/increment'
25
25
  require 'ncmb/acl'
26
26
  require 'ncmb/role'
27
27
  require 'ncmb/relation'
28
+ require 'ncmb/script'
28
29
  require 'ncmb/error'
data/lib/ncmb/client.rb CHANGED
@@ -9,6 +9,8 @@ end
9
9
 
10
10
  module NCMB
11
11
  DOMAIN = 'mbaas.api.nifcloud.com'
12
+ SCRIPT_DOMAIN = 'script.mbaas.api.nifcloud.com'
13
+ SCRIPT_API_VERSION = '2015-09-01'
12
14
  API_VERSION = '2013-09-01'
13
15
  @application_key = nil
14
16
  @client_key = nil
@@ -18,28 +20,30 @@ module NCMB
18
20
 
19
21
  class Client
20
22
  include NCMB
21
- attr_accessor :application_key, :client_key, :domain, :api_version
23
+ attr_accessor :application_key, :client_key, :domain, :api_version, :script_api_version
22
24
  def initialize(params = {})
23
25
  @domain = NCMB::DOMAIN
24
26
  @api_version = NCMB::API_VERSION
27
+ @script_domain = NCMB::SCRIPT_DOMAIN
28
+ @script_api_version = NCMB::SCRIPT_API_VERSION
25
29
  @application_key = params[:application_key]
26
30
  @client_key = params[:client_key]
27
31
  end
28
32
 
29
- def get(path, params = {})
30
- request :get, path, params
33
+ def get(path, params = {}, headers = {})
34
+ request :get, path, params, headers
31
35
  end
32
36
 
33
- def post(path, params = {})
34
- request :post, path, params
37
+ def post(path, params = {}, headers = {})
38
+ request :post, path, params, headers
35
39
  end
36
40
 
37
- def put(path, params = {})
38
- request :put, path, params
41
+ def put(path, params = {}, headers = {})
42
+ request :put, path, params, headers
39
43
  end
40
44
 
41
- def delete(path, params = {})
42
- request :delete, path, params
45
+ def delete(path, params = {}, headers = {})
46
+ request :delete, path, params, headers
43
47
  end
44
48
 
45
49
  def array2hash(ary)
@@ -90,7 +94,6 @@ module NCMB
90
94
  results[k.to_s] = v.to_s
91
95
  end
92
96
  end
93
- puts results
94
97
  results
95
98
  end
96
99
 
@@ -114,7 +117,7 @@ module NCMB
114
117
  end
115
118
  signature_base = []
116
119
  signature_base << method.upcase
117
- signature_base << @domain
120
+ signature_base << get_domain(path)
118
121
  signature_base << path
119
122
  signature_base << params.collect{|k,v| "#{k}=#{v}"}.join("&")
120
123
  Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), @client_key, signature_base.join("\n"))).strip()
@@ -135,10 +138,19 @@ module NCMB
135
138
  post_body.join("\r\n")
136
139
  end
137
140
 
138
- 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 = {})
139
150
  now = Time.now.utc.iso8601
140
151
  signature = generate_signature(method, path, now, queries)
141
- http = Net::HTTP.new(@domain, 443)
152
+ domain = get_domain(path)
153
+ http = Net::HTTP.new(domain, 443)
142
154
  http.use_ssl=true
143
155
  headers = {
144
156
  "X-NCMB-Application-Key" => @application_key,
@@ -146,6 +158,9 @@ module NCMB
146
158
  "X-NCMB-Timestamp" => now,
147
159
  "Content-Type" => 'application/json'
148
160
  }
161
+ (addHeaders || {}).each do |name, value|
162
+ headers[name] = value
163
+ end
149
164
  if NCMB.CurrentUser
150
165
  headers['X-NCMB-Apps-Session-Token'] = NCMB.CurrentUser.sessionToken
151
166
  end
@@ -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
+ .headers(params[:headers])
32
+ .body(params[:body])
33
+ .query(params[:query])
34
+ self
35
+ end
36
+
37
+ def headers(params)
38
+ @params[:headers] = 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[:headers])
54
+ end
55
+ end
56
+ end
data/lib/ncmb/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ncmb
4
- VERSION = '0.1.4'
4
+ VERSION = '0.1.5'
5
5
  end
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.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Atsushi Nakatsugawa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-02 00:00:00.000000000 Z
11
+ date: 2018-10-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -73,6 +73,7 @@ files:
73
73
  - examples/pointer_test.rb
74
74
  - examples/push.rb
75
75
  - examples/relation.rb
76
+ - examples/script.rb
76
77
  - examples/signature.rb
77
78
  - examples/user_test.rb
78
79
  - examples/venue_search.rb
@@ -91,6 +92,7 @@ files:
91
92
  - lib/ncmb/query.rb
92
93
  - lib/ncmb/relation.rb
93
94
  - lib/ncmb/role.rb
95
+ - lib/ncmb/script.rb
94
96
  - lib/ncmb/user.rb
95
97
  - lib/ncmb/version.rb
96
98
  - ncmb-ruby-client.gemspec