# Copyright (c) 2009 Douglas Willcocks # # Permission is hereby granted, free of charge, to any person # obtaining a copy of this software and associated documentation # files (the "Software"), to deal in the Software without # restriction, including without limitation the rights to use, # copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the # Software is furnished to do so, subject to the following # conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR # OTHER DEALINGS IN THE SOFTWARE. # An object describing an abstract API request module Instapaper module Request # The class representing a generic request class AbstractRequest include Constants include Exceptions include Protocol attr_reader :data attr_reader :url def initialize #:nodoc: @validation_callbacks = Array.new @validation_errors = RequestErrors.new @data = Hash.new @abstract = true @protocol = PROTOCOLS[:secure] @api = false @validated = false end # Prepares the request def prepare build_url end # Return a hash of the data to be sent to the server # # Strict determines whether the method raises an exception # or returns an error object def post_data(strict = true) return @data if @validated raise AbstractRequestException.new if @abstract || !@api @validation_callbacks.each do |callback| unless send(callback.method,*callback.args) if strict raise InvalidRequestDetailsException.new(callback.message) else @validation_errors << RequestError.new(callback.message,callback.method) end end end if @validation_errors.valid? @validated = true @data else @validation_errors end end private def add_validation_callback(callback) #:nodoc: @validation_callbacks << callback unless @validation_callbacks.include?(callback) end def build_url #:nodoc: @url = [@protocol,"://",HOST,"/",API_BASE,"/",@api].join('') end def has_value(value) #:nodoc: !(value.to_s.nil? || value.to_s.empty? || !value) end end end end