0
+# Author: Stefan Saasen <s@juretta.com>
0
+$: << File.join(File.split(__FILE__).first, '.')
0
+ class PrefsError < SystemCallError; end
0
+ # ============================================================================================
0
+ ATTACHR_PREFS = File.expand_path("~/Library/Preferences/com.macromates.textmate.attachr.txt")
0
+ attr_writer :remote_api
0
+ def initialize(remote_api = nil)
0
+ unless remote_api.nil?
0
+ @remote_api = remote_api
0
+ @remote_api = Attachr::API::Remote.new
0
+ # fetch snippet from attachr.com
0
+ def insert_snippet_from_attachr
0
+ options = TmDialog.new.open_insert_dialog
0
+ url_or_id = options['url_or_id']
0
+ selected_id = options['selectedSnippet']
0
+ url_or_id = selected_id.gsub(/#/, "") unless selected_id.nil?
0
+ return "" if url_or_id.nil?
0
+ if url_or_id.match(/([0-9a-z]{32})/i)
0
+ elsif url_or_id.match(/([0-9]+)/)
0
+ elsif url_or_id.match(/attachr\.com\/.+/)
0
+ return @remote_api.fetch_plain_from_url(url_or_id)
0
+ return "invalid attachr URL/id"
0
+ return @remote_api.fetch(id, private)
0
+ api_key = self.class::standard_input_box("Insert API-Key", "Please insert your Api-Key")
0
+ return false if api_key.nil?
0
+ write_api_key(api_key)
0
+ # Post snippet to attachr.com using the API-Key
0
+ def post_to_attachr_account
0
+ api_key = self.class::standard_input_box("Insert API-Key", "Please insert your Api-Key")
0
+ return "" if api_key.nil?
0
+ write_api_key(api_key)
0
+ _post_to_attachr(api_key)
0
+ def _post_to_attachr(api_key=nil)
0
+ if ENV['TM_SCOPE'].scan(/(text|source)\.([^\.]+)/).flatten.first
0
+ text = ENV['TM_SELECTED_TEXT']
0
+ return nil if text.nil? || text.empty?
0
+ @remote_api.api_key = api_key
0
+ @remote_api.attach(text, filter)
0
+ raise("Can't get Api-Key.") unless File.file?(ATTACHR_PREFS)
0
+ IO.readlines(ATTACHR_PREFS).each do |line|
0
+ if line =~ /^API-KEY:(.+)/
0
+ raise "Can't get Api-Key."
0
+ def write_api_key(api_key)
0
+ File.open(ATTACHR_PREFS, "w") do |f|
0
+ f << "API-KEY:#{api_key}"
0
+ result = Attachr::Textmate.new.insert_snippet_from_attachr
0
\ No newline at end of file
Comments
No one has commented yet.