Class: EstraierPure::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/vendor/estraierpure.rb,
lib/acts_as_searchable.rb

Overview


++ Abstraction of connection to P2P node.


Instance Method Summary collapse

Instance Method Details

#doc_numObject

Get the number of documents. The return value is the number of documents. On error, -1 is returned.



685
686
687
688
# File 'lib/vendor/estraierpure.rb', line 685

def doc_num()
  set_info if @dnum < 0
  @dnum
end

#edit_doc(doc) ⇒ Object

Edit attributes of a document. ‘doc’ specifies a document object. The return value is true if success, else it is false.



522
523
524
525
526
527
528
529
530
531
532
533
# File 'lib/vendor/estraierpure.rb', line 522

def edit_doc(doc)
  Utility::check_types({ doc=>Document }) if $DEBUG
  @status = -1
  return false if !@url
  turl = @url + "/edit_doc"
  reqheads = [ "Content-Type: text/x-estraier-draft" ]
  reqheads.push("Authorization: Basic " + Utility::base_encode(@auth)) if @auth
  reqbody = doc.dump_draft
  rv = Utility::shuttle_url(turl, @pxhost, @pxport, @timeout, reqheads, reqbody, nil, nil)
  @status = rv
  rv == 200
end

#etch_doc(id) ⇒ Object

Extract keywords of a document. ‘id’ specifies the ID number of a registered document. The return value is a hash object of keywords and their scores in decimal string or ‘nil’ on error.



608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
# File 'lib/vendor/estraierpure.rb', line 608

def etch_doc(id)
  Utility::check_types({ id=>Integer }) if $DEBUG
  @status = -1
  return nil if !@url
  turl = @url + "/etch_doc"
  reqheads = [ "Content-Type: application/x-www-form-urlencoded" ]
  reqheads.push("Authorization: Basic " + Utility::base_encode(@auth)) if @auth
  reqbody = "id=" + id.to_s
  resbody = StringIO::new
  rv = Utility::shuttle_url(turl, @pxhost, @pxport, @timeout, reqheads, reqbody, nil, resbody)
  @status = rv
  return nil if rv != 200
  kwords = {}
  lines = resbody.string.split(/\n/)
  for i in 0...lines.length
    pair = lines[i].split(/\t/)
    next if pair.length < 2
    kwords[pair[0]] = pair[1]
  end
  kwords
end

#etch_doc_by_uri(uri) ⇒ Object

Extract keywords of a document specified by URI. ‘uri’ specifies the URI of a registered document. The return value is a hash object of keywords and their scores in decimal string or ‘nil’ on error.



633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
# File 'lib/vendor/estraierpure.rb', line 633

def etch_doc_by_uri(uri)
  Utility::check_types({ uri=>String }) if $DEBUG
  @status = -1
  return nil if !@url
  turl = @url + "/etch_doc"
  reqheads = [ "Content-Type: application/x-www-form-urlencoded" ]
  reqheads.push("Authorization: Basic " + Utility::base_encode(@auth)) if @auth
  reqbody = "uri=" + URI::encode(uri);
  resbody = StringIO::new
  rv = Utility::shuttle_url(turl, @pxhost, @pxport, @timeout, reqheads, reqbody, nil, resbody)
  @status = rv
  return nil if rv != 200
  kwords = {}
  lines = resbody.string.split(/\n/)
  for i in 0...lines.length
    pair = lines[i].split(/\t/)
    next if pair.length < 2
    kwords[pair[0]] = pair[1]
  end
  kwords
end

#get_doc(id) ⇒ Object

Retrieve a document. ‘id’ specifies the ID number of a registered document. The return value is a document object. On error, ‘nil’ is returned.



537
538
539
540
541
542
543
544
545
546
547
548
549
550
# File 'lib/vendor/estraierpure.rb', line 537

