Module: Opium::Model::Connectable::ClassMethods
- Defined in:
- lib/opium/model/connectable.rb
Instance Method Summary collapse
- #as_resource(name, &block) ⇒ Object
- #connection ⇒ Object
- #http_delete(id, options = {}) ⇒ Object
- #http_get(options = {}) ⇒ Object
- #http_post(data, options = {}) ⇒ Object
- #http_put(id, data, options = {}) ⇒ Object
-
#no_object_prefix! ⇒ Object
Parse doesn’t route User objects through /classes/, instead treating them as a top-level class.
- #object_prefix ⇒ Object
- #requires_heightened_privileges! ⇒ Object
- #requires_heightened_privileges? ⇒ Boolean (also: #has_heightened_privileges?)
- #reset_connection! ⇒ Object
- #resource_name(resource_id = nil) ⇒ Object
- #with_heightened_privileges(&block) ⇒ Object
Instance Method Details
#as_resource(name, &block) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/opium/model/connectable.rb', line 47 def as_resource( name, &block ) fail ArgumentError, 'no block given' unless block_given? @masked_resource_name = name.to_s.freeze block.call ensure @masked_resource_name = nil end |
#connection ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/opium/model/connectable.rb', line 22 def connection @@connection ||= Faraday.new( url: 'https://api.parse.com/1/' ) do |faraday| faraday.request :multipart faraday.request :url_encoded faraday.request :json faraday.response :logger if Opium.config.log_network_responses faraday.response :json, content_type: /\bjson$/ faraday.headers[:x_parse_application_id] = Opium.config.app_id faraday.adapter Faraday.default_adapter end end |
#http_delete(id, options = {}) ⇒ Object
77 78 79 |
# File 'lib/opium/model/connectable.rb', line 77 def http_delete( id, = {} ) http( :delete, deeply_merge( , id: id ) ) end |
#http_get(options = {}) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/opium/model/connectable.rb', line 61 def http_get( = {} ) http( :get, ) do |request| .fetch(:query, {}).each do |key, value| request.params[key] = key.to_s == 'where' ? value.to_json : value end end end |
#http_post(data, options = {}) ⇒ Object
69 70 71 |
# File 'lib/opium/model/connectable.rb', line 69 def http_post( data, = {} ) http( :post, deeply_merge( , content_type_json ), &infuse_request_with( data ) ) end |
#http_put(id, data, options = {}) ⇒ Object
73 74 75 |
# File 'lib/opium/model/connectable.rb', line 73 def http_put( id, data, = {} ) http( :put, deeply_merge( , content_type_json, id: id ), &infuse_request_with( data ) ) end |
#no_object_prefix! ⇒ Object
Parse doesn’t route User objects through /classes/, instead treating them as a top-level class.
43 44 45 |
# File 'lib/opium/model/connectable.rb', line 43 def no_object_prefix! @object_prefix = '' end |
#object_prefix ⇒ Object
38 39 40 |
# File 'lib/opium/model/connectable.rb', line 38 def object_prefix @object_prefix ||= 'classes' end |
#requires_heightened_privileges! ⇒ Object
81 82 83 |
# File 'lib/opium/model/connectable.rb', line 81 def requires_heightened_privileges! @requires_heightened_privileges = true end |
#requires_heightened_privileges? ⇒ Boolean Also known as: has_heightened_privileges?
85 86 87 |
# File 'lib/opium/model/connectable.rb', line 85 def requires_heightened_privileges? !@requires_heightened_privileges.nil? end |
#reset_connection! ⇒ Object
34 35 36 |
# File 'lib/opium/model/connectable.rb', line 34 def reset_connection! @@connection = nil end |
#resource_name(resource_id = nil) ⇒ Object
55 56 57 58 59 |
# File 'lib/opium/model/connectable.rb', line 55 def resource_name( resource_id = nil ) return @masked_resource_name if @masked_resource_name @resource_name ||= Pathname.new( object_prefix ).join( map_name_to_resource( model_name ) ) ( resource_id ? @resource_name.join( resource_id ) : @resource_name ).to_s end |
#with_heightened_privileges(&block) ⇒ Object
91 92 93 94 95 96 |
# File 'lib/opium/model/connectable.rb', line 91 def with_heightened_privileges(&block) previous, @requires_heightened_privileges = @requires_heightened_privileges, true block.call if block_given? ensure @requires_heightened_privileges = previous end |