Class: Kimotter
- Inherits:
-
Object
- Object
- Kimotter
- Defined in:
- lib/kimotter.rb
Defined Under Namespace
Classes: APILimit
Instance Attribute Summary collapse
-
#confirm_msg ⇒ Object
readonly
Returns the value of attribute confirm_msg.
Class Method Summary collapse
Instance Method Summary collapse
- #api_limit? ⇒ Boolean
- #fetch_api_limit ⇒ Object
-
#filter(message) ⇒ Object
please overwrite this method if you need it.
-
#initialize(options = {}) ⇒ Kimotter
constructor
A new instance of Kimotter.
- #load_from_mala_html(content_body) ⇒ Object
- #tweet(message) ⇒ Object
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(={}) = { :api => 'http://api.ma.la/twitter/', :logger => Logger.new(nil), # nop logger :user_agent => 'kimotter' } = .merge() @logger = [:logger] @api = [:api] @agent = Mechanize.new{ |agent| agent.user_agent = [:user_agent] } @api_limit = APILimit.new @confirm_msg = nil @logger.info "setup finished" end |
Instance Attribute Details
#confirm_msg ⇒ Object (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
18 19 20 21 22 23 24 |
# File 'lib/kimotter.rb', line 18 def self.tweet(, ={}) raise ArgumentError unless kimotter = self.new() kimotter.fetch_api_limit kimotter.tweet() end |
Instance Method Details
#api_limit? ⇒ Boolean
91 92 93 |
# File 'lib/kimotter.rb', line 91 def api_limit? (@api_limit.used >= @api_limit.limit) end |
#fetch_api_limit ⇒ Object
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() 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() @logger.info "try to tweet: #{.inspect}" @agent.page.form_with(:action => '/twitter/'){ |form| form.field_with(:name => 'status').value = filter() form.checkbox_with(:name => 'is_kimoto').check form. } @logger.info "successful tweeted" end |