Module: EstraierPure::Utility

Defined in:
lib/vendor/estraierpure.rb

Overview

:stopdoc:

Module for utility

Class Method Summary collapse

Class Method Details

.base_encode(data) ⇒ Object

Encode a byte sequence with Base64 encoding. ‘data’ specifyes a string object. The return value is the encoded string.



1016
1017
1018
# File 'lib/vendor/estraierpure.rb', line 1016

def base_encode(data)
  [data].pack("m").gsub(/[ \n]/, "")
end

.check_types(types) ⇒ Object

Check types of arguments ‘types’ specifies a hash object whose keys are arguments and values are class objects. If there is a invalid object, an exception is thrown.



883
884
885
886
887
888
889
890
891
892
# File 'lib/vendor/estraierpure.rb', line 883

def check_types(types)
  i = 0
  types.each_key do |key|
    i += 1
    unless key.kind_of?(types[key]) || key == nil
      raise ArgumentError::new("Argument#" + i.to_s +
                                 " should be a kind of " + types[key].to_s)
    end
  end
end

.cond_to_query(cond, depth, wwidth, hwidth, awidth) ⇒ Object

Serialize a condition object into a query string. ‘cond’ specifies a condition object. ‘depth’ specifies depth of meta search. ‘wwidth’ specifies whole width of a snippet. ‘hwidth’ specifies width of strings picked up from the beginning of the text. ‘awidth’ specifies width of strings picked up around each highlighted word. The return value is the serialized string.



980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
# File 'lib/vendor/estraierpure.rb', line 980

def cond_to_query(cond, depth, wwidth, hwidth, awidth)
  buf = StringIO::new
  if cond.phrase
    buf.write("&") if buf.length > 0
    buf.write("phrase=")
    buf.write(URI::encode(cond.phrase))
  end
  for i in 0...cond.attrs.length
    buf.write("&") if buf.length > 0
    buf.write("attr" + (i + 1).to_s + "=")
    buf.write(URI::encode(cond.attrs[i]))
  end
  if cond.order
    buf.write("&") if buf.length > 0
    buf.write("order=")
    buf.write(URI::encode(cond.order))
  end
  if cond.max > 0
    buf.write("&") if buf.length > 0
    buf.write("max=" + cond.max.to_s)
  else
    buf.write("&") if buf.length > 0
    buf.write("max=" + (1 << 30).to_s)
  end
  buf.write("&options=" + cond.options.to_s) if cond.options > 0
  buf.write("&depth=" + depth.to_s) if depth > 0
  buf.write("&wwidth=" + wwidth.to_s)
  buf.write("&hwidth=" + hwidth.to_s)
  buf.write("&awidth=" + awidth.to_s)
  buf.write("&skip=" + cond.skip.to_s)
  buf.string
end

.shuttle_url(url, pxhost, pxport, outsec, reqheads, reqbody, resheads, resbody) ⇒ Object

Perform an interaction of a URL. ‘url’ specifies a URL. ‘pxhost’ specifies the host name of a proxy. If it is ‘nil’, it is not used. ‘pxport’ specifies the port number of the proxy. ‘outsec’ specifies timeout in seconds. If it is negative, it is not used. ‘reqheads’ specifies a list object of extension headers. If it is ‘nil’, it is not used. ‘reqbody’ specifies the pointer of the entitiy body of request. If it is ‘nil’, “GET” method is used. ‘resheads’ specifies a list object into which headers of response is stored. If it is ‘nil’ it is not used. ‘resbody’ specifies stream object into which the entity body of response is stored. If it is ‘nil’, it is not used. The return value is the status code of the response or -1 on error.



907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
# File 'lib/vendor/estraierpure.rb', line 907

def shuttle_url(url, pxhost, pxport, outsec, reqheads, reqbody, resheads, resbody)
  begin
    status = -1
    th = Thread::start do
      url = URI::parse(url)
      url.normalize
      Thread::current.exit if url.scheme != "http" || !url.host || url.port < 1
      if pxhost
        host = pxhost
        port = pxport
        query = "http://" + url.host + ":" + url.port.to_s + url.path
      else
        host = url.host
        port = url.port
        query = url.path
      end
      query += "?" + url.query if url.query && !reqbody
      begin
        sock = TCPSocket.open(host, port)
        if reqbody
          sock.printf("POST " + query + " HTTP/1.0\r\n")
        else
          sock.printf("GET " + query + " HTTP/1.0\r\n")
        end
        sock.printf("Host: %s:%d\r\n", url.host, url.port)
        sock.printf("Connection: close\r\n")
        sock.printf("User-Agent: HyperEstraierForRuby/1.0.0\r\n")
        if reqheads
          reqheads.each do |line|
            sock.printf("%s\r\n", line)
          end
        end
        sock.printf("Content-Length: %d\r\n", reqbody.length) if reqbody
        sock.printf("\r\n")
        sock.write(reqbody) if reqbody
        line = sock.gets.chomp
        elems = line.split(/  */)
        Thread::current.exit if elems.length < 3 || !(elems[0] =~ /^HTTP/)
        status = elems[1].to_i
        resheads.push(line) if resheads
        begin
          line = sock.gets.chomp
          resheads.push(line) if resheads
        end while line.length > 0
        while buf = sock.read(8192)
          resbody.write(buf) if resbody
        end
      ensure
        sock.close if sock
      end
    end
    if outsec >= 0
      unless th.join(outsec)
        th.exit
        th.join
        return -1
      end
    else
      th.join
    end
    return status
  rescue
    return -1
  end
end