faraday-cli 0.7.3 → 0.8.3

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: 10f23b45623e436e3fd3f1d259dacb9febe5db0e
4
- data.tar.gz: 84c772819e2ceb4350bf4b9e450a8d8f364415a5
3
+ metadata.gz: 33f57152ba258d34004fe9f2a687aa170b7c8494
4
+ data.tar.gz: f634659c2b2e36e9a9a7de928c67870d57eccec9
5
5
  SHA512:
6
- metadata.gz: 38a73c9a321ac886c0da6e75c9fb40c4178ae7f61f618e47793133e770ecfecdd0f6bce71ac2dc03319e4a673f0a47b32126e4abccc52d66ddb109664e435340
7
- data.tar.gz: 8f4baaac0c925b221b3259224e0a019448e196105f6827983f36d7d17e11737ad25ea40fabd24bac4ef1652444b7fd5f1f4e49ad305cd94795088c51e460447b
6
+ metadata.gz: 1efe3302cf27e69356689154f60bcf0b9b9276feb71f53d237f1ad8fa146c55214a036b65c73a343ef13e109697555de9da7bd4a9e0ea953597b09c20b506aa8
7
+ data.tar.gz: 7bc37644f7c14caac610d4988a32a804a7c4e7167973a840d2fb6769fb2f51fbfb5829805d7c9f46a3b9566ba3beca4f7bf8df57eb418c1b1a230f7e12a8cf84
data/README.md CHANGED
@@ -55,16 +55,19 @@ you can use any faraday middlewares as how you pleased.
55
55
 
