Class: Tumblr::CommandLineInterface

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/tumblr/command_line_interface.rb

Instance Method Summary collapse

Instance Method Details

#authorize(*soak) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/tumblr/command_line_interface.rb', line 196

def authorize(*soak)
  require 'tumblr/authentication'
  require 'launchy'
  sinatra_options = {
    :port => options[:port],
    :bind => options[:bind],
    :credential_path => options[:credentials]
  }
  Tumblr::Authentication.run!(sinatra_options) do |server|
    ::Launchy.open("http://#{options[:bind]}:#{options[:port]}/")
  end
  if has_credentials?
    ui_success "Success! Your Tumblr OAuth credentials were written to #{credentials.path}"
  else
    ui_abort "Something went wrong in authorization, and credentials were not correctly written to #{credentials.path}"
  end
end

#backup(dir = Dir.getwd, offset = 0) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/tumblr/command_line_interface.rb', line 147

def backup(dir = Dir.getwd, offset = 0)
  backup_dir = File.expand_path(dir)
  unless File.directory?(backup_dir) && File.writable?(backup_dir)
    ui_abort "Can't write to directory: #{backup_dir}"
  end
  client = get_client
  response = client.posts(:filter => :raw, :offset => offset).perform
  tumblr_error(response) unless response.success?
  json = response.parse["response"]
  total_post_count = json["blog"]["posts"]

  ui_puts "Downloading #{total_post_count} posts to #{backup_dir}." if offset.zero?

  posts = json["posts"].map{|post| Tumblr::Post.create(post) }
  posts.each do |post|
    file_name = post.id.to_s + (post.slug.empty? ? "" : "_#{post.slug}") + ".txt"
    File.open(File.join(backup_dir, file_name), "w") do |f|
      ui_puts "Writing file: #{f.path} for #{post.type} post #{post.id}."
      f.write(post.serialize)
    end
  end

  new_offset = offset + posts.size
  if total_post_count > new_offset
    backup(dir, new_offset)
  else
    ui_success "Downloaded #{new_offset}/#{total_post_count} posts to #{backup_dir}"
  end
end

#delete(id) ⇒ Object



122
123
124
125
126
127
# File 'lib/tumblr/command_line_interface.rb', line 122

def delete(id)
  client = get_client
  response = client.delete(:id => id).perform
  tumblr_error(response) unless response.success?
  ui_success "Post #{id} successfully deleted."
end

#edit(id) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/tumblr/command_line_interface.rb', line 90

def edit(id)
  client = get_client
  response = client.posts(:id => id, :filter => :raw).perform
  tumblr_error(response) unless response.success?
  post = Tumblr::Post.create(response.parse["response"]["posts"].first)
  require 'tempfile'
  tmp_file = Tempfile.new("post_#{id}")
  tmp_file.write(post.serialize)
  tmp_file.rewind
  ui_abort "Something went wrong editing the post." unless system "#{editor} #{tmp_file.path}"
  edited_post = Tumblr::Post.load_from_path tmp_file.path
  edited_response = edited_post.edit(client).perform
  tumblr_error(edited_response) unless edited_response.success?
  ui_success "Post #{id} successfully edited."
ensure
  if tmp_file
    tmp_file.close
    tmp_file.unlink
  end
end

#fetch(id) ⇒ Object



112
113
114
115
116
117
118
119
# File 'lib/tumblr/command_line_interface.rb', line 112

def fetch(id)
  client = get_client
  response = client.posts(:id => id, :filter => :raw).perform
  tumblr_error(response) unless response.success?
  post = Tumblr::Post.create(response.parse["response"]["posts"].first)
  puts post.serialize
  post
end

#listObject



134
135
136
137
138
# File 'lib/tumblr/command_line_interface.rb', line 134

def list()
  client = get_client
  posts = Tumblr::Post.perform client.posts(:tag => options[:tag], :type => options[:type], :filter => :text)
  ui_puts Hash[ posts.map {|post| [post.id, post.post_url]} ].to_yaml
end

#pipe(*args) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/tumblr/command_line_interface.rb', line 36

def pipe(*args)
  if !$stdin.tty?
    puts post($stdin).serialize
  else
    invoke :help
  end
end

#post(arg) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/tumblr/command_line_interface.rb', line 64

def post(arg)
  client = get_client
  post =  if arg.respond_to? :read
            Tumblr::Post.load arg.read
          elsif File.file?(file_path = File.expand_path(arg))
            Tumblr::Post.load_from_path file_path
          else
            Tumblr::Post.load arg.to_s
          end
  post.publish! if options[:publish]
  post.queue! if options[:queue]
  post.draft! if options[:draft]
  response = post.post(client).perform
  tumblr_error(response) unless response.success?
  ui_success %Q(Post was successfully created! Post ID: #{response.parse["response"]["id"]}) if $stdin.tty?
  post
end

#versionObject



215
216
217
# File 'lib/tumblr/command_line_interface.rb', line 215

def version
  puts Tumblr::VERSION
end