#!ruby # -*- encoding: UTF-8 -*- require 'uri' require 'net/http' require 'rexml/document' class Timelog4r =begin rdoc Description:: Utility Module. there in 'http', 'uri', 'parser', etc... =end module Utils =begin rdoc Description:: parse for uri queries. Params:: options { :key => :value } [Hash] Return:: '?key=value&key2=value2' =end def parse_options(options) if options.has_key?(:since) options[:since] = options[:since].strftime '%Y%m%d%H%M' end options = options.find_all{|option| option.last } if options.empty? result = nil else result = URI.escape( '?' + options.collect{|option| option.join('=') }.join('&') ) end return result end =begin rdoc Description:: url_string to URI-Instance. Params:: :url_string url_string [String] Return:: URI-Instance =end def create_uri(url_string) return URI.parse(url_string) end =begin rdoc Description:: create of Net::HTTP::Request-Instance. Params:: :type http-method (:get|:post) [String] :uri request-uri [URI] :use_basic_auth basic_auth flag [Boolean] default false Return:: Net::HTTP::Request-Instance =end def create_request(type, uri, use_basic_auth = false) case type when :get request = Net::HTTP::Get.new(uri.request_uri) when :post request = Net::HTTP::Post.new(uri.request_uri) request.content_length = 0 end request['Accept-Charset'] = 'euc-jp, utf-8' request['Accept-Language'] = 'ja, en' request['User-Agent'] = @user_agent request.basic_auth(@user_id, @password) if use_basic_auth return request end =begin rdoc Description:: access to TimelogAPI. Params:: :uri request-uri [URI] :request http-request [Net::HTTP::Request] Return:: REXML::Document-Instance (case of 'success') false (case of 'failed') =end def http_access(uri, request) Net::HTTP.start(uri.host, uri.port){|http| response = http.request(request) if RUBY_VERSION.match(/^1\.9\./) response.body.force_encoding('UTF-8') end if response.code == '200' return REXML::Document.new(response.body) else return false end } end end end