module Access class Response attr_accessor :info, :offers, :stores, :locations, :categories, :suggestions, :oauth_applications, :access_tokens, :oauth_tokens, :oauth_token, :verify, :reports, :members, :filters, :links, :dev_node, :message, :status, :success, :error, :response_status, :content_type, :redemption_method, :details, :oauth_access_token def initialize(response) response.each { |key, value| instance_variable_set("@#{key}", value) if self.class.instance_methods.include? key.to_sym } @response_status = response.message @status ||= response.code check_success(response) if @success @info = Access::Info.new(@info) if @info (@links = @links.is_a?(Array) ? Access::Link.process_batch(@links) : Access::Link.new(@links)) if @links process_data end end def check_success(response) if response.success? @success = true else create_error end end def create_error @success = false @error = Access::Error.new(@response_status, @status, @message) remove_instance_variable(:@message) if @message remove_instance_variable(:@status) end end class OfferResponse < Response def process_data # for when you search and it returns 0 (@offers = []; create_error) if @message @offers = Access::Offer.process_batch(@offers) end end class StoreResponse < Response def process_data (@stores = []; create_error) if @message @stores = Access::Store.process_batch(@stores) end end class LocationResponse < Response def process_data (@locations = []; create_error) if @message @locations = Access::Location.process_batch(@locations) end end class CategoryResponse < Response def process_data (@categories = []; create_error) if @message @categories = Access::Category.process_batch(@categories) end end class AutocompleteResponse < Response def process_data # (@suggestions = []; create_error) if @message @suggestions = Access::Autocomplete.new(@suggestions) end end class OauthApplicationResponse < Response def process_data (@oauth_applications = []; create_error) if @message @oauth_applications = Access::OauthApplication.process_batch(@oauth_applications) end end class TokenResponse < Response def process_data (create_error) if @message @access_tokens = Access::Token.process_batch(@access_tokens) if @access_tokens @oauth_tokens = Access::Token.process_batch(@oauth_tokens) if @oauth_tokens @oauth_token = Access::Token.new(@oauth_token) if @oauth_token end end class VerifyResponse < Response def process_data @oauth_access_token = Access::Verify.new(@oauth_access_token) if @oauth_access_token @offers = Access::Verify.new(@offers) if @offers @categories = Access::Verify.new(@categories) if @categories @locations = Access::Verify.new(@locations) if @locations @stores = Access::Verify.new(@stores) if @stores end end class RedeemResponse < Response def process_data @details = Access::Redeem.new(@details) if @details end end class ReportResponse < Response def process_data @reports = Access::Report.new(@reports) end end class MemberResponse < Response def process_data @members = Access::Member.process_batch(@members) end end class FilterResponse < Response def process_data @filters = Access::Filter.process_batch(@filters) end end class CitySavingsResponse < Response def process_data # something? # @members = Access::Member.process_batch(@members) end end end