• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ruby-rdf / rdf-json / 7451961379

08 Jan 2024 07:01PM UTC coverage: 92.517%. Remained the same
7451961379

push

github

gkellogg
* CI on Ruby 3.3.
* Reference release notes on GitHub.

136 of 147 relevant lines covered (92.52%)

87.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.27
/lib/rdf/json/writer.rb
1
module RDF::JSON
2✔
2
  ##
3
  # RDF/JSON serializer.
4
  #
5
  # @example Loading RDF/JSON serialization support
6
  #   require 'rdf/json'
7
  #
8
  # @example Obtaining an RDF/JSON writer class
9
  #   RDF::Writer.for(:json)         #=> RDF::JSON::Writer
10
  #   RDF::Writer.for("etc/test.rj")
11
  #   RDF::Writer.for(:file_name      => "etc/test.rj")
12
  #   RDF::Writer.for(:file_extension => "rj")
13
  #   RDF::Writer.for(:content_type   => "application/rdf+json")
14
  #
15
  # @example Serializing RDF statements into an RDF/JSON file
16
  #   RDF::JSON::Writer.open("etc/test.rj") do |writer|
17
  #     graph.each_statement do |statement|
18
  #       writer << statement
19
  #     end
20
  #   end
21
  #
22
  # @example Serializing RDF statements into an RDF/JSON string
23
  #   RDF::JSON::Writer.buffer do |writer|
24
  #     graph.each_statement do |statement|
25
  #       writer << statement
26
  #     end
27
  #   end
28
  #
29
  # @see http://n2.talis.com/wiki/RDF_JSON_Specification
30
  class Writer < RDF::Writer
2✔
31
    format RDF::JSON::Format
2✔
32

33
    ##
34
    # Stores the RDF/JSON representation of a triple.
35
    #
36
    # @param  [RDF::Resource] subject
37
    # @param  [RDF::URI]      predicate
38
    # @param  [RDF::Value]    object
39
    # @return [void]
40
    # @see    #write_epilogue
41
    def write_triple(subject, predicate, object)
2✔
42
      s = subject.to_s
50✔
43
      p = predicate.to_s
50✔
44
      o = object.is_a?(RDF::Value) ? object : RDF::Literal.new(object)
50✔
45
      @json       ||= {}
50✔
46
      @json[s]    ||= {}
50✔
47
      @json[s][p] ||= []
50✔
48
      @json[s][p] << o.to_rdf_json
50✔
49
    end
50

51
    ##
52
    # Outputs the RDF/JSON representation of all stored triples.
53
    #
54
    # @return [void]
55
    # @see    #write_triple
56
    def write_epilogue
2✔
57
      puts @json.to_json
46✔
58
      super
46✔
59
    end
60

61
    ##
62
    # Returns the RDF/JSON representation of a blank node.
63
    #
64
    # @param  [RDF::Node] value
65
    # @param  [Hash{Symbol => Object}] options
66
    # @return [String]
67
    def format_node(value, **options)
2✔
68
      value.to_rdf_json.to_json
×
69
    end
70

71
    ##
72
    # Returns the RDF/JSON representation of a URI reference.
73
    #
74
    # @param  [RDF::URI] value
75
    # @param  [Hash{Symbol => Object}] options
76
    # @return [String]
77
    def format_uri(value, **options)
2✔
78
      value.to_rdf_json.to_json
×
79
    end
80

81
    ##
82
    # Returns the RDF/JSON representation of a literal.
83
    #
84
    # @param  [RDF::Literal, String, #to_s] value
85
    # @param  [Hash{Symbol => Object}] options
86
    # @return [String]
87
    def format_literal(value, **options)
2✔
88
      case value
×
89
        when RDF::Literal then value.to_rdf_json.to_json
×
90
        else RDF::Literal.new(value).to_rdf_json.to_json
×
91
      end
92
    end
93
  end # Writer
94
end # RDF::JSON
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc