faraday-cli 0.1.0 → 0.2.0

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: f05a956dccfc757f41470e2d83a9073c992b1959
4
- data.tar.gz: b9e8b968189f9034bf5ce6d3511c684f3c06a9b6
3
+ metadata.gz: fd56916e841f94acb051ef3bba61c781365b4a33
4
+ data.tar.gz: 5e082dcee9e28df98d2f42d5a56b1c1c73424875
5
5
  SHA512:
6
- metadata.gz: 3205560b172da216bc751eccd3e9c92416cedede685212d949357c2dc3d3482857bcf873aef60a196ded4e42b628ec857c381a7c193019ad2ea020ebf30b0490
7
- data.tar.gz: f9b823c5abf8358688beeb0a41d58847055dd32047fee9b5c97c2b75a50d14841f655138da1984b594e5670d480f9a6d9edd13096ae194923a211b4f18f86f11
6
+ metadata.gz: a3829febeef5abbaa2b29f870e8a936c643a6ecea4c07621b076ca0ddcb8379af78f20aff2d78b66b18d4ce9b2363cfa25564fa3e595b67f4b9c5d302bcdd02a
7
+ data.tar.gz: 3647c38fe784742544750f762687538c803ecca19801ec05900602cbedbe2e28fe9794732a5d0ebcebf0cb9bbff33a6608a84556783d512d2bb50d3ec72aeeac
data/Gemfile CHANGED
@@ -1,4 +1,9 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in faraday-cli.gemspec
4
3
  gemspec
4
+
5
+ gem 'rack'
6
+ gem 'escher'
7
+ gem 'escher-keypool'
8
+ gem 'escher-rack_middleware'
9
+ gem 'faraday_middleware-escher'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -1,14 +1,13 @@
1
1
  #!/usr/bin/env ruby
2
-
3
- if ENV['DEVELOPER_ENV'] == 'true'
4
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
- end
2
+ # if ENV['DEVELOPER_ENV'] == 'true'
3
+ # $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
+ # end
6
5
 
7
6
  require 'optparse'
8
7
  require 'faraday/cli'
9
8
 
10
-
11
9
  CLI_OPTIONS = {}
10
+ CLI_OPTIONS[:flags]= []
12
11
  OptionParser.new do |o|
13
12
 
14
13
  o.banner.concat(' <url>')
@@ -26,21 +25,38 @@ OptionParser.new do |o|
26
25
  end
27
26
 
28
27
  CLI_OPTIONS[:params]= []
29
- o.on('-q','--query key=value','Pass Query key values to use in the request') do |raw_query_pair|
28
+ o.on('-q', '--query key=value', 'Pass Query key values to use in the request') do |raw_query_pair|
30
29
  CLI_OPTIONS[:params].push(raw_query_pair.split('='))
31
30
  end
32
31
 
33
- # o.on('-d', '--data PAYLOAD_STRING', 'HTTP POST data (H)') { |payload| CLI_OPTIONS[:payload]= payload }
34
- # o.on('--data_file FILE_PATH', 'Read file content for use as payload') do |payload_file|
35
- # CLI_OPTIONS[:payload]= File.read(payload_file)
36
- # end
37
- #
38
- # o.on('-A', '--user-agent STRING', 'Send User-Agent STRING to server (H)') do |user_agent|
39
- # CLI_OPTIONS[:user_agent]= user_agent
40
- # end
41
- #
42
- # o.on('-o', '--output FILE_PATH', 'Write to FILE instead of stdout') { |out_file_path| CLI_OPTIONS[:out_file]= out_file_path }
43
- #
32
+ o.on('-d', '--data PAYLOAD_STRING', 'HTTP POST data (H)') { |payload| CLI_OPTIONS[:body]= payload }
33
+
34
+ o.on('--upload_file KEY=FILE_PATH[:CONTENT_TYPE]', 'Pass File upload io in the request pointing to the given file') do |payload_file_path|
35
+
36
+ CLI_OPTIONS[:flags] << :multipart
37
+
38
+ parts = payload_file_path.split('=')
39
+ raw_file_path = parts.pop
40
+ key = parts.first
41
+
42
+ file_path, content_type = raw_file_path.split(':')
43
+ content_type ||= 'application/octet-stream'
44
+
45
+ file_stream_io = Faraday::UploadIO.new(File.realpath(file_path), content_type)
46
+
47
+ CLI_OPTIONS[:body] = {key => file_stream_io}
48
+
49
+ end
50
+
51
+ o.on('-A', '--user-agent STRING', 'Send User-Agent STRING to server (H)') do |user_agent|
52
+ CLI_OPTIONS[:http_headers] << ['User-Agent', user_agent]
53
+ end
54
+
55
+ o.on('-o', '--output FILE_PATH', 'Write to FILE instead of stdout') do |out_file_path|
56
+ $stdout.reopen(out_file_path, 'a+')
57
+ $stderr.reopen(out_file_path, 'a+')
58
+ end
59
+
44
60
  # o.on('-x', '--proxy HOST:PORT', 'HOST[:PORT] Use proxy on given port') do |host_port_str|
