Sha256: 134b9ff0eaf80af6502eeda33da0ad30c601c9040dff8761dbce13d59506cca3

Contents?: true

Size: 1.61 KB

Versions: 3

Compression:

Stored size: 1.61 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, headers = nil)
              res = core.request(path, :#{m}, nil, try_reading_hash(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, body, headers = nil)
              res = core.request(path, :#{m}, try_reading_file(body), try_reading_hash(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.strip}\e[0m"
          end
        end

      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
magellan-cli-0.5.2 lib/magellan/cli/messaging/http.rb
magellan-cli-0.5.1 lib/magellan/cli/messaging/http.rb
magellan-cli-0.5.0 lib/magellan/cli/messaging/http.rb