Sha256: 8b14a1d914275ed2c40738e340a49f3eb32fc7b8d757c8a5e35f1f3b62de668e

Contents?: true

Size: 1.6 KB

Versions: 33

Compression:

Stored size: 1.6 KB

Contents

require "magellan/cli/messaging"

module Magellan
  module Cli
    module Messaging
      class Http < Magellan::Cli::Messaging::Base

        desc "ping", I18n.t(:ping, scope: [:messaging, :http])
        def ping
          res = core.ping
          puts_res(res)
        end

        COMMAND_ORDER = %w[get post put patch delete ping]

        [:get, :delete].each do |m|
          desc "#{m} PATH", I18n.t(m, scope: [:messaging, :http])
          method_option :headers, aliases: "-H", desc: I18n.t(:headers, scope: [:messaging, :http, :common_options])
          module_eval(<<-EOM, __FILE__, __LINE__ + 1)
            def #{m}(path)
              res = core.request(path, :#{m}, nil, try_reading_hash(options[:headers]) || {})
              puts_res(res)
            end
          EOM
        end

        [:post, :put, :patch].each do |m|
          desc "#{m} PATH", I18n.t(m, scope: [:messaging, :http])
          method_option :body   , aliases: "-d", desc: I18n.t(:body   , scope: [:messaging, :http, :common_options])
          method_option :headers, aliases: "-H", desc: I18n.t(:headers, scope: [:messaging, :http, :common_options])
          module_eval(<<-EOM, __FILE__, __LINE__ + 1)
            def #{m}(path)
              res = core.request(path, :#{m}, try_reading_file(options[:body]), try_reading_hash(options[:headers]) || {})
              puts_res(res)
            end
          EOM
        end

        no_commands do
          def puts_res(res)
            color = (res.code =~ /\A2\d\d\z/) ? "32" : "31"
            puts "\e[#{color}m#{res.body}\e[0m"
          end
        end

      end
    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
magellan-cli-0.11.1 lib/magellan/cli/messaging/http.rb
magellan-cli-0.11.0 lib/magellan/cli/messaging/http.rb
magellan-cli-0.10.0 lib/magellan/cli/messaging/http.rb
magellan-cli-0.9.1 lib/magellan/cli/messaging/http.rb
magellan-cli-0.9.0 lib/magellan/cli/messaging/http.rb
magellan-cli-0.8.3 lib/magellan/cli/messaging/http.rb
magellan-cli-0.8.2 lib/magellan/cli/messaging/http.rb
magellan-cli-0.8.1 lib/magellan/cli/messaging/http.rb
magellan-cli-0.8.0 lib/magellan/cli/messaging/http.rb
magellan-cli-0.7.11 lib/magellan/cli/messaging/http.rb
magellan-cli-0.7.10 lib/magellan/cli/messaging/http.rb
magellan-cli-0.7.9 lib/magellan/cli/messaging/http.rb
magellan-cli-0.7.8 lib/magellan/cli/messaging/http.rb
magellan-cli-0.7.7 lib/magellan/cli/messaging/http.rb
magellan-cli-0.7.6 lib/magellan/cli/messaging/http.rb
magellan-cli-0.7.5 lib/magellan/cli/messaging/http.rb
magellan-cli-0.7.4 lib/magellan/cli/messaging/http.rb
magellan-cli-0.7.3 lib/magellan/cli/messaging/http.rb
magellan-cli-0.7.2 lib/magellan/cli/messaging/http.rb
magellan-cli-0.7.1 lib/magellan/cli/messaging/http.rb