Class: Swicky::JsonEncoder

Inherits:
Object
  • Object
show all
Defined in:
lib/swicky/json_encoder.rb

Overview

Takes a number of triples and encodes them in the SIMILE JSON format

Instance Method Summary collapse

Constructor Details

#initialize(triples) ⇒ JsonEncoder

Returns a new instance of JsonEncoder.



6
7
8
9
10
11
12
# File 'lib/swicky/json_encoder.rb', line 6

def initialize(triples)
  @triples = triples
  @types_hash = {}
  @properties_hash = {}
  @label_hash = {}
  @label_inverse = {}
end

Instance Method Details

#to_jsonObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/swicky/json_encoder.rb', line 14

def to_json
  @items ||= begin
    items = []
    # First a round to make sure that each item has a label
    triple_hash.each { |object, values| values['label'] = make_label!(object, values) }
    # Now build the items themselves
    triple_hash.each { |object, values| items += build_item(object, values) }
    items
  end
  hashy = { 'items' => @items, 'types' => @types_hash, 'properties' => @properties_hash }
  { 'items' => @items, 'types' => @types_hash, 'properties' => @properties_hash }.to_json
end