def get_doc(id)
  Utility::check_types({ id=>Integer }) if $DEBUG
  @status = -1
  return nil if !@url
  turl = @url + "/get_doc"
  reqheads = [ "Content-Type: application/x-www-form-urlencoded" ]
  reqheads.push("Authorization: Basic " + Utility::base_encode(@auth)) if @auth
  reqbody = "id=" + id.to_s
  resbody = StringIO::new
  rv = Utility::shuttle_url(turl, @pxhost, @pxport, @timeout, reqheads, reqbody, nil, resbody)
  @status = rv
  return nil if rv != 200
  Document::new(resbody.string)
end

#get_doc_attr(id, name) ⇒ Object

Retrieve the value of an attribute of a document. ‘id’ specifies the ID number of a registered document. ‘name’ specifies the name of an attribute. The return value is the value of the attribute or ‘nil’ if it does not exist.



572
573
574
575
576
577
578
579
580
581
582
583
584
585
# File 'lib/vendor/estraierpure.rb', line 572

def get_doc_attr(id, name)
  Utility::check_types({ id=>Integer, name=>String }) if $DEBUG
  @status = -1
  return nil if !@url
  turl = @url + "/get_doc_attr"
  reqheads = [ "Content-Type: application/x-www-form-urlencoded" ]
  reqheads.push("Authorization: Basic " + Utility::base_encode(@auth)) if @auth
  reqbody = "id=" + id.to_s + "&attr=" + URI::encode(name)
  resbody = StringIO::new
  rv = Utility::shuttle_url(turl, @pxhost, @pxport, @timeout, reqheads, reqbody, nil, resbody)
  @status = rv
  return nil if rv != 200
  resbody.string.chomp
end

#get_doc_attr_by_uri(uri, name) ⇒ Object

Retrieve the value of an attribute of a document specified by URI. ‘uri’ specifies the URI of a registered document. ‘name’ specifies the name of an attribute. The return value is the value of the attribute or ‘nil’ if it does not exist.



590
591
592
593
594
595
596
597
598
599
600
601
602
603
# File 'lib/vendor/estraierpure.rb', line 590

def get_doc_attr_by_uri(uri, name)
  Utility::check_types({ uri=>String, name=>String }) if $DEBUG
  @status = -1
  return nil if !@url
  turl = @url + "/get_doc_attr"
  reqheads = [ "Content-Type: application/x-www-form-urlencoded" ]
  reqheads.push("Authorization: Basic " + Utility::base_encode(@auth)) if @auth
  reqbody = "uri=" + URI::encode(uri) + "&attr=" + URI::encode(name)
  resbody = StringIO::new
  rv = Utility::shuttle_url(turl, @pxhost, @pxport, @timeout, reqheads, reqbody, nil, resbody)
  @status = rv
  return nil if rv != 200
  resbody.string.chomp
end

#get_doc_by_uri(uri) ⇒ Object

Retrieve a document. ‘uri’ specifies the URI of a registered document. The return value is a document object. On error, ‘nil’ is returned.



554
555
556
557
558
559
560
561
562
563
564
565
566
567
# File 'lib/vendor/estraierpure.rb', line 554

def get_doc_by_uri(uri)
  Utility::check_types({ uri=>String }) if $DEBUG
  @status = -1
  return nil if !@url
  turl = @url + "/get_doc"
  reqheads = [ "Content-Type: application/x-www-form-urlencoded" ]
  reqheads.push("Authorization: Basic " + Utility::base_encode(@auth)) if @auth
  reqbody = "uri=" + URI::encode(uri)
  resbody = StringIO::new
  rv = Utility::shuttle_url(turl, @pxhost, @pxport, @timeout, reqheads, reqbody, nil, resbody)
  @status = rv
  return nil if rv != 200
  Document::new(resbody.string)
end

#labelObject

Get the label. The return value is the label. On error, ‘nil’ is returned.



679
680
681
682
# File 'lib/vendor/estraierpure.rb', line 679

def label()
  set_info if !@label
  @label
end

#listObject



340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/acts_as_searchable.rb', line 340

