parsecom 0.0.2 → 0.0.3

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.
@@ -5,19 +5,20 @@ module Parse
5
5
  API_VERSION = 1
6
6
  @@default_client = nil
7
7
 
8
- attr_accessor :http_client, :session_token
8
+ attr_accessor :http_client, :session_token, :master_key
9
9
 
10
10
  def self.default_client
11
11
  @@default_client ||= new
12
12
  end
13
13
 
14
- def initialize application_id=nil, api_key=nil, http_client=nil
14
+ def initialize application_id=nil, api_key=nil, master_key=nil, http_client=nil
15
15
  @application_id = application_id || Parse.application_id
16
16
  @api_key = api_key || Parse.api_key
17
+ @master_key = master_key || Parse.master_key
17
18
  if @application_id.nil? || @api_key.nil?
18
- raise ArgumentError.new <<-EOS
19
- Both Application ID and API Key must be set.
20
- ex. Parse.credentials application_id: APPLICATION_ID, api_key: API_KEY
19
+ raise ArgumentError.new <<-EOS.gsub(/^ +/)
20
+ Both Application ID and API Key must be set.
21
+ ex. Parse.credentials application_id: APPLICATION_ID, api_key: API_KEY
21
22
  EOS
22
23
  end
23
24
  @http_client = http_client || Parse::HttpClient.new(API_SERVER)
@@ -25,17 +26,25 @@ ex. Parse.credentials application_id: APPLICATION_ID, api_key: API_KEY
25
26
 
26
27
  def call_api method, endpoint, body=nil, opt_headers={}, &block
27
28
  endpoint = "/#{API_VERSION}/#{endpoint}" unless endpoint[0] == '/'
29
+ headers = build_headers opt_headers
30
+ if body.is_a?(Hash)
31
+ body = Hash[*(body.to_a.map{|k, v| [k, URI.encode(v)]}.flatten)].to_json
32
+ end
33
+ @http_client.request method, endpoint, headers, body, &block
34
+ end
35
+
36
+ def build_headers opt_headers={}
28
37
  headers = {
29
38
  'X-Parse-Application-Id' => @application_id,
30
- 'X-Parse-REST-API-Key' => @api_key,
31
39
  'Content-Type' => 'application/json'
32
40
  }
33
- headers.update('X-Parse-Session-Token' => @session_token) if @session_token
34
- headers.update opt_headers
35
- if body.is_a?(Hash)
36
- body = Hash[*(body.to_a.map{|k, v| [k, URI.encode(v)]}.flatten)].to_json
41
+ if @use_master_key
42
+ headers['X-Parse-Master-Key'] = @master_key
43
+ else
44
+ headers['X-Parse-REST-API-Key'] = @api_key
37
45
  end
38
- @http_client.request method, endpoint, headers, body, &block
46
+ headers['X-Parse-Session-Token'] = @session_token if @session_token
47
+ headers.update opt_headers
39
48
  end
40
49
 
41
50
  def sign_up username, password, opts={}, &block
@@ -138,6 +147,34 @@ ex. Parse.credentials application_id: APPLICATION_ID, api_key: API_KEY
138
147
  end
139
148
  end
140
149
 
150
+ def use_master_key!
151
+ self.use_master_key = true
152
+ end
153
+
154
+ def use_master_key= val
155
+ raise ArgumentError.new('master_key is not set.') if val && !@master_key
156
+ @use_master_key = val
157
+ end
158
+
159
+ def use_master_key &block
160
+ return @use_master_key unless block
161
+
162
+ tmp, @use_master_key = @use_master_key, true
163
+ ret = block.call
164
+ @use_master_key = tmp
165
+ ret
166
+ end
167
+
168
+ %w(find find_by_id find_by_query create update delete call_function).each do |name|
169
+ eval <<-EOS
170
+ def #{name}! *args, &block
171
+ use_master_key do
172
+ #{name} *args, &block
173
+ end
174
+ end
175
+ EOS
176
+ end
177
+
141
178
  def method_missing name, *args, &block
142
179
  call_function name, args.first
143
180
  end
@@ -33,6 +33,10 @@ module Parse
33
33
  def find object_id_or_conditions, opts={}
34
34
  parse_client.find self, object_id_or_conditions, opts
35
35
  end
36
+
37
+ def find! object_id_or_conditions, opts={}
38
+ parse_client.find! self, object_id_or_conditions, opts
39
+ end
36
40
  end
37
41
 
38
42
  attr_accessor :obj_id, :created_at, :updated_at, :acl
@@ -70,21 +74,32 @@ module Parse
70
74
  self.class.parse_class_name
71
75
  end
72
76
 
73
- def save hash=@updated_hash
77
+ def save hash=@updated_hash, use_master_key=false
74
78
  check_deleted!
79
+
80
+ unless hash.is_a? Hash
81
+ hash = @updated_hash
82
+ use_master_key = hash
83
+ end
75
84
  hash = string_keyed_hash hash
76
85
  if new?
77
- create hash
86
+ create hash, use_master_key
78
87
  else
79
- update hash
88
+ update hash, use_master_key
80
89
  end
81
90
  end
82
91
 
83
- def create hash
92
+ def save! hash=@updated_hash
93
+ save hash, true
94
+ end
95
+
96
+ def create hash, use_master_key=false
84
97
  check_deleted!
85
98
  hash = string_keyed_hash hash
86
99
  @updated_hash.update hash
87
- parse_client.create(self, @updated_hash).tap do |response|
100
+ #parse_client.create(self, @updated_hash).tap do |response|
101
+ method = use_master_key ? :create! : :create
102
+ parse_client.send(method, self, @updated_hash).tap do |response|
88
103
  @obj_id = response['objectId']
89
104
  @created_at = Date.parse response['createdAt']
