lib/rubytter.rb in jugyo-rubytter-0.7.0 vs lib/rubytter.rb in jugyo-rubytter-0.8.0

- old
+ new

@@ -7,11 +7,11 @@ require 'rubytter/connection' require 'rubytter/oauth_rubytter' class Rubytter - VERSION = '0.7.0' + VERSION = '0.8.0' class APIError < StandardError attr_reader :response def initialize(msg, response = nil) super(msg) @@ -209,9 +209,28 @@ unless @@structs.has_key?(keys) struct = Struct.new(*keys) struct.class_eval do def method_missing(*args, &block) nil + end + + def to_hash(escape = false) + hash = {} + self.members.each do |member| + value = self[member] + if value.respond_to?(:to_hash) + hash[member] = value.to_hash(escape) + elsif value.is_a?(Array) + hash[member] = value.map{ |i| i.to_hash(escape) } + else + hash[member] = escape && value.is_a?(String) ? CGI.escapeHTML(value) : value + end + end + hash + end + + def to_json(escape = false) + to_hash(escape).to_json end end @@structs[keys] = struct end @@structs[keys]