Class: Trahald::Article

Inherits:
Object
  • Object
show all
Defined in:
lib/trahald/article.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, body, date) ⇒ Article

Returns a new instance of Article.



7
8
9
10
11
# File 'lib/trahald/article.rb', line 7

def initialize(name, body, date)
  @name = name
  @body = body
  @date = date
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



6
7
8
# File 'lib/trahald/article.rb', line 6

def body
  @body
end

#dateObject (readonly)

Returns the value of attribute date.



6
7
8
# File 'lib/trahald/article.rb', line 6

def date
  @date
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/trahald/article.rb', line 6

def name
  @name
end

Class Method Details

.from_json(json) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/trahald/article.rb', line 25

def Article.from_json(json)
  begin
    h = JSON.parse json
    Article.new(h["name"], h["body"], h["date"])
  rescue exception
    "Json parse error."
  end
end

Instance Method Details

#to_hObject



13
14
15
16
17
18
19
# File 'lib/trahald/article.rb', line 13

def to_h
  {
    :name => @name,
    :body => @body,
    :date => @date
  }
end

#to_jsonObject



21
22
23
# File 'lib/trahald/article.rb', line 21

def to_json
  JSON.generate to_h
end