Module: ContextIO::API::Resource
- Included in:
- Lite::EmailAccount, Lite::Folder, Lite::Message, Lite::User, Lite::Webhook
- Defined in:
- lib/contextio/api/resource.rb
Overview
When ‘include`d into a class, this module provides some helper methods for various things a singular resource will need or find useful.
Defined Under Namespace
Modules: DeclarativeClassSyntax
Instance Attribute Summary collapse
-
#api ⇒ Object
readonly
Handle for the ‘API` instance.
-
#api_attributes ⇒ {String => Numeric, String, Hash, Array, Boolean}
readonly
The attributes returned from the API as a Hash.
-
#primary_key ⇒ String, Symbol
readonly
The name of the key used to build the resource URL.
-
#resource_url ⇒ String
readonly
The URL that will fetch attributes from the API.
-
#with_constraints ⇒ Object
readonly
Returns the value of attribute with_constraints.
Instance Method Summary collapse
-
#delete ⇒ Boolean
Deletes the resource.
-
#initialize(api, options = {}) ⇒ Object
For internal use only.
- #with(constraints) ⇒ Object
Instance Attribute Details
#api ⇒ Object (readonly)
Handle for the ‘API` instance. For internal use only.
9 10 11 |
# File 'lib/contextio/api/resource.rb', line 9 def api @api end |
#api_attributes ⇒ {String => Numeric, String, Hash, Array, Boolean} (readonly)
Returns The attributes returned from the API as a Hash. If it hasn’t been populated, it will ask the API and populate it.
66 67 68 |
# File 'lib/contextio/api/resource.rb', line 66 def api_attributes @api_attributes ||= fetch_attributes end |
#primary_key ⇒ String, Symbol (readonly)
Returns The name of the key used to build the resource URL.
73 74 75 |
# File 'lib/contextio/api/resource.rb', line 73 def primary_key self.class.primary_key end |
#resource_url ⇒ String (readonly)
Returns The URL that will fetch attributes from the API.
51 52 53 |
# File 'lib/contextio/api/resource.rb', line 51 def resource_url @resource_url ||= api.url_for(self) end |
#with_constraints ⇒ Object (readonly)
Returns the value of attribute with_constraints.
13 14 15 |
# File 'lib/contextio/api/resource.rb', line 13 def with_constraints @with_constraints end |
Instance Method Details
#delete ⇒ Boolean
Deletes the resource.
58 59 60 |
# File 'lib/contextio/api/resource.rb', line 58 def delete api.request(:delete, resource_url)['success'] end |
#initialize(api, options = {}) ⇒ Object
For internal use only. Users of this gem shouldn’t be calling this directly.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/contextio/api/resource.rb', line 23 def initialize(api, = {}) @options ||= @with_constraints = @options[:with] || {} @api = api @options.each do |key, value| key = key.to_s.gsub('-', '_') if self.class.associations.include?(key.to_sym) && value.is_a?(Array) association_class = ContextIO::API::AssociationHelpers.class_for_association_name(key.to_sym) value = association_class.new(api, self.class.association_name => self, attribute_hashes: value) end instance_variable_set("@#{key}", value) unless self.respond_to?(key) define_singleton_method(key) do instance_variable_get("@#{key}") end end end end |
#with(constraints) ⇒ Object
78 79 80 81 |
# File 'lib/contextio/api/resource.rb', line 78 def with(constraints) constraints.each{|i,c| constraints[i] = (c ? 1 : 0) if c == !!c } self.class.new(api, @options.merge(with: with_constraints.merge(constraints))) end |