45
61
  # host, port = host_port_str.split(':')
46
62
  # port = '80' if port.nil?
@@ -48,31 +64,46 @@ OptionParser.new do |o|
48
64
  # CLI_OPTIONS[:proxy]= {host: host, port: port}
49
65
  # end
50
66
 
67
+ o.on('-s', '--silent', "Silent mode (don't output anything)") { CLI_OPTIONS[:flags] << :silent }
68
+
69
+ CLI_OPTIONS[:config_file_paths]= []
70
+ o.on('-c', '--config FILE_PATH', 'File path to the .faraday.rb if you want use other than default') do |file_path|
71
+ CLI_OPTIONS[:config_file_paths] << File.realpath(file_path)
72
+ end
73
+
51
74
  o.parse!
52
75
 
53
76
  end
54
77
 
55
- # p CLI_OPTIONS
56
- # p ARGV
57
-
58
78
  ALLOWED_HTTP_METHODS = %w(get head post put patch delete options)
59
79
  raise('invalid http method given') unless ALLOWED_HTTP_METHODS.include?(CLI_OPTIONS[:http_method])
60
- conn = Faraday.new
61
80
 
62
- Faraday::CLI::MiddlewareFetcher.extend!(conn)
81
+ connection = Faraday.new do |builder|
82
+ Faraday::CLI::MiddlewareFetcher.extend!(builder, *CLI_OPTIONS[:config_file_paths])
83
+
84
+ builder.request(:multipart) if CLI_OPTIONS[:flags].include?(:multipart)
85
+ builder.response :logger unless CLI_OPTIONS[:flags].include?(:silent)
86
+
87
+ builder.adapter(:net_http)
88
+
89
+ end
90
+
91
+ response = connection.public_send(CLI_OPTIONS[:http_method].downcase) do |request|
63
92
 
64
- response = conn.public_send(CLI_OPTIONS[:http_method]) do |request|
65
93
  request.url(ARGV[0])
66
94
 
67
95
  CLI_OPTIONS[:http_headers].each do |key, value|
68
96
  request.headers[key]=value
69
97
  end
70
98
 
71
- CLI_OPTIONS[:params].each do |key,value|
99
+ CLI_OPTIONS[:params].each do |key, value|
72
100
  request.params[key]=value
73
101
  end
74
102
 
103
+ request.body = CLI_OPTIONS[:body] unless CLI_OPTIONS[:body].nil?
104
+
75
105
  end
76
106
 
77
107
 
78
- $stdout.puts Faraday::CLI::ResponseFormatter.format(response)
108
+ $stdout.puts Faraday::CLI::ResponseFormatter.format(response) unless CLI_OPTIONS[:flags].include?(:silent)
109
+ exit(1) unless (200..299).include?(response.status)
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
11
11
 
12
12
  spec.summary = %q{Console line interface for faraday gem.}
13
13
  spec.description = %q{Console line interface for faraday gem client so you can use your favorite middleware based ruby http client on the terminal!}
14
- spec.homepage = "https://github.com/adamluzsi/faraday-cli.rb"
14
+ spec.homepage = 'https://github.com/adamluzsi/faraday-cli.rb'
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
17
  spec.bindir = 'bin'
@@ -4,12 +4,15 @@ module Faraday::CLI::MiddlewareFetcher
4
4
 
