Class: Tinatra

Inherits:
Object
  • Object
show all
Includes:
Singleton, Helpers
Defined in:
lib/tinatra.rb

Defined Under Namespace

Modules: Helpers

Constant Summary collapse

API_BASE =
'http://api.twitter.com/'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTinatra

Returns a new instance of Tinatra.



56
57
58
59
60
61
# File 'lib/tinatra.rb', line 56

def initialize
  @config = {:rubytter => OAuthRubytter}
  @actions = {}
  @db = nil
  @t = nil
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



204
205
206
# File 'lib/tinatra.rb', line 204

def config
  @config
end

Class Method Details

.method_missing(name, *args) ⇒ Object



200
201
202
# File 'lib/tinatra.rb', line 200

def self.method_missing(name, *args)
  Tinatra.instance.__send__(name, *args)
end

Instance Method Details

#add_action(event, block) ⇒ Object



139
140
141
142
# File 'lib/tinatra.rb', line 139

def add_action(event, block)
  @actions[event] ||= []
  @actions[event] << block
end

#apiObject



186
187
188
189
# File 'lib/tinatra.rb', line 186

def api
  init_twit unless @t
  @t
end

#authorizeObject



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
# File 'lib/tinatra.rb', line 95

def authorize
  puts "------------ AUTHORIZING ------------"
  init_db
  @db.transaction do |d|
    if d[:consumer]
      puts "You're already setted consumer key/secret."
      cons_again = HighLine.new.agree("Input consumer key/secret again? ")
    else
      cons_again = true
    end

    if cons_again
      cons = []
      cons << HighLine.new.ask("Input your consumer key: ")
      cons << HighLine.new.ask("Input your consumer secret: ")
      d[:consumer] = OAuth::Consumer.new(cons[0],cons[1], :site => API_BASE)
    end

    puts

    request_token = d[:consumer].get_request_token
    puts "Access This URL and press 'Allow' in account for tinatra => #{request_token.authorize_url}"
    pin = HighLine.new.ask('Input key shown by twitter: ')
    access_token = request_token.get_access_token(
      :oauth_verifier => pin
    )
    d[:token] = access_token#[access_token.token.dup,access_token.secret.dup]

    @t = @config[:rubytter].new(access_token)
    d[:self] = eval(@t.verify_credentials.inspect)

    puts
    puts "Authorizing is done."
  end
end

#call_action(event, *args) ⇒ Object



144
145
146
147
148
149
# File 'lib/tinatra.rb', line 144

def call_action(event, *args)
  @actions[event] ||= []
  @actions[event].each do |a|
    a.yield(*args)
  end
end

#dbObject



191
192
193
194
# File 'lib/tinatra.rb', line 191

def db
  init_db unless @db
  @db
end

#parse_optionObject



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
# File 'lib/tinatra.rb', line 63

def parse_option
  init = false
  help = false
  ARGV.each do |a|
    case a
    when "--init"
      init = true
      help = false
    when /--db=(.+)/
      set :db, $1
    when "--help"
      help = true
      init = false
    end
  end

  if init
    authorize
    exit
  end
  if help
    puts <<-EOH
Usage: #{File.basename($0)} [--db=DATABASE] [--init|--help]

--db   -- set a path to database file.
--init -- authorize an account
--help -- show this message
    EOH
    exit
  end
end

#resetObject



131
132
133
# File 'lib/tinatra.rb', line 131

def reset
  initialize
end

#runObject



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
178
179
180
181
182
183
184
# File 'lib/tinatra.rb', line 151

def run
  init_db
  init_twit

  call_action(:always)

  [[:mention, :replies], [:timeline, :home_timeline],
   [:direct_message, :direct_messages]].each do |act|
    if @actions[act[0]]
      r = @t.__send__(act[1])
      @db.transaction do |d|
        (r - d[act[0]]).each do |new|
          call_action(act[0],new)
        end
        d[act[0]] = eval(r.inspect)
      end
    end
  end
  
  @db.transaction do |d|
    if @actions[:followed]||@actions[:removed]
      f = @t.followers_ids(d[:self][:id])
      [[:followed,(f-d[:follower])],
       [:removed,(d[:follower]-f)]].each do |x|
        if @actions[x[0]]
          x[1].each do |n|
            call_action(x[0],@t.user(n))
          end
        end
      end
      d[:follower] = eval(f.inspect)
    end
  end
end

#set(a, b) ⇒ Object



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

def set(a,b)
  @config[a]=b
end