Class: Tilia::VObject::Parser::Json
- Inherits:
-
Tilia::VObject::Parser
- Object
- Tilia::VObject::Parser
- Tilia::VObject::Parser::Json
- Defined in:
- lib/tilia/v_object/parser/json.rb
Overview
Json Parser.
This parser parses both the jCal and jCard formats.
Instance Method Summary collapse
-
#input=(input) ⇒ void
Sets the input data.
-
#parse(input = nil, options = 0) ⇒ Document
This method starts the parsing process.
-
#parse_component(j_comp) ⇒ Component
Parses a component.
-
#parse_property(j_prop) ⇒ \Sabre\VObject\Property
Parses properties.
Instance Method Details
#input=(input) ⇒ void
This method returns an undefined value.
Sets the input data.
130 131 132 133 134 135 |
# File 'lib/tilia/v_object/parser/json.rb', line 130 def input=(input) input = input.read unless input.is_a?(String) input = JSON.parse(input) @input = input end |
#parse(input = nil, options = 0) ⇒ Document
This method starts the parsing process.
If the input was not supplied during construction, it’s possible to pass it here instead.
If either input or options are not supplied, the defaults will be used.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/tilia/v_object/parser/json.rb', line 20 def parse(input = nil, = 0) self.input = input unless input.nil? if @input.nil? fail Tilia::VObject::EofException, 'End of input stream, or no input supplied' end @options = if 0 != case @input[0] when 'vcalendar' @root = Tilia::VObject::Component::VCalendar.new({}, false) when 'vcard' @root = Tilia::VObject::Component::VCard.new({}, false) else fail Tilia::VObject::ParseException, 'The root component must either be a vcalendar, or a vcard' end @input[1].each do |prop| @root.add(parse_property(prop)) end if @input[2] @input[2].each do |comp| @root.add(parse_component(comp)) end end # Resetting the input so we can throw an feof exception the next time. @input = nil @root end |
#parse_component(j_comp) ⇒ Component
Parses a component.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/tilia/v_object/parser/json.rb', line 57 def parse_component(j_comp) properties = j_comp[1].map do |j_prop| parse_property(j_prop) end if j_comp[2] components = j_comp[2].map do |j| parse_component(j) end else components = [] end @root.create_component( j_comp[0], components + properties, false ) end |
#parse_property(j_prop) ⇒ \Sabre\VObject\Property
Parses properties.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/tilia/v_object/parser/json.rb', line 82 def parse_property(j_prop) ( property_name, parameters, value_type ) = j_prop property_name = property_name.upcase # This is the default class we would be using if we didn't know the # value type. We're using this value later in this function. default_property_class = @root.class_name_for_property_name(property_name) # parameters = (array)parameters value = j_prop[3..-1] value_type = value_type.upcase if parameters.key?('group') property_name = parameters['group'] + '.' + property_name parameters.delete('group') end prop = @root.create_property(property_name, nil, parameters, value_type) prop.json_value = value # We have to do something awkward here. FlatText as well as Text # represents TEXT values. We have to normalize these here. In the # future we can get rid of FlatText once we're allowed to break BC # again. if default_property_class == Tilia::VObject::Property::FlatText default_property_class = Tilia::VObject::Property::Text end # If the value type we received (e.g.: TEXT) was not the default value # type for the given property (e.g.: BDAY), we need to add a VALUE= # parameter. prop['VALUE'] = value_type if default_property_class != prop.class prop end |