leif 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9d21327777d65dd1e53077d2ee7f800dac2c93a6
4
- data.tar.gz: ed6d1bf362b4eaa69bc2ddcc416f9164e7d5022d
3
+ metadata.gz: 803bf425951a72e2a0a26354c63479fb7a18da26
4
+ data.tar.gz: 0f024f19bb334ef1a8c21e5211e9407af9082ce1
5
5
  SHA512:
6
- metadata.gz: 2f8e3aa4056db2da381cb4cad711ed3cc63868457b0cfc3fec1bdc93b98f730632d1a305d1d2868bdb21a769486e9d35c9ec9e1f5b12cfbe485283ec2e6393fc
7
- data.tar.gz: 04cb8df37f9aa1cf0410ad4695fde9b601cbb841a4e7574cc639338eab634b2849fac3df64bf616fa5002a67030f3857dee7fcdb2e9ead4f5a4d1a66df3159cd
6
+ metadata.gz: 81c1c298807283bc86d597974cb3a8f5581182683a7686d772b552d6dd517b37996670a31896b30f13294fb0354f66a727e00b1149a58fe98891d44871ea4c26
7
+ data.tar.gz: 125bb032ca4e458fd6cc907a34d2312271d32b5869a4a7f36539b5d94a2f1700ae72e4ffa3eaa05f1ff1f93d59284e9fc653525ec817061cc93a9b60262ad580
data/README.md CHANGED
@@ -67,9 +67,6 @@ $ gem man leif
67
67
  - `token` <u>token</u>:
68
68
  Authenticate using the given token and reload the current resource.
69
69
 
70
- - `debug`:
71
- Print debug output from the previous HTTP request and response.
72
-
73
70
  - `help`:
74
71
  Print available commands.
75
72
 
data/bin/leif CHANGED
@@ -3,7 +3,8 @@ require 'leif/cli'
3
3
  require 'leif/collection_json'
4
4
  require 'leif/section'
5
5
 
6
- Leif::Cli.new.tap do |cli|
6
+ insecure = !!ARGV.delete('--insecure')
7
+ Leif::Cli.new(ssl_verify: !insecure).tap do |cli|
7
8
  cli.get_root
8
9
  loop do
9
10
  cli.print_overview
data/lib/leif/cli.rb CHANGED
@@ -1,55 +1,51 @@
1
- require 'faraday'
2
- require 'faraday_middleware'
3
1
  require 'highline/import'
4
2
  require 'json'
5
- require 'logger'
6
- require 'stringio'
3
+ require 'leif/connection'
7
4
 
8
5
  module Leif
9
6
  class Cli
10
- def conn
11
- @conn ||= Faraday.new(url: 'https://api.getcloudapp.com') do |config|
12
- config.request :url_encoded
13
- config.response :logger, logger
14
- config.response :json, :content_type => /\bjson$/
15
- config.adapter Faraday.default_adapter
16
- end
7
+ ENDPOINT = 'https://api.getcloudapp.com'
8
+ attr_reader :last_exchange, :connection
9
+
10
+ def initialize(connection_options = {})
11
+ @connection_options = connection_options
12
+ @connection = Connection.to_url(ENDPOINT, connection_options)
17
13
  end
18
14
 
19
- def logger
20
- @logger ||= Logger.new(debug_output)
15
+ def basic_auth(username, password)
16
+ @connection = Connection.to_url(ENDPOINT,
17
+ connection_options(username: username,
18
+ password: password))
21
19
  end
22
20
 
23
- def debug_output
24
- @debug_output ||= StringIO.new
21
+ def token_auth(token)
22
+ @connection = Connection.to_url(ENDPOINT,
23
+ connection_options(token: token))
25
24
  end
26
25
 
27
- def banner(banner, &message)
28
- puts
29
- Section.banner(banner, &message)
26
+ def connection_options(options = {})
27
+ @connection_options.merge(options)
30
28
  end
31
29
 
32
- def reset_debug_output
33
- debug_output.rewind
34
- debug_output.truncate 0
30
+ def request(*args)
31
+ @last_exchange = connection.request(*args)
35
32
  end
36
33
 
37
- def make_request(uri, data = {}, method = :unset)
38
- method = data.empty? ? :get : :post if method == :unset
39
- reset_debug_output
40
- @response = conn.send(method, uri, data)
34
+ def banner(banner, &message)
35
+ puts
36
+ Section.banner(banner, &message)
41
37
  end
42
38
 
43
39
  def collection
44
- Leif::CollectionJson::Collection.new(@response.body)
40
+ Leif::CollectionJson::Collection.new(last_exchange.response_body)
45
41
  end
46
42
 
47
43
  def get_root
48
- make_request '/'
44
+ request '/'
49
45
  end
50
46
 
51
- def retry_request
52
- make_request @response.env[:url].request_uri
47
+ def reload
48
+ request last_exchange.uri
53
49
  end
54
50
 
55
51
  def print_overview
