class Camper::Resource
Attributes
data[R]
hash[R]
Public Class Methods
create(hash)
click to toggle source
# File lib/camper/resource.rb, line 35 def self.create(hash) klass = detect_type(hash["url"]) return klass.new(hash) end
new(hash)
click to toggle source
Creates a new Resource
object.
# File lib/camper/resource.rb, line 7 def initialize(hash) @hash = hash @data = resourcify_data end
Private Class Methods
detect_type(url)
click to toggle source
# File lib/camper/resource.rb, line 70 def self.detect_type(url) case url when /#{Configuration.base_api_endpoint}\/\d+\/projects\/\d+\.json/ return Project else return Resource end end
Public Instance Methods
[](key)
click to toggle source
# File lib/camper/resource.rb, line 23 def [](key) data[key] end
can_be_commented?()
click to toggle source
Check whether a resource can be commented on or not given the presences of `comments_count` and `comments_url` keys
@return [Boolean]
# File lib/camper/resource.rb, line 31 def can_be_commented? hash.key?('comments_url') && hash.key?('comments_count') end
inspect()
click to toggle source
@return [String] Formatted string with the class name, object id and original hash.
# File lib/camper/resource.rb, line 19 def inspect "#<#{self.class}:#{object_id} {hash: #{hash.inspect}}" end
to_hash()
click to toggle source
@return [Hash] The original hash.
# File lib/camper/resource.rb, line 13 def to_hash hash end
Also aliased as: to_h
Private Instance Methods
method_missing(method_name, *args, &block)
click to toggle source
Respond to messages for which `self.data` has a key
Calls superclass method
# File lib/camper/resource.rb, line 62 def method_missing(method_name, *args, &block) @data.key?(method_name.to_s) ? @data[method_name.to_s] : super end
resourcify_data()
click to toggle source
# File lib/camper/resource.rb, line 45 def resourcify_data @hash.each_with_object({}) do |(key, value), data| value = resourcify_value(value) data[key.to_s] = value end end
resourcify_value(input)
click to toggle source
# File lib/camper/resource.rb, line 53 def resourcify_value(input) return Resource.new(input) if input.is_a? Hash return input unless input.is_a? Array input.map { |curr| resourcify_value(curr) } end
respond_to_missing?(method_name, include_private = false)
click to toggle source
Calls superclass method
# File lib/camper/resource.rb, line 66 def respond_to_missing?(method_name, include_private = false) @hash.keys.map(&:to_sym).include?(method_name.to_sym) || super end