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
-
#http_client ⇒ Object
Returns the value of attribute http_client.
-
#session_token ⇒ Object
Returns the value of attribute session_token.
Class Method Summary collapse
Instance Method Summary collapse
- #call_api(method, endpoint, body = nil, opt_headers = {}, &block) ⇒ Object
- #call_function(name, param) ⇒ Object
- #create(parse_object, values) ⇒ Object
- #delete(parse_object) ⇒ Object
- #find(parse_class, object_id_or_conditions, opts = {}) ⇒ Object
- #find_by_id(parse_class, object_id, opts = {}) ⇒ Object
- #find_by_query(parse_class, conditions) ⇒ Object
-
#initialize(application_id = nil, api_key = nil, http_client = nil) ⇒ Client
constructor
A new instance of Client.
- #log_in(username, password, &block) ⇒ Object
- #method_missing(name, *args, &block) ⇒ Object
- #sign_up(username, password, opts = {}, &block) ⇒ Object
- #update(parse_object, values) ⇒ Object
Constructor Details
#initialize(application_id = nil, api_key = nil, http_client = nil) ⇒ Client
Returns a new instance of Client.
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/parse/client.rb', line 14 def initialize application_id=nil, api_key=nil, http_client=nil @application_id = application_id || Parse.application_id @api_key = api_key || Parse.api_key if @application_id.nil? || @api_key.nil? raise ArgumentError.new <<-EOS Both Application ID and API Key must be set. ex. Parse.credentials application_id: APPLICATION_ID, api_key: API_KEY EOS end @http_client = http_client || Parse::HttpClient.new(API_SERVER) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
137 138 139 |
# File 'lib/parse/client.rb', line 137 def method_missing name, *args, &block call_function name, args.first end |
Instance Attribute Details
#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 |
#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_client ⇒ Object
10 11 12 |
# File 'lib/parse/client.rb', line 10 def self.default_client @@default_client ||= new end |
Instance Method Details
#call_api(method, endpoint, body = nil, opt_headers = {}, &block) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/parse/client.rb', line 26 def call_api method, endpoint, body=nil, opt_headers={}, &block endpoint = "/#{API_VERSION}/#{endpoint}" unless endpoint[0] == '/' headers = { 'X-Parse-Application-Id' => @application_id, 'X-Parse-REST-API-Key' => @api_key, 'Content-Type' => 'application/json' } headers.update('X-Parse-Session-Token' => @session_token) if @session_token headers.update opt_headers if body.is_a?(Hash) body = Hash[*(body.to_a.map{|k, v| [k, URI.encode(v)]}.flatten)].to_json end @http_client.request method, endpoint, headers, body, &block end |
#call_function(name, param) ⇒ Object
126 127 128 129 130 131 132 133 134 135 |
# File 'lib/parse/client.rb', line 126 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 |
#create(parse_object, values) ⇒ Object
108 109 110 111 112 |
# File 'lib/parse/client.rb', line 108 def create parse_object, values call_api :post, "classes/#{parse_object.parse_class_name}", values.to_json do |resp_body| resp_body end end |
#delete(parse_object) ⇒ Object
120 121 122 123 124 |
# File 'lib/parse/client.rb', line 120 def delete parse_object call_api :delete, "classes/#{parse_object.parse_class_name}/#{parse_object.object_id}" do |resp_body| resp_body end end |
#find(parse_class, object_id_or_conditions, opts = {}) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/parse/client.rb', line 50 def find parse_class, object_id_or_conditions, opts={} if object_id_or_conditions.is_a? String find_by_id parse_class, object_id_or_conditions, opts elsif object_id_or_conditions.is_a? Hash find_by_query parse_class, object_id_or_conditions end end |
#find_by_id(parse_class, object_id, opts = {}) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/parse/client.rb', line 58 def find_by_id parse_class, object_id, opts={} call_api :get, "classes/#{parse_class.parse_class_name}/#{object_id}" do |resp_body| convert parse_class, resp_body if opts.has_key? :include included_keys = opts[:include] included_keys = [included_keys] unless included_keys.is_a? Enumerable included_keys.each do |included_key| pointer = resp_body[included_key] pointer.load end end parse_class.new resp_body end end |
#find_by_query(parse_class, conditions) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/parse/client.rb', line 75 def find_by_query parse_class, conditions query = Query.new parse_class 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
45 46 47 48 |
# File 'lib/parse/client.rb', line 45 def log_in username, password, &block call_api :get, "login?username=#{URI.encode username}&password=#{ URI.encode password}", nil, &block end |
#sign_up(username, password, opts = {}, &block) ⇒ Object
41 42 43 |
# File 'lib/parse/client.rb', line 41 def sign_up username, password, opts={}, &block call_api :post, 'users', {'username' => username, 'password' => password}.update(opts || {}), &block end |
#update(parse_object, values) ⇒ Object
114 115 116 117 118 |
# File 'lib/parse/client.rb', line 114 def update parse_object, values call_api :put, "classes/#{parse_object.parse_class_name}/#{parse_object.object_id}", values.to_json do |resp_body| resp_body end end |