def list
  return false unless @url
  turl = @url + "/list"
  reqheads = [ "Content-Type: application/x-www-form-urlencoded" ]
  reqheads.push("Authorization: Basic " + Utility::base_encode(@auth)) if @auth
  reqbody = ""
  resbody = StringIO::new
  rv = Utility::shuttle_url(turl, @pxhost, @pxport, @timeout, reqheads, reqbody, nil, resbody)
  @status = rv
  return nil if rv != 200
  lines = resbody.string.split(/\n/)
  lines.collect { |l| val = l.split(/\t/) and { :id => val[0], :uri => val[1], :digest => val[2] } }
end

#nameObject

Get the name. The return value is the name. On error, ‘nil’ is returned.



673
674
675
676
# File 'lib/vendor/estraierpure.rb', line 673

def name()
  set_info if !@name
  @name
end

#out_doc(id) ⇒ Object

Remove a document. ‘id’ specifies the ID number of a registered document. The return value is true if success, else it is false.



492
493
494
495
496
497
498
499
500
501
502
503
# File 'lib/vendor/estraierpure.rb', line 492

def out_doc(id)
  Utility::check_types({ id=>Integer }) if $DEBUG
  @status = -1
  return false if !@url
  turl = @url + "/out_doc"
  reqheads = [ "Content-Type: application/x-www-form-urlencoded" ]
  reqheads.push("Authorization: Basic " + Utility::base_encode(@auth)) if @auth
  reqbody = "id=" + id.to_s
  rv = Utility::shuttle_url(turl, @pxhost, @pxport, @timeout, reqheads, reqbody, nil, nil)
  @status = rv
  rv == 200
end

#out_doc_by_uri(uri) ⇒ Object

Remove a document specified by URI. ‘uri’ specifies the URI of a registered document. The return value is true if success, else it is false.



507
508
509
510
511
512
513
514
515
516
517
518
# File 'lib/vendor/estraierpure.rb', line 507

def out_doc_by_uri(uri)
  Utility::check_types({ uri=>String }) if $DEBUG
  @status = -1
  return false if !@url
  turl = @url + "/out_doc"
  reqheads = [ "Content-Type: application/x-www-form-urlencoded" ]
  reqheads.push("Authorization: Basic " + Utility::base_encode(@auth)) if @auth
  reqbody = "uri=" + URI::encode(uri)
  rv = Utility::shuttle_url(turl, @pxhost, @pxport, @timeout, reqheads, reqbody, nil, nil)
  @status = rv
  rv == 200
end

#put_doc(doc) ⇒ Object

Add a document. ‘doc’ specifies a document object. The document object should have the URI attribute. The return value is true if success, else it is false.



477
478
479
480
481
482
483
484
485
486
487
488
# File 'lib/vendor/estraierpure.rb', line 477

def put_doc(doc)
  Utility::check_types({ doc=>Document }) if $DEBUG
  @status = -1
  return false if !@url
  turl = @url + "/put_doc"
  reqheads = [ "Content-Type: text/x-estraier-draft" ]
  reqheads.push("Authorization: Basic " + Utility::base_encode(@auth)) if @auth
  reqbody = doc.dump_draft
  rv = Utility::shuttle_url(turl, @pxhost, @pxport, @timeout, reqheads, reqbody, nil, nil)
  @status = rv
  rv == 200
end

#search(cond, depth) ⇒ Object

Search documents corresponding a condition. ‘cond’ specifies a condition object. ‘depth’ specifies the depth of meta search. The return value is a node result object. On error, ‘nil’ is returned.



705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
# File 'lib/vendor/estraierpure.rb', line 705

