Class EstraierPure::Node
In: lib/acts_as_searchable.rb
lib/vendor/estraierpure.rb
Parent: Object

Abstraction of connection to P2P node.

Methods

Public Class methods

Create a node connection object.

[Source]

     # File lib/vendor/estraierpure.rb, line 836
836:     def initialize()
837:       @url = nil
838:       @pxhost = nil
839:       @pxport = -1
840:       @timeout = -1
841:       @auth = nil
842:       @name = nil
843:       @label = nil
844:       @dnum = -1
845:       @wnum = -1
846:       @size = -1.0
847:       @wwidth = 480;
848:       @hwidth = 96;
849:       @awidth = 96;
850:       @status = -1
851:     end

Public Instance methods

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

[Source]

     # File lib/vendor/estraierpure.rb, line 685
685:     def doc_num()
686:       set_info if @dnum < 0
687:       @dnum
688:     end

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

[Source]

     # File lib/vendor/estraierpure.rb, line 522
522:     def edit_doc(doc)
523:       Utility::check_types({ doc=>Document }) if $DEBUG
524:       @status = -1
525:       return false if !@url
526:       turl = @url + "/edit_doc"
527:       reqheads = [ "Content-Type: text/x-estraier-draft" ]
528:       reqheads.push("Authorization: Basic " + Utility::base_encode(@auth)) if @auth
529:       reqbody = doc.dump_draft
530:       rv = Utility::shuttle_url(turl, @pxhost, @pxport, @timeout, reqheads, reqbody, nil, nil)
531:       @status = rv
532:       rv == 200
533:     end

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.

[Source]

     # File lib/vendor/estraierpure.rb, line 608
608:     def etch_doc(id)
609:       Utility::check_types({ id=>Integer }) if $DEBUG
610:       @status = -1
611:       return nil if !@url
612:       turl = @url + "/etch_doc"
613:       reqheads = [ "Content-Type: application/x-www-form-urlencoded" ]
614:       reqheads.push("Authorization: Basic " + Utility::base_encode(@auth)) if @auth
615:       reqbody = "id=" + id.to_s
616:       resbody = StringIO::new
617:       rv = Utility::shuttle_url(turl, @pxhost, @pxport, @timeout, reqheads, reqbody, nil, resbody)
618:       @status = rv
619:       return nil if rv != 200
620:       kwords = {}
621:       lines = resbody.string.split(/\n/)
622:       for i in 0...lines.length
623:         pair = lines[i].split(/\t/)
624:         next if pair.length < 2
625:         kwords[pair[0]] = pair[1]
626:       end
627:       kwords
628:     end

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.

[Source]

     # File lib/vendor/estraierpure.rb, line 633
633:     def etch_doc_by_uri(uri)
634:       Utility::check_types({ uri=>String }) if $DEBUG
635:       @status = -1
636:       return nil if !@url
637:       turl = @url + "/etch_doc"
638:       reqheads = [ "Content-Type: application/x-www-form-urlencoded" ]
639:       reqheads.push("Authorization: Basic " + Utility::base_encode(@auth)) if @auth
640:       reqbody = "uri=" + URI::encode(uri);
641:       resbody = StringIO::new
642:       rv = Utility::shuttle_url(turl, @pxhost, @pxport, @timeout, reqheads, reqbody, nil, resbody)
643:       @status = rv
644:       return nil if rv != 200
645:       kwords = {}
646:       lines = resbody.string.split(/\n/)
647:       for i in 0...lines.length
648:         pair = lines[i].split(/\t/)
649:         next if pair.length < 2
650:         kwords[pair[0]] = pair[1]
651:       end
652:       kwords
653:     end

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

[Source]

     # File lib/vendor/estraierpure.rb, line 537
537:     def get_doc(id)
538:       Utility::check_types({ id=>Integer }) if $DEBUG
539:       @status = -1
540:       return nil if !@url
541:       turl = @url + "/get_doc"
542:       reqheads = [ "Content-Type: application/x-www-form-urlencoded" ]
543:       reqheads.push("Authorization: Basic " + Utility::base_encode(@auth)) if @auth
544:       reqbody = "id=" + id.to_s
545:       resbody = StringIO::new
546:       rv = Utility::shuttle_url(turl, @pxhost, @pxport, @timeout, reqheads, reqbody, nil, resbody)
547:       @status = rv
548:       return nil if rv != 200
549:       Document::new(resbody.string)
550:     end

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.

