lib/weary/resource.rb in weary-0.2.0 vs lib/weary/resource.rb in weary-0.2.1

- old
+ new

@@ -1,18 +1,55 @@ module Weary class Resource - attr_reader :name - attr_accessor :domain, :with, :requires, :via, :format, :url, :authenticates, :follows + attr_accessor :name, :domain, :with, :requires, :via, :format, :url, :authenticates, :follows def initialize(name) - @name = name + self.name = name self.via = :get self.authenticates = false self.follows = true self.with = [] - self.requires + self.requires = [] end + + def name=(resource_name) + resource_name = resource_name.to_s unless resource_name.is_a?(String) + @name = resource_name.downcase.strip.gsub(/\s/,'_') + end + + def via=(http_verb) + @via = case http_verb + when *Methods[:get] + :get + when *Methods[:post] + :post + when *Methods[:put] + :put + when *Methods[:delete] + :delete + else + raise ArgumentError, "#{http_verb} is not a supported method" + end + end + + def format=(type) + type = type.downcase if type.is_a?(String) + @format = case type + when *ContentTypes[:json] + :json + when *ContentTypes[:xml] + :xml + when *ContentTypes[:html] + :html + when *ContentTypes[:yaml] + :yaml + when *ContentTypes[:plain] + :plain + else + raise ArgumentError, "#{type} is not a recognized format." + end + end def with=(params) unless @requires.nil? @with = params.collect {|x| x.to_sym} | @requires else @@ -34,14 +71,22 @@ pattern = pattern.gsub("<format>", @format.to_s) @url = pattern end def authenticates? - @authenticates == true + if @authenticates + true + else + false + end end def follows_redirects? - @follows == true + if @follows + true + else + false + end end def to_hash {@name.to_sym => { :via => @via, :with => @with, \ No newline at end of file