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.
- #no_really_i_need_master! ⇒ Object
- #object_prefix ⇒ Object
- #parse_server_url ⇒ 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
51 52 53 54 55 56 57 |
# File 'lib/opium/model/connectable.rb', line 51 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
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/opium/model/connectable.rb', line 26 def connection @@connection ||= Faraday.new( url: parse_server_url ) 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
81 82 83 |
# File 'lib/opium/model/connectable.rb', line 81 def http_delete( id, = {} ) http( :delete, deeply_merge( , id: id ) ) end |
#http_get(options = {}) ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/opium/model/connectable.rb', line 65 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
73 74 75 |
# File 'lib/opium/model/connectable.rb', line 73 def http_post( data, = {} ) http( :post, deeply_merge( , content_type_json ), &infuse_request_with( data ) ) end |
#http_put(id, data, options = {}) ⇒ Object
77 78 79 |
# File 'lib/opium/model/connectable.rb', line 77 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.
47 48 49 |
# File 'lib/opium/model/connectable.rb', line 47 def no_object_prefix! @object_prefix = '' end |
#no_really_i_need_master! ⇒ Object
102 103 104 |
# File 'lib/opium/model/connectable.rb', line 102 def no_really_i_need_master! @always_heightened_privileges = true end |
#object_prefix ⇒ Object
42 43 44 |
# File 'lib/opium/model/connectable.rb', line 42 def object_prefix @object_prefix ||= 'classes' end |
#parse_server_url ⇒ Object
22 23 24 |
# File 'lib/opium/model/connectable.rb', line 22 def parse_server_url ::URI.join( ::Opium.config.server_url, ::Opium.config.mount_point ).to_s end |
#requires_heightened_privileges! ⇒ Object
85 86 87 |
# File 'lib/opium/model/connectable.rb', line 85 def requires_heightened_privileges! @requires_heightened_privileges = true end |
#requires_heightened_privileges? ⇒ Boolean Also known as: has_heightened_privileges?
89 90 91 |
# File 'lib/opium/model/connectable.rb', line 89 def requires_heightened_privileges? !@requires_heightened_privileges.nil? end |
#reset_connection! ⇒ Object
38 39 40 |
# File 'lib/opium/model/connectable.rb', line 38 def reset_connection! @@connection = nil end |
#resource_name(resource_id = nil) ⇒ Object
59 60 61 62 63 |
# File 'lib/opium/model/connectable.rb', line 59 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
95 96 97 98 99 100 |
# File 'lib/opium/model/connectable.rb', line 95 def with_heightened_privileges(&block) previous, @requires_heightened_privileges = @requires_heightened_privileges, true block.call if block_given? ensure @requires_heightened_privileges = previous end |