public
Description: A Tumblr client written in Shoes
Homepage: http://yapok.org/
Clone URL: git://github.com/madx/shumblr.git
shumblr / shumblr.rb
100644 177 lines (159 sloc) 4.976 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
60
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
176
177
#!/usr/bin/env shoes
 
$:.unshift(path = File.join(File.dirname(__FILE__), 'lib')) unless
  $:.include?(path)
 
require 'tumblr'
 
Shoes.app :title => "Shumblr.", :width => 700, :height => 600 do
  style Shoes::Para, :stroke => white
  style Shoes::Title, :stroke => white, :font => '24px'
 
  background '#334668'
  background 'img/background.png', :height => 501, :width => 1.0
 
  @actions = {
    :text => {
      :fields => {
        :body => [:required, :text],
        :title => [:optional, :line]
      },
      :method => :regular
    },
    :quote => {
      :fields => {
        :text => [:required, :text],
        :source => [:optional, :text]
      },
      :method => :quote
    },
    :photo => {
      :fields => {
        :source => [:required, :line],
        :caption => [:optional, :text]
      },
      :method => :photo,
    },
    :video => {
      :fields => {
        :embed => [:required, :line],
        :caption => [:optional, :text]
      },
      :method => :video
    },
    :link => {
      :fields => {
        :url => [:required, :line],
        :name => [:optional, :line],
        :description => [:optional, :text]
      },
      :method => :link
    },
    :chat => {
      :fields => {
        :conversation => [:required, :text],
        :title => [:optional, :line]
      },
      :method => :conversation
    }
  }
 
  def home(&blk)
    @contents.clear do
      para strong("Welcome to Shumblr!\n"),
        "Shumblr is a client for Tumblr.\nYou can post to your Tumblr from ",
        "here without having to open your browser.\n\n",
        em("File uploads are not implemented yet!")
      yield if block_given?
    end
  end
 
  @credentials = flow :margin => 5 do
    @logged = false
    background rgb(255, 255, 128, 0.9), :curve => 5
    para "Credentials please:", :font => "bold", :stroke => black, :margin => 8
    $mail = edit_line "email", :margin => 5
    $pass = edit_line "password", :margin => 5, :secret => true
    button "Ok", :margin => 4 do
      @credentials.hide
      @logged = true
    end
  end
 
  banner "Shumblr.", :font => 'georgia,serif bold', :stroke => '#eee',
      :margin => [10, 5, 10, 5]
 
  
  stack :margin => [5, 0, 5, 5] do
    background rgb(51, 70, 104, 0.5), :curve => 12
    flow :margin => [110, 10, 145, 10] do
      background white, :curve => 5
      @actions.each do |action, opts|
        image "img/#{action}.png", :click => proc { create(action) }
      end
    end
 
    @contents = stack(:margin => 10)
 
    home
  end
 
  def create(action)
    unless @logged
      alert "Please login first!"
    else
 
      @contents.clear do
        @fields = {}
        @actions[action][:fields].each do |field, params|
          if params[0] == :required
            field_text = "#{field.to_s.capitalize}"
          else
            field_text = ["#{field.to_s.capitalize}", em(" (optional)")]
          end
          title field_text
          @fields[field] = case params[1]
            when :line: edit_line :width => 1.0, :margin => [10, 5, 10, 10]
            when :text: edit_box :width => 1.0, :margin => [10, 5, 10, 10]
          end
        end
        @controls = flow do
          button "Post" do
            valid = true
            @actions[action][:fields].select {|k,v|
              v[0] == :required
            }.collect {|f| f[0] }.each do |field|
              if @fields[field].text.empty?
                alert "Field #{field.to_s.capitalize} can't be empty"
                valid = false
              end
            end
 
            @actions[action][:fields].select {|k,v|
              v[0] == :optional
            }.collect {|f| f[0] }.each do |field|
              text = @fields[field].text
              @fields[field].text = text.empty? ? nil : text
            end
 
            if valid
              write_to_api(@actions[action][:method], @fields)
            end
          end
          button("Cancel") { home }
        end
      end
    end
  end
 
  def write_to_api(method, params)
    begin
      Tumblr::API.write($mail.text, $pass.text, "Shumblr") do
        case method
          when :regular
            regular(params[:body].text, params[:title].text)
          when :link
            link(params[:url].text, params[:name].text, params[:description].text)
          when :conversation
            conversation(params[:conversation].text, params[:title].text)
          when :quote
            quote(params[:text].text, params[:source].text)
          when :video
            video(params[:embed].text, params[:caption].text)
          when :photo
            photo(params[:source].text, params[:caption].text)
        end
      end
      home { para "Content published!", :stroke => green, :font => 'bold' }
    rescue Tumblr::API::AuthError
      alert "Authentication error"
      @credentials.show
      @logged = false
    rescue => e
      alert "#{e.class}, not published"
    end
  end
end