90
105
  @updated_at = @created_at
@@ -93,25 +108,41 @@ module Parse
93
108
  @updated_hash.clear
94
109
  end
95
110
  end
111
+
112
+ def create! hash
113
+ create hash, true
114
+ end
96
115
 
97
- def update hash
116
+ def update hash, use_master_key=false
98
117
  check_deleted!
99
118
  hash = string_keyed_hash hash
100
- parse_client.update(self, hash).tap do |response|
119
+ #parse_client.update(self, hash).tap do |response|
120
+ method = use_master_key ? :update! : :update
121
+ parse_client.send(method, self, hash).tap do |response|
101
122
  @updated_at = Date.parse response['updatedAt']
102
123
  @raw_hash.update @updated_hash
103
124
  @updated_hash.clear
104
125
  end
105
126
  end
106
127
 
107
- def delete
128
+ def update! hash
129
+ update hash, true
130
+ end
131
+
132
+ def delete use_master_key=false
108
133
  raise 'You cannot delete new object' if new?
109
134
  check_deleted!
110
- parse_client.delete(self).tap do |response|
135
+ #parse_client.delete(self).tap do |response|
136
+ method = use_master_key ? :delete! : :delete
137
+ parse_client.send(method, self).tap do |response|
111
138
  @deleted = true
112
139
  end
113
140
  end
114
141
 
142
+ def delete!
143
+ delete true
144
+ end
145
+
115
146
  def obj_id
116
147
  @obj_id || @raw_hash['objectId']
117
148
  end
@@ -1,3 +1,3 @@
1
1
  module Parse
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -19,6 +19,7 @@ require 'parse/file'
19
19
  module Parse
20
20
  @@application_id = nil
21
21
  @@api_key = nil
22
+ @@master_key = nil
22
23
  @@auto_snake_case = false
23
24
 
24
25
  module_function
@@ -39,9 +40,18 @@ module Parse
39
40
  @@api_key = api_key
40
41
  end
41
42
 
43
+ def master_key
44
+ @@master_key
45
+ end
46
+
47
+ def master_key= master_key
48
+ @@master_key = master_key
49
+ end
50
+
42
51
  def credentials hash
43
52
  @@application_id = hash[:application_id]
44
53
  @@api_key = hash[:api_key]
54
+ @@master_key = hash[:master_key]
45
55
  end
46
56
 
47
57
  def credentials= hash
@@ -8,7 +8,7 @@ Gem::Specification.new do |gem|
8
8
  gem.version = Parse::VERSION
9
9
  gem.authors = ["Ando Yasushi"]
10
10
  gem.email = ["andyjpn@gmail.com"]
11
- gem.description = %q{Pure Ruby version of parse.com client. This library allow you to access straightforwardly to parse.com REST API.}
11
+ gem.description = %q{Pure Ruby version of parse.com client. This library allows you to access straightforwardly to parse.com REST API.}
12
12
  gem.summary = %q{Pure Ruby version of parse.com client}
13
13
  gem.homepage = "https://github.com/technohippy/parsecom"
14
14
 
@@ -0,0 +1,26 @@
1
+ # coding:utf-8
2
+ require 'spec_helper'
3
+ require 'parsecom'
4
+
5
+ describe Parse::Client, 'when using master_key' do
6
+ it 'should use api key' do
7
+ client = Parse::Client.new# 'appid', 'apikey'
8
+ client.build_headers.keys.should be_include('X-Parse-REST-API-Key')
9
+ client.build_headers.keys.should_not be_include('X-Parse-Master-Key')
10
+ end
11
+
12
+ it 'should use master key' do
13
+ client = Parse::Client.new# 'appid', 'apikey', 'masterkey'
14
+ client.use_master_key!
15
+ client.build_headers.keys.should_not be_include('X-Parse-REST-API-Key')
16
+ client.build_headers.keys.should be_include('X-Parse-Master-Key')
17
+ end
18
+
19
+ it 'should raise an error when master_key is not set' do
20
+ client = Parse::Client.new# 'appid', 'apikey'
21
+ client.master_key = nil
22
+ expect {
23
+ client.use_master_key!
24
+ }.to raise_error
25
+ end
26
+ end
@@ -10,5 +10,5 @@ end
10
10
 
11
11
  require 'parsecom'
12
12
  # setup Parse object
13
- Parse.credentials application_id: ENV['APPLICATION_ID'], api_key: ENV['API_KEY']
13
+ Parse.credentials application_id: ENV['APPLICATION_ID'], api_key: ENV['API_KEY'], master_key: ENV['MASTER_KEY']
14
14
  #puts('set your credentials in spec/spec_helper.rb and remove this line') || exit
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parsecom
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-03 00:00:00.000000000 Z
12
+ date: 2013-10-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -27,7 +27,7 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
- description: Pure Ruby version of parse.com client. This library allow you to access
30
+ description: Pure Ruby version of parse.com client. This library allows you to access
31
31
  straightforwardly to parse.com REST API.
32
32
  email:
33
33
  - andyjpn@gmail.com
@@ -52,6 +52,7 @@ files:
52
52
  - lib/parse/version.rb
53
53
  - lib/parsecom.rb
54
54
  - parsecom.gemspec
55
+ - spec/parse_client_spec.rb
55
56
  - spec/parse_object_spec.rb
56
57
  - spec/parse_query_spec.rb
57
58
  - spec/parse_user_spec.rb
@@ -81,6 +82,7 @@ signing_key:
81
82
  specification_version: 3
82
83
  summary: Pure Ruby version of parse.com client
83
84
  test_files:
85
+ - spec/parse_client_spec.rb
84
86
  - spec/parse_object_spec.rb
85
87
  - spec/parse_query_spec.rb
86
88
  - spec/parse_user_spec.rb