Class: WpRpc::Post

Inherits:
Base
  • Object
show all
Defined in:
lib/wp_rpc/post.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#change_attributes, #conn, #pattern

Constructor Details

#initialize(attributes = { }, options = { }) ⇒ Post

Returns a new instance of Post.



8
9
10
11
# File 'lib/wp_rpc/post.rb', line 8

def initialize(attributes = { }, options = { })
  super(options)
  self.attributes = attributes
end

Instance Attribute Details

#categoriesObject

Returns the value of attribute categories.



6
7
8
# File 'lib/wp_rpc/post.rb', line 6

def categories
  @categories
end

#contentObject

Returns the value of attribute content.



4
5
6
# File 'lib/wp_rpc/post.rb', line 4

def content
  @content
end

#created_atObject

Returns the value of attribute created_at.



5
6
7
# File 'lib/wp_rpc/post.rb', line 5

def created_at
  @created_at
end

#keywordsObject

Returns the value of attribute keywords.



6
7
8
# File 'lib/wp_rpc/post.rb', line 6

def keywords
  @keywords
end

#publishedObject

Returns the value of attribute published.



6
7
8
# File 'lib/wp_rpc/post.rb', line 6

def published
  @published
end

#titleObject

Returns the value of attribute title.



4
5
6
# File 'lib/wp_rpc/post.rb', line 4

def title
  @title
end

Instance Method Details

#attributesObject



76
77
78
79
80
81
82
83
84
# File 'lib/wp_rpc/post.rb', line 76

def attributes
  h = { }
  h[:title] = title if title
  h[:description] = content if content
  h[:dateCreated] = created_at if created_at
  h[:mt_keywords] = keywords.join(",")
  h[:categories] = categories if categories
  h
end

#attributes=(attr) ⇒ Object



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

def attributes=(attr)
  symbolized_attr = { }
  attr = attr.each { |k,v| symbolized_attr[k.to_sym] = v }
  attr = symbolized_attr
  @title = attr[:title]
  @keywords = attr[:keywords].to_s.split(/,|;/).collect { |k| k.strip }
  @categories = attr[:categories]
  @content = attr[:content]
  @created_at = attr[:dateCreated]
  @id = attr[:postid]
  @userid = attr[:userid]
  @published = attr[:published]
end

#createObject



40
41
42
43
# File 'lib/wp_rpc/post.rb', line 40

def create
  @id = conn.new_post(attributes, published)
  self
end

#idObject



27
28
29
# File 'lib/wp_rpc/post.rb', line 27

def id
  @id
end

#publishObject



45
46
47
48
# File 'lib/wp_rpc/post.rb', line 45

def publish
  self.published = true
  save
end

#reloadObject



86
87
88
89
90
91
92
93
# File 'lib/wp_rpc/post.rb', line 86

def reload
  if id
    saved_id = id
    self.attributes = conn.posts.find(id).attributes
    @id = saved_id
    true
  end
end

#saveObject



31
32
33
# File 'lib/wp_rpc/post.rb', line 31

def save
  id ? update : create
end

#updateObject



35
36
37
38
# File 'lib/wp_rpc/post.rb', line 35

def update
  conn.edit_post(id, attributes, published)
  self
end