[Source]

     # File lib/vendor/estraierpure.rb, line 572
572:     def get_doc_attr(id, name)
573:       Utility::check_types({ id=>Integer, name=>String }) if $DEBUG
574:       @status = -1
575:       return nil if !@url
576:       turl = @url + "/get_doc_attr"
577:       reqheads = [ "Content-Type: application/x-www-form-urlencoded" ]
578:       reqheads.push("Authorization: Basic " + Utility::base_encode(@auth)) if @auth
579:       reqbody = "id=" + id.to_s + "&attr=" + URI::encode(name)
580:       resbody = StringIO::new
581:       rv = Utility::shuttle_url(turl, @pxhost, @pxport, @timeout, reqheads, reqbody, nil, resbody)
582:       @status = rv
583:       return nil if rv != 200
584:       resbody.string.chomp
585:     end

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.

[Source]

     # File lib/vendor/estraierpure.rb, line 590
590:     def get_doc_attr_by_uri(uri, name)
591:       Utility::check_types({ uri=>String, name=>String }) if $DEBUG
592:       @status = -1
593:       return nil if !@url
594:       turl = @url + "/get_doc_attr"
595:       reqheads = [ "Content-Type: application/x-www-form-urlencoded" ]
596:       reqheads.push("Authorization: Basic " + Utility::base_encode(@auth)) if @auth
597:       reqbody = "uri=" + URI::encode(uri) + "&attr=" + URI::encode(name)
598:       resbody = StringIO::new
599:       rv = Utility::shuttle_url(turl, @pxhost, @pxport, @timeout, reqheads, reqbody, nil, resbody)
600:       @status = rv
601:       return nil if rv != 200
602:       resbody.string.chomp
603:     end

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

[Source]

     # File lib/vendor/estraierpure.rb, line 554
554:     def get_doc_by_uri(uri)
555:       Utility::check_types({ uri=>String }) if $DEBUG
556:       @status = -1
557:       return nil if !@url
558:       turl = @url + "/get_doc"
559:       reqheads = [ "Content-Type: application/x-www-form-urlencoded" ]
560:       reqheads.push("Authorization: Basic " + Utility::base_encode(@auth)) if @auth
561:       reqbody = "uri=" + URI::encode(uri)
562:       resbody = StringIO::new
563:       rv = Utility::shuttle_url(turl, @pxhost, @pxport, @timeout, reqheads, reqbody, nil, resbody)
564:       @status = rv
565:       return nil if rv != 200
566:       Document::new(resbody.string)
567:     end

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

[Source]

     # File lib/vendor/estraierpure.rb, line 679
679:     def label()
680:       set_info if !@label
681:       @label
682:     end

[Source]

     # File lib/acts_as_searchable.rb, line 340
340:     def list
341:       return false unless @url
342:       turl = @url + "/list"
343:       reqheads = [ "Content-Type: application/x-www-form-urlencoded" ]
344:       reqheads.push("Authorization: Basic " + Utility::base_encode(@auth)) if @auth
345:       reqbody = ""
346:       resbody = StringIO::new
347:       rv = Utility::shuttle_url(turl, @pxhost, @pxport, @timeout, reqheads, reqbody, nil, resbody)
348:       @status = rv
349:       return nil if rv != 200
350:       lines = resbody.string.split(/\n/)
351:       lines.collect { |l| val = l.split(/\t/) and { :id => val[0], :uri => val[1], :digest => val[2] } }
352:     end

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

[Source]

     # File lib/vendor/estraierpure.rb, line 673
673:     def name()
674:       set_info if !@name
675:       @name
676:     end

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

[Source]

     # File lib/vendor/estraierpure.rb, line 492
492:     def out_doc(id)
493:       Utility::check_types({ id=>Integer }) if $DEBUG
494:       @status = -1
495:       return false if !@url
496:       turl = @url + "/out_doc"
497:       reqheads = [ "Content-Type: application/x-www-form-urlencoded" ]
498:       reqheads.push("Authorization: Basic " + Utility::base_encode(@auth)) if @auth
499:       reqbody = "id=" + id.to_s
500:       rv = Utility::shuttle_url(turl, @pxhost, @pxport, @timeout, reqheads, reqbody, nil, nil)
501:       @status = rv
502:       rv == 200
503:     end
 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.

