parse-stack 1.4.3 → 1.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Changes.md +52 -39
- data/Gemfile.lock +2 -2
- data/README.md +609 -124
- data/bin/console +0 -9
- data/lib/parse/api/all.rb +3 -0
- data/lib/parse/api/analytics.rb +2 -2
- data/lib/parse/api/apps.rb +15 -17
- data/lib/parse/api/batch.rb +4 -1
- data/lib/parse/api/cloud_functions.rb +2 -0
- data/lib/parse/api/config.rb +14 -2
- data/lib/parse/api/files.rb +6 -3
- data/lib/parse/api/hooks.rb +4 -4
- data/lib/parse/api/objects.rb +14 -11
- data/lib/parse/api/push.rb +4 -2
- data/lib/parse/api/schemas.rb +6 -5
- data/lib/parse/api/sessions.rb +11 -1
- data/lib/parse/api/users.rb +65 -15
- data/lib/parse/client/authentication.rb +4 -2
- data/lib/parse/client/body_builder.rb +11 -3
- data/lib/parse/client/caching.rb +17 -6
- data/lib/parse/client/protocol.rb +14 -8
- data/lib/parse/client/request.rb +4 -1
- data/lib/parse/client/response.rb +59 -6
- data/lib/parse/client.rb +72 -42
- data/lib/parse/model/acl.rb +22 -4
- data/lib/parse/model/associations/belongs_to.rb +22 -10
- data/lib/parse/model/associations/collection_proxy.rb +14 -1
- data/lib/parse/model/associations/has_many.rb +76 -15
- data/lib/parse/model/associations/has_one.rb +69 -0
- data/lib/parse/model/associations/pointer_collection_proxy.rb +13 -6
- data/lib/parse/model/associations/relation_collection_proxy.rb +5 -2
- data/lib/parse/model/bytes.rb +6 -2
- data/lib/parse/model/classes/installation.rb +27 -0
- data/lib/parse/model/classes/role.rb +20 -0
- data/lib/parse/model/classes/session.rb +26 -0
- data/lib/parse/model/classes/user.rb +185 -0
- data/lib/parse/model/core/actions.rb +40 -26
- data/lib/parse/model/core/properties.rb +126 -20
- data/lib/parse/model/core/querying.rb +63 -3
- data/lib/parse/model/core/schema.rb +9 -6
- data/lib/parse/model/date.rb +5 -1
- data/lib/parse/model/file.rb +12 -9
- data/lib/parse/model/geopoint.rb +6 -4
- data/lib/parse/model/model.rb +29 -21
- data/lib/parse/model/object.rb +29 -76
- data/lib/parse/model/pointer.rb +8 -6
- data/lib/parse/model/push.rb +4 -1
- data/lib/parse/query/constraint.rb +3 -0
- data/lib/parse/query/constraints.rb +6 -3
- data/lib/parse/query/operation.rb +3 -0
- data/lib/parse/query/ordering.rb +3 -0
- data/lib/parse/query.rb +85 -38
- data/lib/parse/stack/generators/rails.rb +3 -0
- data/lib/parse/stack/railtie.rb +2 -0
- data/lib/parse/stack/tasks.rb +4 -1
- data/lib/parse/stack/version.rb +4 -1
- data/lib/parse/stack.rb +3 -0
- data/lib/parse/webhooks/payload.rb +14 -8
- data/lib/parse/webhooks/registration.rb +11 -8
- data/lib/parse/webhooks.rb +11 -8
- data/lib/parse-stack.rb +3 -0
- data/parse-stack.gemspec +10 -8
- metadata +16 -4
data/bin/console
CHANGED
@@ -6,17 +6,8 @@ require 'dotenv'
|
|
6
6
|
require 'byebug'
|
7
7
|
Dotenv.load
|
8
8
|
|
9
|
-
|
10
9
|
Parse.setup
|
11
10
|
|
12
|
-
class Song < Parse::Object
|
13
|
-
property :name
|
14
|
-
|
15
|
-
before_save do
|
16
|
-
self.name = self.name.truncate(60)
|
17
|
-
end
|
18
|
-
|
19
|
-
end
|
20
11
|
# You can add fixtures and/or initialization code here to make experimenting
|
21
12
|
# with your gem easier. You can also use a different console, if you like.
|
22
13
|
|
data/lib/parse/api/all.rb
CHANGED
data/lib/parse/api/analytics.rb
CHANGED
data/lib/parse/api/apps.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
1
3
|
|
2
4
|
module Parse
|
3
5
|
|
@@ -5,29 +7,25 @@ module Parse
|
|
5
7
|
|
6
8
|
module Apps
|
7
9
|
|
8
|
-
|
9
|
-
def fetch_app_keys(appid, email, password)
|
10
|
-
headers
|
11
|
-
|
12
|
-
request :get, "apps/#{appid}", headers: headers
|
10
|
+
APPS_PATH = "apps"
|
11
|
+
def fetch_app_keys(appid, email, password, headers: {})
|
12
|
+
headers.merge!( { Parse::Protocol::EMAIL => email, Parse::Protocol::PASSWORD => password } )
|
13
|
+
request :get, "#{APPS_PATH}/#{appid}", headers: headers
|
13
14
|
end
|
14
15
|
|
15
|
-
def fetch_apps(email, password)
|
16
|
-
headers
|
17
|
-
|
18
|
-
request :get, "apps", headers: headers
|
16
|
+
def fetch_apps(email, password, headers: {})
|
17
|
+
headers.merge!( { Parse::Protocol::EMAIL => email, Parse::Protocol::PASSWORD => password } )
|
18
|
+
request :get, APPS_PATH, headers: headers
|
19
19
|
end
|
20
20
|
|
21
|
-
def create_app(
|
22
|
-
headers
|
23
|
-
|
24
|
-
request :post, "apps", body: opts, headers: headers
|
21
|
+
def create_app(body, email, password, headers: {})
|
22
|
+
headers.merge!( { Parse::Protocol::EMAIL => email, Parse::Protocol::PASSWORD => password } )
|
23
|
+
request :post, APPS_PATH, body: body, headers: headers
|
25
24
|
end
|
26
25
|
|
27
|
-
def update_app(appid,
|
28
|
-
headers
|
29
|
-
|
30
|
-
request :put, "apps/#{appid}", body: opts, headers: headers
|
26
|
+
def update_app(appid, body, email, password, headers: {})
|
27
|
+
headers.merge!( { Parse::Protocol::EMAIL => email, Parse::Protocol::PASSWORD => password } )
|
28
|
+
request :put, "#{APPS_PATH}/#{appid}", body: body, headers: headers
|
31
29
|
end
|
32
30
|
|
33
31
|
|
data/lib/parse/api/batch.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
1
4
|
require 'parallel'
|
2
5
|
require 'active_support'
|
3
6
|
require 'active_support/core_ext'
|
@@ -53,7 +56,7 @@ module Parse
|
|
53
56
|
include Enumerable
|
54
57
|
|
55
58
|
def client
|
56
|
-
@client ||= Parse::Client.
|
59
|
+
@client ||= Parse::Client.client
|
57
60
|
end
|
58
61
|
|
59
62
|
def initialize(reqs = nil)
|
data/lib/parse/api/config.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
1
3
|
|
2
4
|
module Parse
|
3
5
|
|
@@ -5,7 +7,7 @@ module Parse
|
|
5
7
|
#object fetch methods
|
6
8
|
module Config
|
7
9
|
attr_accessor :config
|
8
|
-
|
10
|
+
CONFIG_PATH = "config"
|
9
11
|
def config!
|
10
12
|
@config = nil
|
11
13
|
self.config
|
@@ -13,13 +15,23 @@ module Parse
|
|
13
15
|
|
14
16
|
def config
|
15
17
|
if @config.nil?
|
16
|
-
response = request :get,
|
18
|
+
response = request :get, CONFIG_PATH
|
17
19
|
unless response.error?
|
18
20
|
@config = response.result["params"]
|
19
21
|
end
|
20
22
|
end
|
21
23
|
@config
|
22
24
|
end
|
25
|
+
|
26
|
+
def update_config(params)
|
27
|
+
body = { params: params }
|
28
|
+
response = request :put, CONFIG_PATH, body: body
|
29
|
+
return false if response.error?
|
30
|
+
result = response.result["result"]
|
31
|
+
@config.merge!(params) if result && @config.present?
|
32
|
+
result
|
33
|
+
end
|
34
|
+
|
23
35
|
end
|
24
36
|
|
25
37
|
end
|
data/lib/parse/api/files.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
1
4
|
require 'active_support'
|
2
5
|
require 'active_support/core_ext'
|
3
6
|
|
@@ -6,13 +9,13 @@ module Parse
|
|
6
9
|
module API
|
7
10
|
#object fetch methods
|
8
11
|
module Files
|
9
|
-
|
12
|
+
FILES_PATH = "files"
|
10
13
|
# /1/classes/<className> POST Creating Objects
|
11
14
|
def create_file(fileName, data = {}, content_type = nil)
|
12
15
|
headers = {}
|
13
16
|
headers.merge!( { Parse::Protocol::CONTENT_TYPE => content_type.to_s } ) if content_type.present?
|
14
|
-
response = request :post, "
|
15
|
-
response.parse_class =
|
17
|
+
response = request :post, "#{FILES_PATH}/#{fileName}", body: data, headers: headers
|
18
|
+
response.parse_class = Parse::Model::TYPE_FILE
|
16
19
|
response
|
17
20
|
end
|
18
21
|
|
data/lib/parse/api/hooks.rb
CHANGED
@@ -1,16 +1,16 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
3
|
|
4
4
|
module Parse
|
5
5
|
|
6
6
|
|
7
7
|
module API
|
8
8
|
module Hooks
|
9
|
-
HOOKS_PREFIX = "hooks/"
|
9
|
+
HOOKS_PREFIX = "hooks/"
|
10
10
|
TRIGGER_NAMES = [:beforeSave, :afterSave, :beforeDelete, :afterDelete].freeze
|
11
11
|
def _verify_trigger(triggerName)
|
12
12
|
triggerName = triggerName.to_s.camelize(:lower).to_sym
|
13
|
-
raise "Invalid trigger name #{triggerName}" unless TRIGGER_NAMES.include?(triggerName)
|
13
|
+
raise ArgumentError, "Invalid trigger name #{triggerName}" unless TRIGGER_NAMES.include?(triggerName)
|
14
14
|
triggerName
|
15
15
|
end
|
16
16
|
|
data/lib/parse/api/objects.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
1
4
|
require 'active_support'
|
2
5
|
require 'active_support/core_ext'
|
3
6
|
|
@@ -7,7 +10,7 @@ module Parse
|
|
7
10
|
#object fetch methods
|
8
11
|
module Objects
|
9
12
|
|
10
|
-
CLASS_PATH_PREFIX = "classes/"
|
13
|
+
CLASS_PATH_PREFIX = "classes/"
|
11
14
|
PREFIX_MAP = { installation: "installations", _installation: "installations",
|
12
15
|
user: "users", _user: "users",
|
13
16
|
role: "roles", _role: "roles",
|
@@ -39,36 +42,36 @@ module Parse
|
|
39
42
|
end
|
40
43
|
|
41
44
|
# /1/classes/<className> POST Creating Objects
|
42
|
-
def create_object(className,
|
43
|
-
response = request :post, uri_path(className) , body:
|
45
|
+
def create_object(className, body = {}, headers: {}, **opts)
|
46
|
+
response = request :post, uri_path(className) , body: body, headers: headers, opts: opts
|
44
47
|
response.parse_class = className if response.present?
|
45
48
|
response
|
46
49
|
end
|
47
50
|
|
48
51
|
# /1/classes/<className>/<objectId> DELETE Deleting Objects
|
49
|
-
def delete_object(className, id)
|
50
|
-
response = request :delete, uri_path(className, id)
|
52
|
+
def delete_object(className, id, headers: {}, **opts)
|
53
|
+
response = request :delete, uri_path(className, id), headers: headers, opts: opts
|
51
54
|
response.parse_class = className if response.present?
|
52
55
|
response
|
53
56
|
end
|
54
57
|
|
55
58
|
# /1/classes/<className>/<objectId> GET Retrieving Objects
|
56
|
-
def fetch_object(className, id,
|
57
|
-
response = request :get, uri_path(className, id), opts: opts
|
59
|
+
def fetch_object(className, id, headers: {}, **opts)
|
60
|
+
response = request :get, uri_path(className, id), headers: headers, opts: opts
|
58
61
|
response.parse_class = className if response.present?
|
59
62
|
response
|
60
63
|
end
|
61
64
|
|
62
65
|
# /1/classes/<className> GET Queries
|
63
|
-
def find_objects(className, query = {},
|
64
|
-
response = request :get, uri_path(className), query: query, opts: opts
|
66
|
+
def find_objects(className, query = {}, headers: {}, **opts)
|
67
|
+
response = request :get, uri_path(className), query: query, headers: headers, opts: opts
|
65
68
|
response.parse_class = className if response.present?
|
66
69
|
response
|
67
70
|
end
|
68
71
|
|
69
72
|
# /1/classes/<className>/<objectId> PUT Updating Objects
|
70
|
-
def update_object(className, id,
|
71
|
-
response = request :put, uri_path(className,id) , body:
|
73
|
+
def update_object(className, id, body = {}, headers: {}, **opts)
|
74
|
+
response = request :put, uri_path(className,id) , body: body, headers: headers, opts: opts
|
72
75
|
response.parse_class = className if response.present?
|
73
76
|
response
|
74
77
|
end
|
data/lib/parse/api/push.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
1
3
|
|
2
4
|
module Parse
|
3
5
|
|
4
6
|
module API
|
5
7
|
#object fetch methods
|
6
8
|
module Push
|
7
|
-
|
9
|
+
PUSH_PATH = "push"
|
8
10
|
def push(payload = {})
|
9
|
-
request :post,
|
11
|
+
request :post, PUSH_PATH, body: payload.as_json
|
10
12
|
end
|
11
13
|
|
12
14
|
end
|
data/lib/parse/api/schemas.rb
CHANGED
@@ -1,21 +1,22 @@
|
|
1
|
-
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
2
3
|
|
3
4
|
module Parse
|
4
5
|
|
5
6
|
module API
|
6
7
|
#object fetch methods
|
7
8
|
module Schema
|
8
|
-
|
9
|
+
SCHEMAS_PATH = "schemas"
|
9
10
|
def schema(className)
|
10
|
-
request :get, "#{
|
11
|
+
request :get, "#{SCHEMAS_PATH}/#{className}"
|
11
12
|
end
|
12
13
|
|
13
14
|
def create_schema(className, schema)
|
14
|
-
request :post, "#{
|
15
|
+
request :post, "#{SCHEMAS_PATH}/#{className}", body: schema
|
15
16
|
end
|
16
17
|
|
17
18
|
def update_schema(className, schema)
|
18
|
-
request :put, "#{
|
19
|
+
request :put, "#{SCHEMAS_PATH}/#{className}", body: schema
|
19
20
|
end
|
20
21
|
|
21
22
|
end #Schema
|
data/lib/parse/api/sessions.rb
CHANGED
@@ -1,9 +1,19 @@
|
|
1
|
-
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
2
3
|
|
3
4
|
module Parse
|
4
5
|
|
5
6
|
module API
|
6
7
|
module Sessions
|
8
|
+
SESSION_PATH_PREFIX = "sessions"
|
9
|
+
|
10
|
+
def fetch_session(session_token, **opts)
|
11
|
+
opts.merge!({use_master_key: false, cache: false})
|
12
|
+
headers = {Parse::Protocol::SESSION_TOKEN => session_token}
|
13
|
+
response = request :get, "#{SESSION_PATH_PREFIX}/me", headers: headers, opts: opts
|
14
|
+
response.parse_class = Parse::Model::CLASS_SESSION
|
15
|
+
response
|
16
|
+
end
|
7
17
|
|
8
18
|
end
|
9
19
|
end
|
data/lib/parse/api/users.rb
CHANGED
@@ -1,4 +1,7 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
1
3
|
|
4
|
+
require 'open-uri'
|
2
5
|
|
3
6
|
module Parse
|
4
7
|
|
@@ -6,34 +9,81 @@ module Parse
|
|
6
9
|
module Users
|
7
10
|
# Note that Parse::Objects mainly use the objects.rb API since we can
|
8
11
|
# detect class names to proper URI handlers
|
9
|
-
USER_PATH_PREFIX = "users"
|
10
|
-
|
11
|
-
|
12
|
-
|
12
|
+
USER_PATH_PREFIX = "users"
|
13
|
+
LOGOUT_PATH = "logout"
|
14
|
+
LOGIN_PATH = "login"
|
15
|
+
REQUEST_PASSWORD_RESET = "requestPasswordReset"
|
16
|
+
|
17
|
+
def fetch_user(id, headers: {}, **opts)
|
18
|
+
request :get, "#{USER_PATH_PREFIX}/#{id}", headers: headers, opts: opts
|
19
|
+
end
|
20
|
+
|
21
|
+
def find_users(query = {}, headers: {}, **opts)
|
22
|
+
response = request :get, USER_PATH_PREFIX, query: query, headers: headers, opts: opts
|
23
|
+
response.parse_class = Parse::Model::CLASS_USER
|
24
|
+
response
|
25
|
+
end
|
26
|
+
|
27
|
+
def current_user(session_token, headers: {}, **opts)
|
28
|
+
headers.merge!({Parse::Protocol::SESSION_TOKEN => session_token})
|
29
|
+
response = request :get, "#{USER_PATH_PREFIX}/me", headers: headers, opts: opts
|
30
|
+
response.parse_class = Parse::Model::CLASS_USER
|
31
|
+
response
|
13
32
|
end
|
14
33
|
|
15
|
-
def
|
16
|
-
|
17
|
-
|
34
|
+
def create_user(body, headers: {}, **opts)
|
35
|
+
headers.merge!({ Parse::Protocol::REVOCABLE_SESSION => '1'})
|
36
|
+
if opts[:session_token].present?
|
37
|
+
headers.merge!({ Parse::Protocol::SESSION_TOKEN => opts[:session_token]})
|
38
|
+
end
|
39
|
+
response = request :post, USER_PATH_PREFIX, body: body, headers: headers, opts: opts
|
40
|
+
response.parse_class = Parse::Model::CLASS_USER
|
18
41
|
response
|
19
42
|
end
|
20
43
|
|
21
|
-
def
|
22
|
-
response = request :
|
23
|
-
response.parse_class =
|
44
|
+
def update_user(id, body = {}, headers: {}, **opts)
|
45
|
+
response = request :put, "#{USER_PATH_PREFIX}/#{id}", body: body, opts: opts
|
46
|
+
response.parse_class = Parse::Model::CLASS_USER
|
24
47
|
response
|
25
48
|
end
|
26
49
|
|
27
|
-
|
28
|
-
|
29
|
-
|
50
|
+
# deleting or unlinking is done by setting the authData of the service name to nil
|
51
|
+
def set_service_auth_data(id, service_name, auth_data, headers: {}, **opts)
|
52
|
+
body = { authData: { service_name => auth_data } }
|
53
|
+
update_user(id, body, opts)
|
54
|
+
end
|
55
|
+
|
56
|
+
def delete_user(id, headers: {}, **opts)
|
57
|
+
request :delete, "#{USER_PATH_PREFIX}/#{id}", headers: headers, opts: opts
|
58
|
+
end
|
59
|
+
|
60
|
+
def request_password_reset(email, **opts)
|
61
|
+
body = {email: email}
|
62
|
+
request :post, REQUEST_PASSWORD_RESET, body: body, opts: opts
|
63
|
+
end
|
64
|
+
|
65
|
+
def login(username, password, headers: {}, **opts)
|
66
|
+
# Probably pass Installation-ID as header
|
67
|
+
query = { username: username, password: password }
|
68
|
+
headers.merge!({ Parse::Protocol::REVOCABLE_SESSION => '1'})
|
69
|
+
# headers.merge!( { Parse::Protocol::INSTALLATION_ID => ''} )
|
70
|
+
response = request :get, LOGIN_PATH, query: query, headers: headers, opts: opts
|
71
|
+
response.parse_class = Parse::Model::CLASS_USER
|
30
72
|
response
|
31
73
|
end
|
32
74
|
|
33
|
-
def
|
34
|
-
|
75
|
+
def logout(session_token, headers: {}, **opts)
|
76
|
+
headers.merge!({ Parse::Protocol::SESSION_TOKEN => session_token})
|
77
|
+
opts.merge!({use_master_key: false, session_token: session_token})
|
78
|
+
request :post, LOGOUT_PATH, headers: headers, opts: opts
|
35
79
|
end
|
36
80
|
|
81
|
+
# {username: "", password: "", email: nil} # minimum
|
82
|
+
def signup(username, password, email = nil, body: {}, **opts)
|
83
|
+
body = body.merge({ username: username, password: password })
|
84
|
+
body[:email] = email || body[:email]
|
85
|
+
create_user(body, opts)
|
86
|
+
end
|
37
87
|
|
38
88
|
|
39
89
|
end # Users
|
@@ -1,3 +1,6 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
1
4
|
require 'faraday'
|
2
5
|
require 'faraday_middleware'
|
3
6
|
require 'active_support'
|
@@ -34,10 +37,9 @@ module Parse
|
|
34
37
|
def call!(env)
|
35
38
|
# We add the main Parse protocol headers
|
36
39
|
headers = {}
|
37
|
-
raise "No Parse Application Id specified for authentication." unless @application_id.present?
|
40
|
+
raise ArgumentError, "No Parse Application Id specified for authentication." unless @application_id.present?
|
38
41
|
headers[APP_ID] = @application_id
|
39
42
|
headers[API_KEY] = @api_key unless @api_key.blank?
|
40
|
-
|
41
43
|
unless @master_key.blank? || env[:request_headers][DISABLE_MASTER_KEY].present?
|
42
44
|
headers[MASTER_KEY] = @master_key
|
43
45
|
end
|
@@ -1,3 +1,6 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
1
4
|
require 'faraday'
|
2
5
|
require 'faraday_middleware'
|
3
6
|
require_relative 'response'
|
@@ -15,7 +18,7 @@ module Parse
|
|
15
18
|
end
|
16
19
|
|
17
20
|
include Parse::Protocol
|
18
|
-
HTTP_OVERRIDE = 'X-Http-Method-Override'
|
21
|
+
HTTP_OVERRIDE = 'X-Http-Method-Override'
|
19
22
|
|
20
23
|
# thread-safety
|
21
24
|
def call(env)
|
@@ -29,7 +32,7 @@ module Parse
|
|
29
32
|
# The standard maximum POST request (which is a server setting), is usually set to 20MBs
|
30
33
|
if env[:method] == :get && env[:url].to_s.length > 2_000
|
31
34
|
env[:request_headers][HTTP_OVERRIDE] = 'GET'
|
32
|
-
env[:request_headers][CONTENT_TYPE] = 'application/x-www-form-urlencoded'
|
35
|
+
env[:request_headers][CONTENT_TYPE] = 'application/x-www-form-urlencoded'
|
33
36
|
env[:body] = env[:url].query
|
34
37
|
env[:url].query = nil
|
35
38
|
#override
|
@@ -42,6 +45,11 @@ module Parse
|
|
42
45
|
|
43
46
|
if self.class.logging
|
44
47
|
puts "[Request #{env.method.upcase}] #{env[:url]}"
|
48
|
+
env[:request_headers].each do |k,v|
|
49
|
+
next if k == Parse::Protocol::MASTER_KEY
|
50
|
+
puts "[Header] #{k} : #{v}"
|
51
|
+
end
|
52
|
+
|
45
53
|
puts "[Request Body] #{env[:body]}"
|
46
54
|
end
|
47
55
|
@app.call(env).on_complete do |response_env|
|
@@ -56,7 +64,7 @@ module Parse
|
|
56
64
|
|
57
65
|
begin
|
58
66
|
r = Parse::Response.new(response_env.body)
|
59
|
-
rescue
|
67
|
+
rescue => e
|
60
68
|
r = Parse::Response.new
|
61
69
|
r.code = response_env.status
|
62
70
|
r.error = "Invalid response for #{env[:method]} #{env[:url]}: #{e}"
|
data/lib/parse/client/caching.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
1
4
|
require 'faraday'
|
2
5
|
require 'faraday_middleware'
|
3
6
|
require 'moneta'
|
@@ -5,7 +8,7 @@ require_relative 'protocol'
|
|
5
8
|
# This is a caching middleware for Parse queries using Moneta.
|
6
9
|
module Parse
|
7
10
|
module Middleware
|
8
|
-
class CachingError <
|
11
|
+
class CachingError < StandardError; end;
|
9
12
|
class Caching < Faraday::Middleware
|
10
13
|
include Parse::Protocol
|
11
14
|
# Cache-Control: no-cache
|
@@ -17,9 +20,9 @@ module Parse
|
|
17
20
|
# * 302 - 'Found'
|
18
21
|
# * 404 - 'Not Found' - removed
|
19
22
|
# * 410 - 'Gone' - removed
|
20
|
-
CACHEABLE_HTTP_CODES = [200, 203, 300, 301, 302]
|
21
|
-
CACHE_CONTROL = 'Cache-Control'
|
22
|
-
CACHE_EXPIRES_DURATION = 'X-Parse-Stack-Cache-Expires'
|
23
|
+
CACHEABLE_HTTP_CODES = [200, 203, 300, 301, 302]
|
24
|
+
CACHE_CONTROL = 'Cache-Control'
|
25
|
+
CACHE_EXPIRES_DURATION = 'X-Parse-Stack-Cache-Expires'
|
23
26
|
|
24
27
|
class << self
|
25
28
|
attr_accessor :enabled, :logging
|
@@ -60,7 +63,7 @@ module Parse
|
|
60
63
|
# get default caching state
|
61
64
|
@enabled = self.class.enabled
|
62
65
|
# disable cache for this request if "no-cache" was passed
|
63
|
-
if @request_headers[CACHE_CONTROL] == "no-cache"
|
66
|
+
if @request_headers[CACHE_CONTROL] == "no-cache"
|
64
67
|
@enabled = false
|
65
68
|
end
|
66
69
|
|
@@ -80,6 +83,14 @@ module Parse
|
|
80
83
|
url = env.url
|
81
84
|
method = env.method
|
82
85
|
@cache_key = url.to_s
|
86
|
+
|
87
|
+
if @request_headers.key?(SESSION_TOKEN)
|
88
|
+
session_token = @request_headers[SESSION_TOKEN]
|
89
|
+
@cache_key = "#{session_token}#{@cache_key}" # prefix tokens
|
90
|
+
elsif @request_headers.key?(MASTER_KEY)
|
91
|
+
@cache_key = "mk:#{@cache_key}" # prefix for master key requests
|
92
|
+
end
|
93
|
+
|
83
94
|
begin
|
84
95
|
if method == :get && @cache_key.present? && @store.key?(@cache_key)
|
85
96
|
puts("[Parse::Cache::Hit] >> #{url}") if self.class.logging.present?
|
@@ -111,7 +122,7 @@ module Parse
|
|
111
122
|
# Only cache GET requests with valid HTTP status codes whose content-length
|
112
123
|
# is greater than 20. Otherwise they could be errors, successes and empty result sets.
|
113
124
|
if @enabled && method == :get && CACHEABLE_HTTP_CODES.include?(response_env.status) &&
|
114
|
-
response_env.present? && response_env.response_headers["content-length"
|
125
|
+
response_env.present? && response_env.response_headers["content-length"].to_i > 20
|
115
126
|
@store.store(@cache_key, response_env, expires: @expires) # ||= response_env.body
|
116
127
|
end # if
|
117
128
|
# do something with the response
|
@@ -1,16 +1,22 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
1
3
|
|
2
4
|
# A module to contain all the main constants.
|
3
5
|
module Parse
|
4
6
|
|
5
7
|
module Protocol
|
6
|
-
HOST
|
7
|
-
SERVER_URL
|
8
|
-
APP_ID
|
9
|
-
API_KEY
|
10
|
-
MASTER_KEY
|
11
|
-
SESSION_TOKEN
|
12
|
-
|
13
|
-
|
8
|
+
HOST = 'api.parse.com'
|
9
|
+
SERVER_URL = 'https://api.parse.com/1/'
|
10
|
+
APP_ID = 'X-Parse-Application-Id'
|
11
|
+
API_KEY = 'X-Parse-REST-API-Key'
|
12
|
+
MASTER_KEY = 'X-Parse-Master-Key'
|
13
|
+
SESSION_TOKEN = 'X-Parse-Session-Token'
|
14
|
+
REVOCABLE_SESSION = 'X-Parse-Revocable-Session'
|
15
|
+
EMAIL = 'X-Parse-Email'
|
16
|
+
PASSWORD = 'X-Parse-Password'
|
17
|
+
INSTALLATION_ID = 'Parse-Installation-Id'
|
18
|
+
CONTENT_TYPE = 'Content-Type'
|
19
|
+
CONTENT_TYPE_FORMAT = 'application/json; charset=utf-8'
|
14
20
|
end
|
15
21
|
|
16
22
|
end
|
data/lib/parse/client/request.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
1
4
|
require 'active_support'
|
2
5
|
require 'active_support/json'
|
3
6
|
|
@@ -11,7 +14,7 @@ module Parse
|
|
11
14
|
@tag = 0
|
12
15
|
method = method.downcase.to_sym
|
13
16
|
unless method == :get || method == :put || method == :post || method == :delete
|
14
|
-
raise "Invalid method #{method} for request : '#{uri}'"
|
17
|
+
raise ArgumentError, "Invalid method #{method} for request : '#{uri}'"
|
15
18
|
end
|
16
19
|
self.method = method
|
17
20
|
self.path = uri
|