Sha256: 6f52a0f87218b3ee9e516dd090c30cfdeb06bacafddced8de51b07713d7c270a

Contents?: true

Size: 1.4 KB

Versions: 3

Compression:

Stored size: 1.4 KB

Contents

# This class represents a single resource.
# It contains attributes from parsed definition.
# So anywhere in view template we can use this object.
class Calamum::Resource
  attr_accessor :uri, :action, :headers,
    :auth, :params, :errors, :description, :response, :tryit

  # Initialize object from attributes.
  #
  # @param attrs [Hash] attributes to set
  def initialize(attrs)
    @uri = attrs['uri']
    @action = attrs['action'].upcase
    @headers = attrs['headers'] || {}
    @auth = !attrs['authentication']
    @params = attrs['params'] || {}
    @errors = attrs['errors'] || {}
    @description = attrs['description']
    @response = attrs['response']
    @tryit = attrs['tryit']
  end

  # Returns a unique, but readable name for this resource suitable for use as a filename
  #
  # @return [String] resource filename
  def slug
    sanitized_uri = uri.gsub(/[^\w]/, '_').gsub('__', '_')
    "#{sanitized_uri}_#{action.downcase}_#{self.object_id}"
  end

  # @override
  # Returns a string representing a label css class.
  #
  # @return [String] css class
  def action_label
    case @action
    when 'GET'
      'label-info'
    when 'POST'
      'label-success'
    when 'PUT'
      'label-warning'
    when 'DELETE'
      'label-important'
    end
  end

  # @override
  # Returns a string representing a resource.
  #
  # @return [String] resource in a form (action: uri)
  def to_s
    "#{action}: #{uri}"
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
calamum-1.2.2 lib/calamum/resource.rb
calamum-1.2.1 lib/calamum/resource.rb
calamum-1.2.0 lib/calamum/resource.rb