[Source]

     # File lib/vendor/estraierpure.rb, line 507
507:     def out_doc_by_uri(uri)
508:       Utility::check_types({ uri=>String }) if $DEBUG
509:       @status = -1
510:       return false if !@url
511:       turl = @url + "/out_doc"
512:       reqheads = [ "Content-Type: application/x-www-form-urlencoded" ]
513:       reqheads.push("Authorization: Basic " + Utility::base_encode(@auth)) if @auth
514:       reqbody = "uri=" + URI::encode(uri)
515:       rv = Utility::shuttle_url(turl, @pxhost, @pxport, @timeout, reqheads, reqbody, nil, nil)
516:       @status = rv
517:       rv == 200
518:     end

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.

[Source]

     # File lib/vendor/estraierpure.rb, line 477
477:     def put_doc(doc)
478:       Utility::check_types({ doc=>Document }) if $DEBUG
479:       @status = -1
480:       return false if !@url
481:       turl = @url + "/put_doc"
482:       reqheads = [ "Content-Type: text/x-estraier-draft" ]
483:       reqheads.push("Authorization: Basic " + Utility::base_encode(@auth)) if @auth
484:       reqbody = doc.dump_draft
485:       rv = Utility::shuttle_url(turl, @pxhost, @pxport, @timeout, reqheads, reqbody, nil, nil)
486:       @status = rv
487:       rv == 200
488:     end

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.

[Source]

     # File lib/vendor/estraierpure.rb, line 705
705:     def search(cond, depth)
706:       Utility::check_types({ cond=>Condition, depth=>Integer }) if $DEBUG
707:       @status = -1
708:       return nil if !@url
709:       turl = @url + "/search"
710:       reqheads = [ "Content-Type: application/x-www-form-urlencoded" ]
711:       reqheads.push("Authorization: Basic " + Utility::base_encode(@auth)) if @auth
712:       reqbody = Utility::cond_to_query(cond, depth, @wwidth, @hwidth, @awidth)
713:       resbody = StringIO::new
714:       rv = Utility::shuttle_url(turl, @pxhost, @pxport, @timeout, reqheads, reqbody, nil, resbody)
715:       @status = rv
716:       return nil if rv != 200
717:       lines = resbody.string.split(/\n/)
718:       return nil if lines.length < 1
719:       docs = []
720:       hints = {}
721:       nres = NodeResult::new(docs, hints)
722:       border = lines[0]
723:       isend = false
724:       lnum = 1
725:       while lnum < lines.length
726:         line = lines[lnum]
727:         lnum += 1
728:         if line.length >= border.length && line.index(border) == 0
729:           isend = true if line[border.length...line.length] == ":END"
730:           break
731:         end
732:         lidx = line.index("\t")
733:         if lidx
734:           key = line[0...lidx]
735:           value = line[(lidx+1)...line.length]
736:           hints[key] = value
737:         end
738:       end
739:       snum = lnum
740:       while !isend && lnum < lines.length
741:         line = lines[lnum]
742:         lnum += 1
743:         if line.length >= border.length && line.index(border) == 0
744:           if lnum > snum
745:             rdattrs = {}
746:             sb = StringIO::new
747:             rdvector = ""
748:             rlnum = snum
749:             while rlnum < lnum - 1
750:               rdline = lines[rlnum].strip
751:               rlnum += 1
752:               break if rdline.length < 1
753:               if rdline =~ /^%/
754:                 lidx = rdline.index("\t")
755:                 rdvector = rdline[(lidx+1)...rdline.length] if rdline =~ /%VECTOR/ && lidx
756:               else
757:                 lidx = rdline.index("=")
758:                 if lidx
759:                   key = rdline[0...lidx]
760:                   value = rdline[(lidx+1)...rdline.length]
761:                   rdattrs[key] = value
762:                 end
763:               end
764:             end
765:             while rlnum < lnum - 1
766:               rdline = lines[rlnum]
767:               rlnum += 1
768:               sb.printf("%s\n", rdline)
769:             end
770:             rduri = rdattrs["@uri"]
771:             rdsnippet = sb.string
772:             if rduri
773:               rdoc = ResultDocument::new(rduri, rdattrs, rdsnippet, rdvector)
774:               docs.push(rdoc)
775:             end
776:           end
777:           snum = lnum
778:           isend = true if line[border.length...line.length] == ":END"
779:         end
780:       end
781:       return nil if !isend
782:       return nres
783:     end

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

