Class: Post

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(urlstring, apikey, editor = "nano") ⇒ Post

Returns a new instance of Post.



10
11
12
13
14
# File 'lib/post.rb', line 10

def initialize(urlstring, apikey, editor="nano")
  @urlstring = urlstring
  @apikey = apikey
  @editor = editor
end

Instance Attribute Details

#apikeyObject

Returns the value of attribute apikey.



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

def apikey
  @apikey
end

#blogidObject

Returns the value of attribute blogid.



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

def blogid
  @blogid
end

#bodyObject

Returns the value of attribute body.



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

def body
  @body
end

#publication_dateObject

Returns the value of attribute publication_date.



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

def publication_date
  @publication_date
end

#statusObject

Returns the value of attribute status.



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

def status
  @status
end

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

Instance Method Details

#addObject



16
17
18
19
20
21
# File 'lib/post.rb', line 16

def add
  save = RestClient.post @urlstring + "/posts", :api_key=>@apikey, :post => {:title=>title, :body=>body, :publication_date=>publication_date, :status=>status}
  post = XmlSimple.xml_in(save)
  file_contents = File.new("/tmp/"+title).read
  puts "Blog post has been saved with ID " + post["id"].first["content"]
end

#destroy(sel) ⇒ Object



134
135
136
137
# File 'lib/post.rb', line 134

def destroy(sel)
  save = RestClient.delete @urlstring + "/posts/" + sel, :params=>{:api_key => @apikey}
  puts "Your post has been deleted."
end

#edit(action, sel) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
# File 'lib/post.rb', line 61

def edit(action, sel)
  xml = RestClient.get @urlstring + "/posts/" + sel + "/edit", :params=>{:api_key => @apikey}
  post = XmlSimple.xml_in(xml)
  body = post["body"].first
  title = post["title"].first
  
  if action == "body"
    # edit chosen doc
    filename = "/tmp/"+title
    tempfile = File.new(filename, "w")
    tempfile.write body
    tempfile.close
    puts "Editing with " + @editor
    eval "#{@editor} '#{filename}'"
  
    newbody = File.new(filename, "r").read
    save = RestClient.put @urlstring + "/posts/" + sel, :api_key => @apikey, :post => {:body=>newbody}
    
  elsif action == "title"
    print "Enter new title [" + title + "]: "
    newtitle = STDIN.gets.chomp
    if newtitle != title && newtitle != ""
      save = RestClient.put @urlstring + "/posts/" + sel, :api_key => @apikey, :post => {:title=>newtitle}
    end
    
  elsif action == "pubdate"
    print "Enter new publication date [YYYY-MM-DD]: "
    newdate = STDIN.gets.chomp
    parseddate = Time.parse(newdate)
    if parseddate
      save = RestClient.put @urlstring + "/posts/" + sel, :api_key => @apikey, :post => {:publication_date=>parseddate}
    else
      puts "Incorrect date format. Please try again."
    end
  
  elsif action == "categories"
    # get all categories
    xml = RestClient.get @urlstring + "/categories", :params=>{:api_key => @apikey}
    post = XmlSimple.xml_in(xml)
    post["category"].each do |c|
      puts "[" + c["sequence"].first["content"] + "] " + c["name"].first
    end
    puts "Enter a comma-separated list of category IDs: "
    cats = STDIN.gets.chomp
    cats = cats.gsub(" ", "").split(",")
    
    save = RestClient.put @urlstring + "/posts/" + sel, :api_key => @apikey, :categories=>cats
  
  end
  
  puts "Blog post has been saved!"
  
end

#list(action, id = nil) ⇒ Object



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
51
52
53
54
55
56
57
58
59
# File 'lib/post.rb', line 23

def list(action, id=nil)
  if action == "posts"
    docs = RestClient.get @urlstring + "/posts", :params=>{:api_key=>@apikey}
    posts = XmlSimple.xml_in(docs)
    if posts["post"].nil? == false 
      posts["post"].each do |d|
        pubdate = Time.parse(d["publication-date"].first["content"])
        puts "[" + d["sequence"].first["content"] + "] " + d["title"].first + " - " + pubdate.strftime("%Y-%m-%d") + " - " + d["status"].first
      end
    else 
      puts "No postings yet. Use the new command to create your first!"
    end
  elsif action == "categories"
    xml = RestClient.get @urlstring + "/categories", :params=>{:api_key => @apikey}
    post = XmlSimple.xml_in(xml)
    post["category"].each do |c|
      puts "[" + c["sequence"].first["content"] + "] " + c["name"].first
    end
  elsif action == "assets"
    if id.nil?
      xml = RestClient.get @urlstring + "/assets", :params=>{:api_key => @apikey}
    else
      xml = RestClient.get @urlstring + "/assets", :params=>{:api_key => @apikey, :id => id}
    end
    assets = XmlSimple.xml_in(xml)

    #require 'ap'

    assets["array"].first["array"].each do |a|
      if a.nil? == false
       # ap(a)
       puts "[" + a["sequence"].first["content"] + "] - " + a["asset-file-name"].first + " - " + "http://shabng.com/assets/" + a["post-id"].first["content"] + "/" + a["sequence"].first["content"]
      end
    end
  end
  
end

#newcat(cat) ⇒ Object



115
116
117
118
# File 'lib/post.rb', line 115

def newcat(cat)
  save = RestClient.post @urlstring + "/categories", :api_key => @apikey, :category=>cat
  puts "New category has been saved!"
end

#pull(sel) ⇒ Object



129
130
131
132
# File 'lib/post.rb', line 129

def pull(sel)
  save = RestClient.put @urlstring + "/posts/" + sel, :api_key => @apikey, :post => {:status=>"Draft"}
  puts "Your post has been pulled."
end

#push(sel) ⇒ Object



124
125
126
127
# File 'lib/post.rb', line 124

def push(sel)
  save = RestClient.put @urlstring + "/posts/" + sel, :api_key => @apikey, :post => {:status=>"Published"}
  puts "Your post is now live."
end

#upload(asset, id) ⇒ Object



120
121
122
# File 'lib/post.rb', line 120

def upload(asset, id)
  xml = RestClient.post @urlstring + "/assets", :api_key => @apikey, :post=>{:id=>id, :asset=>File.new(asset, "rb")}
end