Class: Nwcopy::Client
- Inherits:
-
Object
- Object
- Nwcopy::Client
- Defined in:
- lib/nwcopy/client.rb
Class Attribute Summary collapse
-
.gist ⇒ Object
writeonly
Sets the attribute gist.
Class Method Summary collapse
- .available? ⇒ Boolean
- .copy(data) ⇒ Object
- .paste(url_or_hash = nil) ⇒ Object
- .time ⇒ Object
- .unavailable_message ⇒ Object
Class Attribute Details
.gist=(value) ⇒ Object (writeonly)
Sets the attribute gist
8 9 10 |
# File 'lib/nwcopy/client.rb', line 8 def gist=(value) @gist = value end |
Class Method Details
.available? ⇒ Boolean
15 16 17 |
# File 'lib/nwcopy/client.rb', line 15 def self.available? ENV['NWCOPY_CREDS'] end |
.copy(data) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/nwcopy/client.rb', line 23 def self.copy data if data.respond_to? :filename filename = data.filename else filename = nil @tmpfile = Tempfile.new 'nwcopy' @tmpfile.write(data.read) @tmpfile.close data = File.open @tmpfile.path end io = UploadIO.new(data, nil, filename) url = URI.parse("#{base_url}/copy") req = Net::HTTP::Post::Multipart.new url.path, 'data' => io res = start req, url res.body ensure @tmpfile.unlink if @tmpfile @tmpfile = nil end |
.paste(url_or_hash = nil) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/nwcopy/client.rb', line 47 def self.paste url_or_hash = nil url = if url_or_hash hash = url_or_hash.split('/').last URI.parse("#{base_url}/pastes/#{hash}") else URI.parse("#{base_url}/paste") end req = Net::HTTP::Get.new(url.path) res = start req, url file = res['Location'] ? res['Location'].split('/').last : nil if file && !File.exists?(file) File.open file, 'w+' do |f| f << res.body end "Pasted to #{file}" else res.body end end |
.time ⇒ Object
19 20 21 |
# File 'lib/nwcopy/client.rb', line 19 def self.time Time.now end |
.unavailable_message ⇒ Object
11 12 13 |
# File 'lib/nwcopy/client.rb', line 11 def self. "nwcopy is not configured. Go to nwcopy.heroku.com. Then set $NWCOPY_CREDS." end |