@@ -59,21 +55,21 @@ module Leif
59
55
  print_links collection
60
56
  end
61
57
 
62
- def request_basic_authentication(username = :ask, password = :ask)
58
+ def set_basic_auth(username = :ask, password = :ask)
63
59
  username = ask('Username: ') if username == :ask
64
60
  password = ask('Password: ') {|q| q.echo = '*' } if password == :ask
65
- conn.basic_auth username, password
66
- retry_request
61
+ basic_auth username, password
62
+ reload
67
63
  end
68
64
 
69
- def request_token_authentication(token = '2x033S09401z300E')
70
- conn.headers['Authorization'] = "Token token=#{token.inspect}"
71
- retry_request
65
+ def set_token_auth(token = '2x033S09401z300E')
66
+ token_auth token
67
+ reload
72
68
  end
73
69
 
74
70
  def follow_link(subject, relation = :ask)
75
71
  relation = ask('Relation: ') if relation == :ask
76
- make_request subject.link_href(relation)
72
+ request subject.link_href(relation)
77
73
  end
78
74
 
79
75
  def fill_and_submit_template(template, label)
@@ -88,7 +84,7 @@ module Leif
88
84
  template = template.fill_field name, new_value
89
85
  end
90
86
 
91
- make_request template.href, template.convert_to_json, template.method
87
+ request template.href, template.convert_to_json, template.method
92
88
  end
93
89
 
94
90
  def create_item
@@ -103,8 +99,8 @@ module Leif
103
99
 
104
100
  def print_request
105
101
  banner 'Request' do |out|