def search(cond, depth)
  Utility::check_types({ cond=>Condition, depth=>Integer }) if $DEBUG
  @status = -1
  return nil if !@url
  turl = @url + "/search"
  reqheads = [ "Content-Type: application/x-www-form-urlencoded" ]
  reqheads.push("Authorization: Basic " + Utility::base_encode(@auth)) if @auth
  reqbody = Utility::cond_to_query(cond, depth, @wwidth, @hwidth, @awidth)
  resbody = StringIO::new
  rv = Utility::shuttle_url(turl, @pxhost, @pxport, @timeout, reqheads, reqbody, nil, resbody)
  @status = rv
  return nil if rv != 200
  lines = resbody.string.split(/\n/)
  return nil if lines.length < 1
  docs = []
  hints = {}
  nres = NodeResult::new(docs, hints)
  border = lines[0]
  isend = false
  lnum = 1
  while lnum < lines.length
    line = lines[lnum]
    lnum += 1
    if line.length >= border.length && line.index(border) == 0
      isend = true if line[border.length...line.length] == ":END"
      break
    end
    lidx = line.index("\t")
    if lidx
      key = line[0...lidx]
      value = line[(lidx+1)...line.length]
      hints[key] = value
    end
  end
  snum = lnum
  while !isend && lnum < lines.length
    line = lines[lnum]
    lnum += 1
    if line.length >= border.length && line.index(border) == 0
      if lnum > snum
        rdattrs = {}
        sb = StringIO::new
        rdvector = ""
        rlnum = snum
        while rlnum < lnum - 1
          rdline = lines[rlnum].strip
          rlnum += 1
          break if rdline.length < 1
          if rdline =~ /^%/
            lidx = rdline.index("\t")
            rdvector = rdline[(lidx+1)...rdline.length] if rdline =~ /%VECTOR/ && lidx
          else
            lidx = rdline.index("=")
            if lidx
              key = rdline[0...lidx]
              value = rdline[(lidx+1)...rdline.length]
              rdattrs[key] = value
            end
          end
        end
        while rlnum < lnum - 1
          rdline = lines[rlnum]
          rlnum += 1
          sb.printf("%s\n", rdline)
        end
        rduri = rdattrs["@uri"]
        rdsnippet = sb.string
        if rduri
          rdoc = ResultDocument::new(rduri, rdattrs, rdsnippet, rdvector)
          docs.push(rdoc)
        end
      end
      snum = lnum
      isend = true if line[border.length...line.length] == ":END"
    end
  end
  return nil if !isend
  return nres
end

#set_auth(name, password) ⇒ Object

Set the authentication information. ‘name’ specifies the name of authentication. ‘passwd’ specifies the password of the authentication. The return value is always ‘nil’.



464
465
466
467
468
# File 'lib/vendor/estraierpure.rb', line 464

def set_auth(name, password)
  Utility::check_types({ name=>String, password=>String }) if $DEBUG
  @auth = name + ":" + password
  nil
end

Manage a link of a node. ‘url’ specifies the URL of the target node of a link. ‘label’ specifies the label of the link. ‘credit’ specifies the credit of the link. If it is negative, the link is removed. The return value is true if success, else it is false.



818
819
820
821
822
823
824
825
826
827
828
829
830
# File 'lib/vendor/estraierpure.rb', line 818

def set_link(url, label, credit)
  Utility::check_types({ url=>String, label=>String, credit=>Integer }) if $DEBUG
  @status = -1
  return false if !@url
  turl = @url + "/_set_link"
  reqheads = [ "Content-Type: application/x-www-form-urlencoded" ]
  reqheads.push("Authorization: Basic " + Utility::base_encode(@auth)) if @auth
  reqbody = "url=" + URI::encode(url) + "&label=" + label
  reqbody += "&credit=" + credit.to_s if credit >= 0
  rv = Utility::shuttle_url(turl, @pxhost, @pxport, @timeout, reqheads, reqbody, nil, nil)
  @status = rv
  rv == 200
end

#set_proxy(host, port) ⇒ Object

Set the proxy information. ‘host’ specifies the host name of a proxy server. ‘port’ specifies the port number of the proxy server. The return value is always ‘nil’.



446
447
448
449
450
451
# File 'lib/vendor/estraierpure.rb', line 446

def set_proxy(host, port)
  Utility::check_types({ host=>String, port=>Integer }) if $DEBUG
  @pxhost = host
  @pxport = port
  nil
end

#set_snippet_width(wwidth, hwidth, awidth) ⇒ Object

