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



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/tumblr/command_line_interface.rb', line 148

def authorize(*soak)
  require 'tumblr/authentication'
  sinatra_options = {
    :port => options[:port],
    :bind => options[:bind],
    :credential_path => options[:credentials]
  }
  Tumblr::Authentication.run!(sinatra_options) do |server|
    `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

#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

#pipeObject



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

def pipe
  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



166
167
168
# File 'lib/tumblr/command_line_interface.rb', line 166

def version
  puts Tumblr::VERSION
end