[Source]

     # File lib/vendor/estraierpure.rb, line 464
464:     def set_auth(name, password)
465:       Utility::check_types({ name=>String, password=>String }) if $DEBUG
466:       @auth = name + ":" + password
467:       nil
468:     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.

[Source]

     # File lib/vendor/estraierpure.rb, line 818
818:     def set_link(url, label, credit)
819:       Utility::check_types({ url=>String, label=>String, credit=>Integer }) if $DEBUG
820:       @status = -1
821:       return false if !@url
822:       turl = @url + "/_set_link"
823:       reqheads = [ "Content-Type: application/x-www-form-urlencoded" ]
824:       reqheads.push("Authorization: Basic " + Utility::base_encode(@auth)) if @auth
825:       reqbody = "url=" + URI::encode(url) + "&label=" + label
826:       reqbody += "&credit=" + credit.to_s if credit >= 0
827:       rv = Utility::shuttle_url(turl, @pxhost, @pxport, @timeout, reqheads, reqbody, nil, nil)
828:       @status = rv
829:       rv == 200
830:     end

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’.

[Source]

     # File lib/vendor/estraierpure.rb, line 446
446:     def set_proxy(host, port)
447:       Utility::check_types({ host=>String, port=>Integer }) if $DEBUG
448:       @pxhost = host
449:       @pxport = port
450:       nil
451:     end

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.

[Source]

     # File lib/vendor/estraierpure.rb, line 791
791:     def set_snippet_width(wwidth, hwidth, awidth)
792:       @wwidth = wwidth;
793:       @hwidth = hwidth if hwidth >= 0
794:       @awidth = awidth if awidth >= 0
795:     end

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

[Source]

     # File lib/vendor/estraierpure.rb, line 455
455:     def set_timeout(sec)
456:       Utility::check_types({ sec=>Integer }) if $DEBUG
457:       @timeout = sec
458:       nil
459:     end

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

[Source]

     # File lib/vendor/estraierpure.rb, line 437
437:     def set_url(url)
438:       Utility::check_types({ url=>String }) if $DEBUG
439:       @url = url
440:       nil
441:     end

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.

[Source]

     # File lib/vendor/estraierpure.rb, line 801
801:     def set_user(name, mode)
802:       Utility::check_types({ name=>String, mode=>Integer }) if $DEBUG
803:       @status = -1
804:       return false if !@url
805:       turl = @url + "/_set_user"
806:       reqheads = [ "Content-Type: application/x-www-form-urlencoded" ]
807:       reqheads.push("Authorization: Basic " + Utility::base_encode(@auth)) if @auth
808:       reqbody = "name=" + URI::encode(name) + "&mode=" + mode.to_s
809:       rv = Utility::shuttle_url(turl, @pxhost, @pxport, @timeout, reqheads, reqbody, nil, nil)
810:       @status = rv
811:       rv == 200
812:     end

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

[Source]

     # File lib/vendor/estraierpure.rb, line 697
697:     def size()
698:       set_info if @size < 0.0
699:       @size
700:     end

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

[Source]

     # File lib/vendor/estraierpure.rb, line 471
471:     def status()
472:       @status
473:     end

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.

[Source]

     # File lib/vendor/estraierpure.rb, line 657
657:     def uri_to_id(uri)
658:       Utility::check_types({ uri=>String }) if $DEBUG
659:       @status = -1
660:       return -1 if !@url
661:       turl = @url + "/uri_to_id"
662:       reqheads = [ "Content-Type: application/x-www-form-urlencoded" ]
663:       reqheads.push("Authorization: Basic " + Utility::base_encode(@auth)) if @auth
664:       reqbody = "uri=" + URI::encode(uri)
665:       resbody = StringIO::new
666:       rv = Utility::shuttle_url(turl, @pxhost, @pxport, @timeout, reqheads, reqbody, nil, resbody)
667:       @status = rv
668:       return nil if rv != 200
669:       resbody.string.chomp
670:     end

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

[Source]

     # File lib/vendor/estraierpure.rb, line 691
691:     def word_num()
692:       set_info if @wnum < 0
693:       @wnum
694:     end

[Validate]