56
56
  ```ruby
57
57
 
58
- class MyAwesomeMiddleware
58
+ class MyAwesomeMiddleware < Faraday::Middleware
59
59
 
60
- def initialize(app)
61
- @app = app
62
- end
60
+ def call(request_env)
63
61
 
64
- def call(env)
65
- puts env.object_id
66
- @app.call(env)
67
- end
62
+ # before main request
63
+
64
+ @app.call(request_env).on_complete do |response_env|
65
+
66
+ # after main request
67
+
68
+ end
69
+
70
+ end
68
71
 
69
72
  end
70
73
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.7.3
1
+ 0.8.3
data/exec/faraday-cli ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ lib_folder = File.join(File.dirname(__FILE__), '..', 'lib')
3
+ $LOAD_PATH.unshift(lib_folder) if Dir.exists?(lib_folder)
4
+
5
+ require 'faraday/cli'
6
+ Faraday::CLI::Client.start(ARGV)
data/faraday-cli.gemspec CHANGED
@@ -12,12 +12,11 @@ Gem::Specification.new do |spec|
12
12
  spec.homepage = 'https://github.com/adamluzsi/faraday-cli.rb'
13
13
 
14
14
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
15
- spec.bindir = 'bin'
16
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
- .select{|cli_command| cli_command =~ /^faraday\-cli/ }
18
-
19
15
  spec.require_paths = ['lib']
20
16
 
17
+ spec.bindir = 'exec'
18
+ spec.executables = spec.files.grep(%r{^exec/}) { |f| File.basename(f) }
19
+
21
20
  spec.add_development_dependency 'bundler', '>= 1.10'
22
21
  spec.add_development_dependency 'rake'
23
22
  spec.add_development_dependency 'rspec'
@@ -1,3 +1,79 @@
1
1
  class Faraday::CLI::Client
2
2
 
3
- end
3
+ def self.start(argv)
4
+ new(argv).start
5
+ end
6
+
7
+ def start
8
+
9
+ cli_options = Faraday::CLI::Option::Parser.new.parse!
10
+
11
+ connection = Faraday.new do |builder|
12
+ builder.use Faraday::Response::RaiseError
13
+
14
+ unless cli_options[:flags].include?(:without_middlewares)
15
+ Faraday::CLI::MiddlewareFetcher.extend!(builder, *cli_options[:config_file_paths])
16
+ end
17
+
18
+ builder.request(:multipart) if cli_options[:flags].include?(:multipart)
19
+
20
+ if cli_options[:flags].include?(:verbose)
21
+ builder.response :logger
22
+ end
23
+
24
+ if cli_options[:flags].include?(:super_verbose)
25
+ builder.use Faraday::CLI::Middleware::VerboseRequest
26
+ builder.use Faraday::CLI::Middleware::VerboseResponse
27
+ end
28
+
29
+ builder.adapter(Faraday.default_adapter)
30
+ end
31
+
32
+ if cli_options[:flags].include?(:show_middlewares)
33
+ $stdout.puts(connection.builder.handlers.map(&:inspect))
34
+ exit
35
+ end
36
+
37
+ Faraday::CLI::Option::Validator.validate(@argv,cli_options)
38
+ Faraday::CLI.active_connection = connection.dup
39
+
40
+ begin
41
+
42
+ response = connection.public_send(cli_options[:http_method].downcase) do |request|
43
+
44
+ request.url(@argv[0])
45
+
46
+ cli_options[:http_headers].each do |key, value|
47
+ request.headers[key]=value
48
+ end
49
+
50
+ cli_options[:params].each do |key, value|
51
+ request.params[key]=value
52
+ end
53
+
54
+ request.body = cli_options[:body] unless cli_options[:body].nil?
55
+
56
+ end
57
+
58
+ $stdout.puts(response.body)
59
+
60
+ rescue Faraday::ClientError => ex
61
+ $stdout.puts(ex.message)
62
+ $stdout.puts(ex.response[:body]) unless ex.response.nil?
63
+ exit(1)
64
+
65
+ rescue URI::InvalidURIError => ex
66
+ $stdout.puts(ex.message)
67
+ exit(1)
68
+
69
+ end
70
+
71
+ end
72
+
73
+ protected
74
+
75
+ def initialize(argv)
76
+ @argv = argv
77
+ end
78
+
79
+ end
@@ -0,0 +1,9 @@
1
+ module Faraday::CLI::CoreExt::Object
2
+
3
+ def active_connection
4
+ ::Faraday::CLI.active_connection
5
+ end
6
+
7
+ end
8
+
9
+ Object.__send__(:include,Faraday::CLI::CoreExt::Object)
@@ -0,0 +1,3 @@
1
+ module Faraday::CLI::CoreExt
2
+ require 'faraday/cli/core_ext/object'
3
+ end
@@ -2,6 +2,8 @@ require 'pwd'
2
2
  module Faraday::CLI::MiddlewareFetcher
3
3
  extend self
4
4
 
5
+ NAME_PATH_MATCHER = '{.faraday,.faraday.rb,.faraday-cli}'
6
+
5
7
  require 'faraday/cli/middleware_fetcher/container'
6
8
 
7
9
  def extend!(faraday_connection_builder, *config_file_paths)
@@ -12,9 +14,6 @@ module Faraday::CLI::MiddlewareFetcher
12
14
  protected
13
15
 
14
16
  def get_file_paths(config_file_paths)
15
- file_name = '{.faraday.rb,.faraday}'
16
- folder_name = '{.faraday.rb,.faraday,.faraday-cli}'
17
-
18
17
  case
19
18
 
20
19
  when !config_file_paths.empty?
@@ -26,22 +25,22 @@ module Faraday::CLI::MiddlewareFetcher
26
25
  file_paths.push(*Dir.glob(given_path)); file_paths
27
26
  end
28
27
 
29
- when !(file_paths = Dir.glob(File.join(Dir.pwd, file_name))).empty?
28
+ when !(file_paths = fetch_file_paths(Dir.pwd)).empty?
30
29
  file_paths
31
30
 
32
- when !(file_paths = folder_content(Dir.pwd, folder_name)).empty?
31
+ when !(file_paths = fetch_file_paths_from_folder(Dir.pwd)).empty?
33
32
  file_paths
34
33
 
35
- when !(file_paths = Dir.glob(File.join(PWD.pwd, file_name))).empty?
34
+ when !(file_paths = fetch_file_paths(PWD.pwd)).empty?
36
35
  file_paths
37
36
 
38
- when !(file_paths = folder_content(PWD.pwd, folder_name)).empty?
37
+ when !(file_paths = fetch_file_paths_from_folder(PWD.pwd)).empty?
39
38
  file_paths
40
39
 
41
- when !(file_paths = Dir.glob(File.join(ENV['HOME'], file_name))).empty?
40
+ when !(file_paths = fetch_file_paths(ENV['HOME'])).empty?
42
41
  file_paths
43
42
 
44
- when !(file_paths = folder_content(ENV['HOME'], folder_name)).empty?
43
+ when !(file_paths = fetch_file_paths_from_folder(ENV['HOME'])).empty?
45
44
  file_paths
46
45
 
47
46
  else
@@ -50,11 +49,18 @@ module Faraday::CLI::MiddlewareFetcher
50
49
  end
51
50
  end
52
51
 
52
+ private
53
53
 
54
- protected
54
+ def fetch_file_paths_from_folder(*from_folder)
55
+ select_file_paths(Dir.glob(File.join(*from_folder,NAME_PATH_MATCHER, '*.{rb,ru}')))
56
+ end
57
+
58
+ def fetch_file_paths(*from_folder)
59
+ select_file_paths(Dir.glob(File.join(*from_folder, NAME_PATH_MATCHER)))
60
+ end
55
61
 
56
- def folder_content(main_path, folder_name)
57
- Dir.glob(File.join(main_path, folder_name, '*.rb')).select { |path| not File.directory?(path) }
62
+ def select_file_paths(file_paths)
63
+ file_paths.select { |path| File.exists?(path) and not File.directory?(path) }
58
64
  end
59
65
 
60
66
  end
@@ -15,7 +15,7 @@ class Faraday::CLI::Option::Parser
15
15
  end
16
16
 
17
17
  o.on('-H', '--header HEADER:VALUE', 'Pass custom header LINE to server (H)') do |header|
18
- options[:http_headers].push(header.split(/: */))
18
+ options[:http_headers].push(header.split(/:\s*/))
19
19
  end
20
20
 
21
21
  o.on('-q', '--query key=value', 'Pass Query key values to use in the request') do |raw_query_pair|
@@ -6,23 +6,30 @@ module Faraday::CLI::Option::Validator
6
6
  def validate(argv,options_hash)
7
7
  validate_url(argv[0])
8
8
  validate_http_method(options_hash)
9
+ validate_http_headers(options_hash)
9
10
  end
10
11
 
12
+ def validate_http_headers(options_hash)
13
+ alert('header is in malformed format!') if options_hash[:http_headers].any?{|pairs| pairs.length != 2 }
14
+ end
11
15
 
12
16
  protected
13
17
 
14
18
  def validate_url(url_str)
15
- if url_str.nil? || url_str == ''
16
- $stderr.puts('Missing URL to make request!')
17
- exit(1)
18
- end
19
+ alert('Missing URL to make request!') if url_str.nil? || url_str == ''
19
20
  end
20
21
 
21
22
  def validate_http_method(options_hash)
22
23
  unless ALLOWED_HTTP_METHODS.include?(options_hash[:http_method])
23
- $stderr.puts("invalid http method given: #{options_hash[:http_method].inspect}")
24
- exit(1)
24
+ alert("invalid http method given: #{options_hash[:http_method].inspect}")
25
25
  end
26
26
  end
27
27
 
28
+ private
29
+
30
+ def alert(message)
31
+ $stderr.puts(message)
32
+ exit(1)
33
+ end
34
+
28
35
  end
data/lib/faraday/cli.rb CHANGED
@@ -1,7 +1,12 @@
1
1
  require 'faraday'
2
2
  module Faraday::CLI
3
3
 
4
+ class << self
5
+ attr_accessor :active_connection
6
+ end
7
+
4
8
  require 'faraday/cli/version'
9
+ require 'faraday/cli/core_ext'
5
10
 
6
11
  require 'faraday/cli/client'
7
12
  require 'faraday/cli/option'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.3
4
+ version: 0.8.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Luzsi
8
8
  autorequire:
9
- bindir: bin
9
+ bindir: exec
10
10
  cert_chain: []
11
- date: 2015-09-24 00:00:00.000000000 Z
11
+ date: 2015-10-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -113,11 +113,13 @@ files:
113
113
  - TODO
114
114
  - VERSION
115
115
  - bin/console
116
- - bin/faraday-cli
117
116
  - bin/setup
117
+ - exec/faraday-cli
118
118
  - faraday-cli.gemspec
119
119
  - lib/faraday/cli.rb
120
120
  - lib/faraday/cli/client.rb
121
+ - lib/faraday/cli/core_ext.rb
122
+ - lib/faraday/cli/core_ext/object.rb
121
123
  - lib/faraday/cli/middleware.rb
122
124
  - lib/faraday/cli/middleware/verbose_request.rb
123
125
  - lib/faraday/cli/middleware/verbose_response.rb
@@ -149,7 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
149
151
  version: '0'
150
152
  requirements: []
151
153
  rubyforge_project:
152
- rubygems_version: 2.2.2
154
+ rubygems_version: 2.4.8
153
155
  signing_key:
154
156
  specification_version: 4
155
157
  summary: Console line interface for faraday gem.
data/bin/faraday-cli DELETED
@@ -1,72 +0,0 @@
1
- #!/usr/bin/env ruby
2
- if ENV['FARADAY_CLI_DEVELOPER_ENV'] == 'true'
3
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
- end
5
-
6
- require 'faraday/cli'
7
-
8
- CLI_OPTIONS = Faraday::CLI::Option::Parser.new.parse!
9
- Faraday::CLI::Option::Validator.validate(ARGV,CLI_OPTIONS)
10
-
11
- connection = Faraday.new do |builder|
12
- builder.use Faraday::Response::RaiseError
13
-
14
- unless CLI_OPTIONS[:flags].include?(:without_middlewares)
15
- Faraday::CLI::MiddlewareFetcher.extend!(builder, *CLI_OPTIONS[:config_file_paths])
16
- end
17
-
18
- builder.request(:multipart) if CLI_OPTIONS[:flags].include?(:multipart)
19
-
20
- if CLI_OPTIONS[:flags].include?(:verbose)
21
- builder.response :logger
22
- end
23
-
24
- if CLI_OPTIONS[:flags].include?(:super_verbose)
25
- builder.use Faraday::CLI::Middleware::VerboseRequest
26
- builder.use Faraday::CLI::Middleware::VerboseResponse
27
- end
28
-
29
- builder.adapter(Faraday.default_adapter)
30
- end
31
-
32
- if CLI_OPTIONS[:flags].include?(:show_middlewares)
33
- $stdout.puts(connection.builder.handlers.map(&:inspect))
34
- exit
35
- end
36
-
37
- FARADAY_ACTIVE_CONNECTION= connection
38
-
39
- def active_connection
40
- FARADAY_ACTIVE_CONNECTION
41
- end
42
-
43
- begin
44
-
45
- response = connection.public_send(CLI_OPTIONS[:http_method].downcase) do |request|
46
-
47
- request.url(ARGV[0])
48
-
49
- CLI_OPTIONS[:http_headers].each do |key, value|
50
- request.headers[key]=value
51
- end
52
-
53
- CLI_OPTIONS[:params].each do |key, value|
54
- request.params[key]=value
55
- end
56
-
57
- request.body = CLI_OPTIONS[:body] unless CLI_OPTIONS[:body].nil?
58
-
59
- end
60
-
61
- $stdout.puts(response.body)
62
-
63
- rescue Faraday::ClientError => ex
64
- $stdout.puts(ex.message)
65
- $stdout.puts(ex.response[:body]) unless ex.response.nil?
66
- exit(1)
67
-
68
- rescue URI::InvalidURIError => ex
69
- $stdout.puts(ex.message)
70
- exit(1)
71
-
72
- end