haste 0.1.7 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 35ecd77b0c99c20e84af80349307159f61188056
4
- data.tar.gz: 6afd2dbe12aa5828f801154cbf4f7f59f9717dde
3
+ metadata.gz: e8d393fa8a36797aa277fe3502fe30cd962a8137
4
+ data.tar.gz: eebf771976a46c47799fa14e97f2364c2a57a712
5
5
  SHA512:
6
- metadata.gz: 9e741f91d208c9c25ba18dfac316bd0a211928006651c8785d2b5108ecc8b8ac0682642a66270b4f155a1c8a0747350d77a0305d2f572d89337963ad97e91c1a
7
- data.tar.gz: 375136cec75a90147bb61786fb634ac8faacbb9c84541d7418fdafd5f509571fac0d1c5481c97b04de3b692899bbc7ff0b2c0c85c8b1951e712af1c9e7b2fe6c
6
+ metadata.gz: 3aab22c47efba69d19b460d0c269401018bc67c37ae061c05ced7b481fc0255738cd239d366bbf12eec2abb249392f42c6465bdbfd881a0a3d7476c43f586a90
7
+ data.tar.gz: 8efa9bb61f312ab51948cb8da0d5253fb6a5c47facb857a6f9e5c4401e1f8e6d42cc6387d3f48a70f88ceab2800f93975a87f99b118a5b6473f40b4c89d96099
data/bin/haste CHANGED
@@ -1,5 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
+ require 'bundler/setup'
4
+
3
5
  require 'pathname'
4
6
  path = Pathname.new(__FILE__)
5
7
  require File.expand_path(File.dirname(path.realpath) + '/../lib/haste')
@@ -1,59 +1,6 @@
1
- require 'json'
2
- require 'net/http'
3
- require 'net/https'
4
- require 'uri'
1
+ require File.dirname(__FILE__) + '/haste/uploader'
2
+ require File.dirname(__FILE__) + '/haste/exception'
3
+ require File.dirname(__FILE__) + '/haste/cli'
5
4
 
6
5
  module Haste
7
-
8
- DEFAULT_URL = 'http://hastebin.com'
9
-
10
- class CLI
11
-
12
- # Pull all of the data from STDIN
13
- def initialize
14
- if STDIN.tty?
15
- file = ARGV.first
16
- abort 'No input file given' unless file
17
- abort "#{file}: No such path" unless File.exists?(file)
18
- @input = open(file).read
19
- else
20
- @input = STDIN.readlines.join
21
- end
22
- # clean up
23
- @input.rstrip!
24
- end
25
-
26
- # Upload the and output the URL we get back
27
- def start
28
- uri = URI.parse server
29
- http = Net::HTTP.new uri.host, uri.port
30
- if uri.scheme =~ /^https/
31
- http.use_ssl = true
32
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
33
- end
34
- response = http.post '/documents', @input
35
- if response.is_a?(Net::HTTPOK)
36
- data = JSON.parse(response.body)
37
- method = STDOUT.tty? ? :puts : :print
38
- STDOUT.send method, "#{server}/#{data['key']}"
39
- else
40
- abort "failure uploading: #{response.code}"
41
- end
42
- rescue JSON::ParserError => e
43
- abort "failure parsing response: #{e.message}"
44
- rescue Errno::ECONNREFUSED => e
45
- abort "failure connecting: #{e.message}"
46
- end
47
-
48
- private
49
-
50
- def server
51
- return @server if @server
52
- @server = (ENV['HASTE_SERVER'] || Haste::DEFAULT_URL).dup
53
- @server.chop! if server.end_with?('/')
54
- @server
55
- end
56
-
57
- end
58
-
59
6
  end