5
5
  require 'faraday/cli/middleware_fetcher/container'
6
6
 
7
- def extend!(faraday_connection)
7
+ def extend!(faraday_connection_builder, *config_file_paths)
8
8
 
9
9
  file_name = '.faraday.rb'
10
- container = Faraday::CLI::MiddlewareFetcher::Container.new(faraday_connection)
10
+ container = Faraday::CLI::MiddlewareFetcher::Container.new(faraday_connection_builder)
11
11
 
12
- case true
12
+ case
13
+
14
+ when !config_file_paths.empty?
15
+ config_file_paths.each { |path| container.merge!(path) }
13
16
 
14
17
  when File.exist?(File.join(Dir.pwd, file_name))
15
18
  container.merge!(File.join(Dir.pwd, file_name))
@@ -2,14 +2,16 @@ require 'forwardable'
2
2
  class Faraday::CLI::MiddlewareFetcher::Container
3
3
 
4
4
  extend Forwardable
5
- def_delegator :@builder, :use
5
+ def_delegators :@builder, :use, :request, :response, :adapter
6
6
 
7
- def initialize(conn)
8
- @builder = conn.builder
7
+ def initialize(builder)
8
+ @builder = builder
9
9
  end
10
10
 
11
11
  def merge!(file_path)
12
- instance_eval(File.read(file_path))
12
+ Dir.chdir(File.dirname(file_path)) do
13
+ instance_eval(File.read(file_path))
14
+ end
13
15
  end
14
16
 
15
17
  end
@@ -0,0 +1,8 @@
1
+ require './credentials'
2
+
3
+ require 'faraday_middleware/escher'
4
+
5
+ use FaradayMiddleware::Escher::RequestSigner,
6
+ credential_scope: CredentialScope,
7
+ options: AuthOptions,
8
+ active_key: -> { Escher::Keypool.new.get_active_key('EscherExample') }
@@ -0,0 +1,29 @@
1
+ require_relative 'credentials'
2
+
3
+ require 'yaml'
4
+ require 'rack'
5
+ require 'escher/rack_middleware'
6
+ Escher::RackMiddleware.config do |c|
7
+ c.add_escher_authenticator { Escher::Auth.new(CredentialScope, AuthOptions) }
8
+ c.add_credential_updater { Escher::Keypool.new.get_key_db }
9
+ end
10
+
11
+ class YourAwesomeApp
12
+
13
+ def call(env)
14
+ puts YAML.dump(env)
15
+ response = Rack::Response.new
16
+ response.write 'OK'
17
+ response.status = 200
18
+
19
+ response.finish
20
+ end
21
+
22
+ end
23
+
24
+ use ECHO
25
+ use Escher::RackMiddleware
26
+ run YourAwesomeApp.new
27
+
28
+ #Rack::Server.start :app => YourAwesomeApp,
29
+ # :Port => ARGV[0].to_i
@@ -0,0 +1,21 @@
1
+ require 'escher-keypool'
2
+
3
+ ENV['KEY_POOL'] = '[{"keyId":"EscherExample","secret":"TheBeginningOfABeautifulFriendship","acceptOnly":0}]'
4
+ ENV['ESCHEREXAMPLE_KEYID'] = 'EscherExample'
5
+
6
+ CredentialScope = 'example/credential/scope'
7
+ AuthOptions = {}
8
+ ESCHER_KEY = {api_key_id: 'EscherExample', api_secret: 'TheBeginningOfABeautifulFriendship'}
9
+
10
+ class ECHO
11
+
12
+ def initialize(app)
13
+ @app = app
14
+ end
15
+
16
+ def call(env)
17
+ puts YAML.dump env
18
+ @app.call(env)
19
+ end
20
+
21
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Luzsi
@@ -123,6 +123,9 @@ files:
123
123
  - lib/faraday/cli/middleware_fetcher/container.rb
124
124
  - lib/faraday/cli/response_formatter.rb
125
125
  - lib/faraday/cli/version.rb
126
+ - prototype/.faraday.rb
127
+ - prototype/config.ru
128
+ - prototype/credentials.rb
126
129
  homepage: https://github.com/adamluzsi/faraday-cli.rb
127
130
  licenses: []
128
131
  metadata: {}