Set width of snippet in the result. ‘wwidth’ specifies whole width of a snippet. By default, it is 480. If it is 0, no snippet is sent. If it is negative, whole body text is sent instead of snippet. ‘hwidth’ specifies width of strings picked up from the beginning of the text. By default, it is 96. If it is negative 0, the current setting is not changed. ‘awidth’ specifies width of strings picked up around each highlighted word. By default, it is 96. If it is negative, the current setting is not changed.



791
792
793
794
795
# File 'lib/vendor/estraierpure.rb', line 791

def set_snippet_width(wwidth, hwidth, awidth)
  @wwidth = wwidth;
  @hwidth = hwidth if hwidth >= 0
  @awidth = awidth if awidth >= 0
end

#set_timeout(sec) ⇒ Object

Set timeout of a connection. ‘sec’ specifies timeout of the connection in seconds. The return value is always ‘nil’.



455
456
457
458
459
# File 'lib/vendor/estraierpure.rb', line 455

def set_timeout(sec)
  Utility::check_types({ sec=>Integer }) if $DEBUG
  @timeout = sec
  nil
end

#set_url(url) ⇒ Object

Set the URL of a node server. ‘url’ specifies the URL of a node. The return value is always ‘nil’.



437
438
439
440
441
# File 'lib/vendor/estraierpure.rb', line 437

def set_url(url)
  Utility::check_types({ url=>String }) if $DEBUG
  @url = url
  nil
end

#set_user(name, mode) ⇒ Object

Manage a user account of a node. ‘name’ specifies the name of a user. ‘mode’ specifies the operation mode. 0 means to delete the account. 1 means to set the account as an administrator. 2 means to set the account as a guest. The return value is true if success, else it is false.



801
802
803
804
805
806
807
808
809
810
811
812
# File 'lib/vendor/estraierpure.rb', line 801

def set_user(name, mode)
  Utility::check_types({ name=>String, mode=>Integer }) if $DEBUG
  @status = -1
  return false if !@url
  turl = @url + "/_set_user"
  reqheads = [ "Content-Type: application/x-www-form-urlencoded" ]
  reqheads.push("Authorization: Basic " + Utility::base_encode(@auth)) if @auth
  reqbody = "name=" + URI::encode(name) + "&mode=" + mode.to_s
  rv = Utility::shuttle_url(turl, @pxhost, @pxport, @timeout, reqheads, reqbody, nil, nil)
  @status = rv
  rv == 200
end

#sizeObject

Get the size of the datbase. The return value is the size of the datbase. On error, -1.0 is returned.



697
698
699
700
# File 'lib/vendor/estraierpure.rb', line 697

def size()
  set_info if @size < 0.0
  @size
end

#statusObject

Get the status code of the last request. The return value is the status code of the last request. -1 means failure of connection.



471
472
473
# File 'lib/vendor/estraierpure.rb', line 471

def status()
  @status
end

#uri_to_id(uri) ⇒ Object

Get the ID of a document specified by URI. ‘uri’ specifies the URI of a registered document. The return value is the ID of the document. On error, -1 is returned.



657
658
659
660
661
662
663
664
665
666
667
668
669
670
# File 'lib/vendor/estraierpure.rb', line 657

def uri_to_id(uri)
  Utility::check_types({ uri=>String }) if $DEBUG
  @status = -1
  return -1 if !@url
  turl = @url + "/uri_to_id"
  reqheads = [ "Content-Type: application/x-www-form-urlencoded" ]
  reqheads.push("Authorization: Basic " + Utility::base_encode(@auth)) if @auth
  reqbody = "uri=" + URI::encode(uri)
  resbody = StringIO::new
  rv = Utility::shuttle_url(turl, @pxhost, @pxport, @timeout, reqheads, reqbody, nil, resbody)
  @status = rv
  return nil if rv != 200
  resbody.string.chomp
end

#word_numObject

Get the number of unique words. The return value is the number of unique words. On error, -1 is returned.



691
692
693
694
# File 'lib/vendor/estraierpure.rb', line 691

def word_num()
  set_info if @wnum < 0
  @wnum
end