Class: Kimotter

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

Defined Under Namespace

Classes: APILimit

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Kimotter

Returns a new instance of Kimotter.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/kimotter.rb', line 26

def initialize(options={})
  default_options = {
    :api => 'http://api.ma.la/twitter/',
    :logger => Logger.new(nil), # nop logger
    :user_agent => 'kimotter'
  }
  options = default_options.merge(options)

  @logger = options[:logger]
  @api = options[:api]

  @agent = Mechanize.new{ |agent|
    agent.user_agent = options[:user_agent]
  }

  @api_limit = APILimit.new
  @confirm_msg = nil

  @logger.info "setup finished"
end

Instance Attribute Details

#confirm_msgObject (readonly)

Returns the value of attribute confirm_msg.



16
17
18
# File 'lib/kimotter.rb', line 16

def confirm_msg
  @confirm_msg
end

Class Method Details

.tweet(message, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
# File 'lib/kimotter.rb', line 18

def self.tweet(message, options={})
  raise ArgumentError unless message

  kimotter = self.new(options)
  kimotter.fetch_api_limit
  kimotter.tweet(message)
end

Instance Method Details

#api_limit?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/kimotter.rb', line 91

def api_limit?
  (@api_limit.used >= @api_limit.limit)
end

#fetch_api_limitObject



47
48
49
50
51
52
53
# File 'lib/kimotter.rb', line 47

def fetch_api_limit
  @logger.info "try to fetch api limit: URL is #{@api.inspect}"
  @agent.get(@api)
  @logger.info "successful fetched api limit"

  load_from_mala_html(@agent.page.body)
end

#filter(message) ⇒ Object

please overwrite this method if you need it



77
78
79
# File 'lib/kimotter.rb', line 77

def filter(message)
  message
end

#load_from_mala_html(content_body) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/kimotter.rb', line 55

def load_from_mala_html(content_body)
  @logger.info "try to parse api limit information"
  doc = Nokogiri(content_body)
  body_text = doc.search("html > body").text

  if body_text =~ /API\s*Limit:\s*(\d+)\s*\/\s*(\d+)/
    @api_limit.used = Regexp.last_match(1).to_i
    @api_limit.limit = Regexp.last_match(2).to_i

    @logger.info "successful parsed api limit"
  else
    raise "not found api limit: #{body_text.inspect}"
  end

  begin
    @confirm_msg = doc.search("body > form > input[@type='checkbox']").first.next.text.chomp
  rescue => ex
    raise "not found confirm_msg: #{ex.inspect}"
  end
end

#tweet(message) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/kimotter.rb', line 81

def tweet(message)
  @logger.info "try to tweet: #{message.inspect}"
  @agent.page.form_with(:action => '/twitter/'){ |form|
    form.field_with(:name => 'status').value = filter(message)
    form.checkbox_with(:name => 'is_kimoto').check
    form.click_button
  }
  @logger.info "successful tweeted"
end