@@ -0,0 +1,36 @@
1
+ module Haste
2
+
3
+ class CLI
4
+
5
+ # Create a new uploader
6
+ def initialize
7
+ @uploader = Uploader.new ENV['HASTE_SERVER']
8
+ end
9
+
10
+ # And then handle the basic usage
11
+ def start
12
+ # Take data in
13
+ if STDIN.tty?
14
+ key = @uploader.upload_path ARGV.first
15
+ else
16
+ key = @uploader.upload_raw STDIN.readlines.join
17
+ end
18
+ # Put together a URL
19
+ if ARGV.include?('--raw')
20
+ url = "#{@uploader.server_url}/raw/#{key}"
21
+ else
22
+ url = "#{@uploader.server_url}/#{key}"
23
+ end
24
+ # And write data out
25
+ if STDOUT.tty?
26
+ STDOUT.puts url
27
+ else
28
+ STDOUT.print url
29
+ end
30
+ rescue Exception => e
31
+ abort e.message
32
+ end
33
+
34
+ end
35
+
36
+ end
@@ -0,0 +1,4 @@
1
+ module Haste
2
+ class Exception < StandardError
3
+ end
4
+ end
@@ -0,0 +1,59 @@
1
+ require 'json'
2
+ require 'faraday'
3
+
4
+ module Haste
5
+
6
+ DEFAULT_URL = 'http://hastebin.com'
7
+
8
+ class Uploader
9
+
10
+ attr_reader :server_url
11
+
12
+ def initialize(server_url = nil)
13
+ @server_url = server_url || Haste::DEFAULT_URL
14
+ @server_url = @server_url.dup
15
+ @server_url = @server_url.chop if @server_url.end_with?('/')
16
+ end
17
+
18
+ # Take in a path and return a key
19
+ def upload_path(path)
20
+ fail_with 'No input file given' unless path
21
+ fail_with "#{path}: No such path" unless File.exists?(path)
22
+ upload_raw open(path).read
23
+ end
24
+
25
+ # Take in data and return a key
26
+ def upload_raw(data)
27
+ data.rstrip!
28
+ response = do_post data
29
+ if response.status == 200
30
+ data = JSON.parse(response.body)
31
+ data['key']
32
+ else
33
+ fail_with "failure uploading: #{response.body}"
34
+ end
35
+ rescue JSON::ParserError => e
36
+ fail_with "failure parsing response: #{e.message}"
37
+ rescue Errno::ECONNREFUSED => e
38
+ fail_with "failure connecting: #{e.message}"
39
+ end
40
+
41
+ private
42
+
43
+ def do_post(data)
44
+ connection.post('/documents', data)
45
+ end
46
+
47
+ def connection
48
+ @connection ||= Faraday.new(:url => server_url) do |c|
49
+ c.adapter Faraday.default_adapter
50
+ end
51
+ end
52
+
53
+ def fail_with(msg)
54
+ raise Exception.new(msg)
55
+ end
56
+
57
+ end
58
+
59
+ end
@@ -1,5 +1,5 @@
1
1
  module Haste
2
2
 
3
- VERSION = '0.1.7'
3
+ VERSION = '0.2.0'
4
4
 
5
5
  end
@@ -0,0 +1,2 @@
1
+ require 'bundler/setup'
2
+ require File.dirname(__FILE__) + '/../lib/haste'
metadata CHANGED
@@ -1,41 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: haste
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Crepezzi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-08 00:00:00.000000000 Z
11
+ date: 2014-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: json
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
39
53
  - !ruby/object:Gem::Version
40
54
  version: '0'
41
55
  description: CLI Haste Client
@@ -45,9 +59,13 @@ executables:
45
59
  extensions: []
46
60
  extra_rdoc_files: []
47
61
  files:
48
- - lib/haste/version.rb
49
- - lib/haste.rb
50
62
  - bin/haste
63
+ - lib/haste.rb
64
+ - lib/haste/cli.rb
65
+ - lib/haste/exception.rb
66
+ - lib/haste/uploader.rb
67
+ - lib/haste/version.rb
68
+ - spec/spec_helper.rb
51
69
  homepage:
52
70
  licenses: []
53
71
  metadata: {}
@@ -57,18 +75,19 @@ require_paths:
57
75
  - lib
58
76
  required_ruby_version: !ruby/object:Gem::Requirement
59
77
  requirements:
60
- - - '>='
78
+ - - ">="
61
79
  - !ruby/object:Gem::Version
62
80
  version: '0'
63
81
  required_rubygems_version: !ruby/object:Gem::Requirement
64
82
  requirements:
65
- - - '>='
83
+ - - ">="
66
84
  - !ruby/object:Gem::Version
67
85
  version: '0'
68
86
  requirements: []
69
87
  rubyforge_project:
70
- rubygems_version: 2.0.6
88
+ rubygems_version: 2.2.0
71
89
  signing_key:
72
90
  specification_version: 4
73
91
  summary: Haste Client
74
- test_files: []
92
+ test_files:
93
+ - spec/spec_helper.rb