Class: Content

Inherits:
Object
  • Object
show all
Defined in:
lib/myer/content.rb

Defined Under Namespace

Classes: Item

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeContent

Returns a new instance of Content.



24
25
26
# File 'lib/myer/content.rb', line 24

def initialize
  @items = []
end

Instance Attribute Details

#titleObject (readonly)

Returns the value of attribute title.



22
23
24
# File 'lib/myer/content.rb', line 22

def title
  @title
end

#typeObject (readonly)

Returns the value of attribute type.



22
23
24
# File 'lib/myer/content.rb', line 22

def type
  @type
end

Instance Method Details

#add(content) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/myer/content.rb', line 28

def add(content)
  json = JSON.parse(content)

  item = Item.new(self)
  item.id = json["id"]
  item.written_at = json["written_at"]
  item.tag = json["tag"]
  if item.tag == "title"
    @title = json["data"]
  elsif item.tag == "type"
    @type = json["data"]
  else
    item.data = json["data"]
    @items.push(item)
  end
end

#at(index) ⇒ Object



49
50
51
# File 'lib/myer/content.rb', line 49

def at(index)
  @items.at(index)
end

#empty?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/myer/content.rb', line 53

def empty?
  @items.empty?
end

#firstObject



45
46
47
# File 'lib/myer/content.rb', line 45

def first
  at(0)
end

#lengthObject



57
58
59
# File 'lib/myer/content.rb', line 57

def length
  @items.length
end

#write_as_csv(output_path) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/myer/content.rb', line 61

def write_as_csv(output_path)
  File.open(output_path, "w") do |file|
    @items.each do |item|
      if type == "json"
        file.puts(item.data.join(","))
      else
        file.puts(item.data)
      end
    end
  end
end

#write_as_json(output_path) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/myer/content.rb', line 73

def write_as_json(output_path)
  json = {}
  json["title"] = title

  data_array = []
  @items.each do |item|
    data_item = {}
    data_item["date"] = item.data[0]
    data_item["value"] = item.data[1]

    data_array.push(data_item)
  end

  json["data"] = data_array

  File.open(output_path, "w") do |file|
    file.write(json.to_json)
  end
end