Method: IMGSrc::API.call_get

Defined in:
lib/imgsrc.rb

.call_get(method, params = {}, cachable = :nocache) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/imgsrc.rb', line 54

def call_get(method, params = {}, cachable = :nocache)
    uri = "/#{method}#{params.empty? ? '' : '?'}#{params.map{ |key, value| "#{key}=#{value}" }.join('&')}"

    params_string = params.map { |k, v| /passw/ =~ k ? nil : "_#{k}-#{ k == 'create' ? v.encode('UTF-8') : v }" }
    cached_file = "#{CACHE_DIR}/#{method.gsub(/\//, '-')}#{params_string.compact.join}.xml"
    if cachable == :cache && File::exists?( cached_file )
        puts "using cached #{cached_file}"
        return File.read( cached_file )
    else
        # puts "fetching #{uri} to #{cached_file}"
        result = @@http_conn.get( uri ).body
        File.new( cached_file, "w" ).write( result ) rescue nil
        return result
    end
rescue Exception => x
    raise RuntimeError, "can not load #{method.sub(/.*\./, "")}: #{x.message}"
end