Class: Parse::Client
- Inherits:
-
Object
- Object
- Parse::Client
- Defined in:
- lib/parse/client.rb
Constant Summary collapse
- API_SERVER =
'api.parse.com'- API_VERSION =
1- @@default_client =
nil
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#application_id ⇒ Object
Returns the value of attribute application_id.
-
#http_client ⇒ Object
Returns the value of attribute http_client.
-
#master_key ⇒ Object
Returns the value of attribute master_key.
-
#session_token ⇒ Object
Returns the value of attribute session_token.
Class Method Summary collapse
Instance Method Summary collapse
- #build_headers(opt_headers = {}) ⇒ Object
- #call_api(method, endpoint, body = nil, opt_headers = {}, &block) ⇒ Object
- #call_function(name, param) ⇒ Object
- #canonicalize_endpoint(endpoint) ⇒ Object
- #create(parse_class_name, values, &block) ⇒ Object
- #delete(parse_class_name, parse_object_id, &block) ⇒ Object
- #dry_run(&block) ⇒ Object
- #dry_run! ⇒ Object
- #dry_run=(val) ⇒ Object
- #dry_run? ⇒ Boolean
- #find(parse_class_name, object_id_or_conditions, opts = {}) ⇒ Object
- #find_by_id(parse_class_name, object_id, opts = {}) ⇒ Object
- #find_by_query(parse_class_name, conditions) ⇒ Object
-
#initialize(application_id = nil, api_key = nil, master_key = nil, http_client = nil) ⇒ Client
constructor
A new instance of Client.
- #log_in(username, password, &block) ⇒ Object
- #log_out ⇒ Object
- #method_missing(name, *args, &block) ⇒ Object
- #sign_up(username, password, opts = {}, &block) ⇒ Object
- #update(parse_class_name, parse_object_id, values, &block) ⇒ Object
- #use_master_key(&block) ⇒ Object
- #use_master_key! ⇒ Object
- #use_master_key=(val) ⇒ Object
Constructor Details
#initialize(application_id = nil, api_key = nil, master_key = nil, http_client = nil) ⇒ Client
Returns a new instance of Client.
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/parse/client.rb', line 18 def initialize application_id=nil, api_key=nil, master_key=nil, http_client=nil @application_id = application_id || Parse.application_id @api_key = api_key || Parse.api_key @master_key = master_key || Parse.master_key if @application_id.nil? || @api_key.nil? raise ArgumentError.new " Both Application ID and API Key must be set.\n ex. Parse.credentials application_id: APPLICATION_ID, api_key: API_KEY\n EOS\n end\n @http_client = http_client || Parse::HttpClient.new(API_SERVER)\nend\n".gsub(/^ +/, '') |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
221 222 223 |
# File 'lib/parse/client.rb', line 221 def method_missing name, *args, &block call_function name, args.first end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
8 9 10 |
# File 'lib/parse/client.rb', line 8 def api_key @api_key end |
#application_id ⇒ Object
Returns the value of attribute application_id.
8 9 10 |
# File 'lib/parse/client.rb', line 8 def application_id @application_id end |
#http_client ⇒ Object
Returns the value of attribute http_client.
8 9 10 |
# File 'lib/parse/client.rb', line 8 def http_client @http_client end |
#master_key ⇒ Object
Returns the value of attribute master_key.
8 9 10 |
# File 'lib/parse/client.rb', line 8 def master_key @master_key end |
#session_token ⇒ Object
Returns the value of attribute session_token.
8 9 10 |
# File 'lib/parse/client.rb', line 8 def session_token @session_token end |
Class Method Details
.default ⇒ Object
10 11 12 |
# File 'lib/parse/client.rb', line 10 def self.default @@default_client ||= new end |
.default=(default_client) ⇒ Object
14 15 16 |
# File 'lib/parse/client.rb', line 14 def self.default= default_client @@default_client = default_client end |
Instance Method Details
#build_headers(opt_headers = {}) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/parse/client.rb', line 49 def build_headers opt_headers={} headers = { 'X-Parse-Application-Id' => @application_id, 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'User-Agent' => 'A parse.com client for ruby' } if @use_master_key headers['X-Parse-Master-Key'] = @master_key else headers['X-Parse-REST-API-Key'] = @api_key end headers['X-Parse-Session-Token'] = @session_token if @session_token headers.update(opt_headers).delete_if {|k, v| v.nil?} end |
#call_api(method, endpoint, body = nil, opt_headers = {}, &block) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/parse/client.rb', line 39 def call_api method, endpoint, body=nil, opt_headers={}, &block endpoint = "/#{API_VERSION}/#{endpoint}" unless endpoint[0] == '/' endpoint = canonicalize_endpoint endpoint headers = build_headers opt_headers if body.is_a?(Hash) body = Hash[*(body.to_a.map{|k, v| [k, URI.encode(v.to_s)]}.flatten)].to_json end @http_client.request method, endpoint, headers, body, &block end |
#call_function(name, param) ⇒ Object
154 155 156 157 158 159 160 161 162 163 |
# File 'lib/parse/client.rb', line 154 def call_function name, param func_name = Parse.auto_snake_case ? name.to_s.gsub(/_([a-z])/) {$1.upcase} : name call_api :post, "functions/#{func_name}", param.to_json do |resp_body| if resp_body.has_key? 'result' resp_body['result'] else raise StandartError.new 'unknown error' end end end |
#canonicalize_endpoint(endpoint) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/parse/client.rb', line 31 def canonicalize_endpoint endpoint if endpoint =~ %r</#{API_VERSION}/classes/_(User|Role|Installation)> endpoint.sub %r</#{API_VERSION}/classes/_(User|Role|Installation)>, "/#{API_VERSION}/#{$1.downcase}s" else endpoint end end |
#create(parse_class_name, values, &block) ⇒ Object
142 143 144 |
# File 'lib/parse/client.rb', line 142 def create parse_class_name, values, &block call_api :post, "classes/#{parse_class_name}", values.to_json, &block end |
#delete(parse_class_name, parse_object_id, &block) ⇒ Object
150 151 152 |
# File 'lib/parse/client.rb', line 150 def delete parse_class_name, parse_object_id, &block call_api :delete, "classes/#{parse_class_name}/#{parse_object_id}", &block end |
#dry_run(&block) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/parse/client.rb', line 208 def dry_run &block return dry_run? unless block tmp = dry_run? dry_run! begin block.call ensure self.dry_run = tmp end nil end |
#dry_run! ⇒ Object
200 201 202 |
# File 'lib/parse/client.rb', line 200 def dry_run! @http_client.dry_run! end |
#dry_run=(val) ⇒ Object
204 205 206 |
# File 'lib/parse/client.rb', line 204 def dry_run= val @http_client = val end |
#dry_run? ⇒ Boolean
196 197 198 |
# File 'lib/parse/client.rb', line 196 def dry_run? @http_client.dry_run? end |
#find(parse_class_name, object_id_or_conditions, opts = {}) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/parse/client.rb', line 78 def find parse_class_name, object_id_or_conditions, opts={} case object_id_or_conditions when :all find_by_query parse_class_name, opts when String, Symbol find_by_id parse_class_name, object_id_or_conditions, opts when Hash find_by_query parse_class_name, object_id_or_conditions else raise ArgumentError.new('the first argument should be a string, a symbol, or a hash.') end end |
#find_by_id(parse_class_name, object_id, opts = {}) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/parse/client.rb', line 91 def find_by_id parse_class_name, object_id, opts={} call_api :get, "classes/#{parse_class_name}/#{object_id}" do |resp_body| if opts.has_key? :include included_keys = [opts[:include]].flatten included_keys.each do |included_key| resp_body[included_key].tap do |pointer| if pointer['__type'] == 'Pointer' pointer['body'] = self.find_by_id pointer['className'], pointer['objectId'] else raise ArgumentError.new('included column should be a pointer.') end end end end resp_body end end |
#find_by_query(parse_class_name, conditions) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/parse/client.rb', line 109 def find_by_query parse_class_name, conditions query = Query.new parse_class_name, self query.limit conditions[:limit] if conditions.has_key? :limit query.skip conditions[:skip] if conditions.has_key? :skip query.count conditions[:count] if conditions.has_key? :count if conditions.has_key? :order order = conditions[:order] order = [order] unless order.is_a? Array query.order order end if conditions.has_key? :keys keys = conditions[:keys] keys = [keys] unless keys.is_a? Array query.keys keys end if conditions.has_key? :include include = conditions[:include] include = [include] unless include.is_a? Array query.include include end if conditions.has_key? :where case condition = conditions[:where] when Hash query.where condition when Proc query.where condition else raise 'wrong condition' end end query.invoke end |
#log_in(username, password, &block) ⇒ Object
69 70 71 72 |
# File 'lib/parse/client.rb', line 69 def log_in username, password, &block call_api :get, "login?username=#{URI.encode username}&password=#{ URI.encode password}", nil, &block end |
#log_out ⇒ Object
74 75 76 |
# File 'lib/parse/client.rb', line 74 def log_out @session_token = nil end |
#sign_up(username, password, opts = {}, &block) ⇒ Object
65 66 67 |
# File 'lib/parse/client.rb', line 65 def sign_up username, password, opts={}, &block call_api :post, 'users', {'username' => username, 'password' => password}.update(opts || {}), &block end |
#update(parse_class_name, parse_object_id, values, &block) ⇒ Object
146 147 148 |
# File 'lib/parse/client.rb', line 146 def update parse_class_name, parse_object_id, values, &block call_api :put, "classes/#{parse_class_name}/#{parse_object_id}", values.to_json, &block end |
#use_master_key(&block) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/parse/client.rb', line 174 def use_master_key &block return @use_master_key unless block tmp, @use_master_key = @use_master_key, true begin ret = block.call ensure @use_master_key = tmp end ret end |
#use_master_key! ⇒ Object
165 166 167 |
# File 'lib/parse/client.rb', line 165 def use_master_key! self.use_master_key = true end |
#use_master_key=(val) ⇒ Object
169 170 171 172 |
# File 'lib/parse/client.rb', line 169 def use_master_key= val raise ArgumentError.new('master_key is not set.') if val && !@master_key @use_master_key = val end |