106
- out.print "#{@response.env[:method].upcase} #{@response.env[:url]}"
107
- out.print @response.env[:request_headers].map {|header, value|
102
+ out.print "#{last_exchange.method} #{last_exchange.uri}"
103
+ out.print last_exchange.request_headers.map {|header, value|
108
104
  "#{header}: #{value}"
109
105
  }
110
106
  end
@@ -112,7 +108,7 @@ module Leif
112
108
 
113
109
  def print_response
114
110
  banner 'Response' do |out|
115
- out.print @response.headers.map {|header, value|
111
+ out.print last_exchange.response_headers.map {|header, value|
116
112
  "#{header}: #{value}"
117
113
  }
118
114
  end
@@ -120,7 +116,7 @@ module Leif
120
116
 
121
117
  def print_body
122
118
  banner 'Body' do |out|
123
- out.print JSON.pretty_generate(@response.body).lines
119
+ out.print JSON.pretty_generate(last_exchange.response_body).lines
124
120
  end
125
121
  end
126
122
 
@@ -171,13 +167,6 @@ module Leif
171
167
  end
172
168
  end
173
169
 
174
- def print_debug
175
- banner 'Debug' do |out|
176
- debug_output.rewind
177
- out.print debug_output.readlines
178
- end
179
- end
180
-
181
170
  def print_help
182
171
  banner 'Help' do |out|
183
172
  out.print <<EOS.lines
@@ -210,9 +199,6 @@ basic [<username> [<password>]]:
210
199
  token <token>:
211
200
  Authenticate using the given token and reload the current resource.
212
201
 
213
- debug:
214
- Print debug output from the previous HTTP request and response.
215
-
216
202
  quit:
217
203
  Exit leif.
218
204
  EOS
@@ -258,7 +244,8 @@ EOS
258
244
  def get_next_action
259
245
  command, args = ask_for_action
260
246
  case command
261
- when 'r', 'root' then get_root
247
+ when 'root' then get_root
248
+ when 'reload' then reload
262
249
  when 'f', 'follow' then follow_link(collection, *args)
263
250
  when 'create' then create_item
264
251
 
@@ -269,10 +256,9 @@ EOS
269
256
  when 'template' then print_template; get_next_action
270
257
  when 'items' then print_items
271
258
 
272
- when 'b', 'basic' then request_basic_authentication(*args)
273
- when 't', 'token' then request_token_authentication(*args)
259
+ when 'b', 'basic' then set_basic_auth(*args)
260
+ when 't', 'token' then set_token_auth(*args)
274
261
 
275
- when 'd', 'debug' then print_debug; get_next_action
276
262
  when '?', 'help' then print_help; get_next_action
277
263
  when 'q', 'quit' then exit
278
264
  else puts 'Try again.'; get_next_action
@@ -0,0 +1,69 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+
4
+ module Leif
5
+ class Connection
6
+ attr_reader :connection
7
+
8
+ def initialize(connection)
9
+ @connection = connection
10
+ end
11
+
12
+ def self.to_url(url, options = {})
13
+ connection = default_connection(url, options)
14
+ if options.has_key?(:username) and options.has_key?(:password)
15
+ connection.basic_auth options.fetch(:username),
16
+ options.fetch(:password)
17
+ elsif options.has_key?(:token)
18
+ connection.token_auth options.fetch(:token)
19
+ end
20
+ new(connection)
21
+ end
22
+
23
+ def self.default_connection(url, options = {})
24
+ ssl_verify = options.fetch(:ssl_verify, true)
25
+ Faraday.new(url: url, ssl: { verify: ssl_verify }) do |config|
26
+ config.request :url_encoded
27
+ config.response :json, :content_type => /\bjson$/
28
+ config.adapter Faraday.default_adapter
29
+ end
30
+ end
31
+
32
+ def request(path, data = {}, method = :unset)
33
+ method = data.empty? ? :get : :post if method == :unset
34
+ Exchange.new(connection.send(method, path, data))
35
+ end
36
+
37
+ class Exchange
38
+ attr_reader :exchange
39
+
40
+ def initialize(exchange)
41
+ @exchange = exchange
42
+ end
43
+
44
+ def method
45
+ exchange.env[:method].upcase
46
+ end
47
+
48
+ def uri
49
+ exchange.env[:url].request_uri
50
+ end
51
+
52
+ def request_headers
53
+ exchange.env[:request_headers]
54
+ end
55
+
56
+ def request_body
57
+ exchange.env[:body]
58
+ end
59
+
60
+ def response_headers
61
+ exchange.headers
62
+ end
63
+
64
+ def response_body
65
+ exchange.body
66
+ end
67
+ end
68
+ end
69
+ end
data/lib/leif/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Leif
2
- VERSION = '0.0.8'
2
+ VERSION = '0.0.9'
3
3
  end
data/man/leif.1 CHANGED
@@ -19,6 +19,10 @@
19
19
  Go back to the root\.
20
20
  .
21
21
  .TP
22
+ \fBreload\fR
23
+ Make a GET request to the last request URI\.
24
+ .
25
+ .TP
22
26
  \fBfollow\fR \fIrel\fR
23
27
  Follow link with the relation \fIrel\fR on the collection or selected item\.
24
28
  .
@@ -67,10 +71,6 @@ Authenticate with HTTP Basic and reload the current resource\. Will be prompted
67
71
  Authenticate using the given token and reload the current resource\.
68
72
  .
69
73
  .TP
70
- \fBdebug\fR
71
- Print debug output from the previous HTTP request and response\.
72
- .
73
- .TP
74
74
  \fBhelp\fR
75
75
  Print available commands\.
76
76
  .
data/man/leif.1.html CHANGED
@@ -91,6 +91,7 @@ programs.</p>
91
91
 
92
92
  <dl>
93
93
  <dt class="flush"><code>root</code></dt><dd><p>Go back to the root.</p></dd>
94
+ <dt class="flush"><code>reload</code></dt><dd><p>Make a GET request to the last request URI.</p></dd>
94
95
  <dt><code>follow</code> <var>rel</var></dt><dd><p>Follow link with the relation <var>rel</var> on the collection or selected item.</p></dd>
95
96
  <dt class="flush"><code>create</code></dt><dd><p>Begin editing the template to create a new item.</p></dd>
96
97
  <dt class="flush"><code>update</code></dt><dd><p>Begin editing the template to update the item selected with <code>items</code>.</p></dd>
@@ -105,7 +106,6 @@ or <code>follow</code> an item's link.</p></dd>
105
106
  <dt><code>basic</code> [<var>username</var> [<var>password</var>]]</dt><dd><p>Authenticate with HTTP Basic and reload the current resource. Will be
106
107
  prompted for username and password if omitted.</p></dd>
107
108
  <dt><code>token</code> <var>token</var></dt><dd><p>Authenticate using the given token and reload the current resource.</p></dd>
108
- <dt class="flush"><code>debug</code></dt><dd><p>Print debug output from the previous HTTP request and response.</p></dd>
109
109
  <dt class="flush"><code>help</code></dt><dd><p>Print available commands.</p></dd>
110
110
  <dt class="flush"><code>quit</code></dt><dd><p>Exit <code>leif</code>.</p></dd>
111
111
  </dl>
data/man/leif.1.ronn CHANGED
@@ -15,6 +15,9 @@ programs.
15
15
  - `root`:
16
16
  Go back to the root.
17
17
 
18
+ - `reload`:
19
+ Make a GET request to the last request URI.
20
+
18
21
  - `follow` <rel>:
19
22
  Follow link with the relation <rel> on the collection or selected item.
20
23
 
@@ -53,9 +56,6 @@ programs.
53
56
  - `token` <token>:
54
57
  Authenticate using the given token and reload the current resource.
55
58
 
56
- - `debug`:
57
- Print debug output from the previous HTTP request and response.
58
-
59
59
  - `help`:
60
60
  Print available commands.
61
61
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: leif
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Larry Marburger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-12 00:00:00.000000000 Z
11
+ date: 2013-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -93,6 +93,7 @@ files:
93
93
  - bin/leif
94
94
  - lib/leif/cli.rb
95
95
  - lib/leif/collection_json.rb
96
+ - lib/leif/connection.rb
96
97
  - lib/leif/section.rb
97
98
  - lib/leif/version.rb
98
99
  - man/leif.1