PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/plugins/open_url.rb

https://github.com/Epictetus/termtter
Ruby | 47 lines | 40 code | 4 blank | 3 comment | 2 complexity | 7838af5fa632b5aaea5b0d8beb175fc6 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. require 'uri'
  3. module Termtter::Client
  4. def self.open_uri(uri)
  5. unless config.plugins.open_url.browser.empty?
  6. system config.plugins.open_url.browser, uri
  7. else
  8. case RUBY_PLATFORM
  9. when /linux/
  10. system 'xdg-open', uri
  11. when /mswin(?!ce)|mingw|bccwin/
  12. system 'explorer', uri
  13. else
  14. system 'open', uri
  15. end
  16. end
  17. end
  18. register_command(
  19. :name => :open_url,
  20. :help => ['open_url (TYPABLE|ID|@USER)', 'Open url'],
  21. :exec_proc => lambda {|arg|
  22. Thread.new(arg) do |arg|
  23. if status = Termtter::Client.typable_id_to_data(arg)
  24. status.text.gsub(URI.regexp(['http', 'https'])) {|uri|
  25. open_uri(uri)
  26. }
  27. else
  28. case arg
  29. when /^@([A-Za-z0-9_]+)/
  30. user = $1
  31. statuses = Termtter::API.twitter.user_timeline(user)
  32. return if statuses.empty?
  33. statuses[0].text.gsub(URI.regexp(['http', 'https'])) {|uri| open_uri(uri) }
  34. when /^\d+/
  35. Termtter::API.twitter.show(arg).text.gsub(URI.regexp(['http', 'https'])) {|uri| open_uri(uri) }
  36. end
  37. end
  38. end
  39. }
  40. )
  41. end
  42. #Optional Setting
  43. # config.plugins.open_url.browser = 'firefox'