class PortaText::Command::Base

The base command class.

Author

Marcelo Gornstein (marcelog@portatext.com)

Copyright

Copyright © 2015 PortaText

License

Apache-2.0

Attributes

client[W]

Public Class Methods

new() click to toggle source
# File lib/portatext/command/base.rb, line 57
def initialize
  @args = {}
end

Public Instance Methods

accept_content_type(_method) click to toggle source
# File lib/portatext/command/base.rb, line 44
def accept_content_type(_method)
  return '*/*' unless @args[:accept_any_file].nil?
  return 'text/csv' unless @args[:accept_file].nil?
  return 'audio/mpeg' unless @args[:accept_sound_file].nil?
  'application/json'
end
body(_method) click to toggle source
# File lib/portatext/command/base.rb, line 51
def body(_method)
  return "file:#{@args[:file]}" unless @args[:file].nil?
  return '' if @args.size.eql? 0
  @args.to_json
end
content_type(_method) click to toggle source
# File lib/portatext/command/base.rb, line 38
def content_type(_method)
  return 'text/csv' unless @args[:file].nil?
  return 'audio/mpeg' unless @args[:sound_file].nil?
  'application/json'
end
delete() click to toggle source
# File lib/portatext/command/base.rb, line 29
def delete
  run :delete
end
get() click to toggle source
# File lib/portatext/command/base.rb, line 13
def get
  run :get
end
patch() click to toggle source
# File lib/portatext/command/base.rb, line 25
def patch
  run :patch
end
post() click to toggle source
# File lib/portatext/command/base.rb, line 17
def post
  run :post
end
put() click to toggle source
# File lib/portatext/command/base.rb, line 21
def put
  run :put
end
set(key, value) click to toggle source
# File lib/portatext/command/base.rb, line 33
def set(key, value)
  @args[key] = value
  self
end

Private Instance Methods

run(method) click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/portatext/command/base.rb, line 64
def run(method)
  a_type = accept_content_type method
  command_endpoint = endpoint(method)
  file = @args[:accept_file]
  file ||= @args[:accept_any_file]
  file ||= @args[:accept_sound_file]
  @args.delete :accept_file
  @args.delete :accept_any_file
  @args.delete :accept_sound_file
  @client.run(
    command_endpoint, method, content_type(method),
    a_type, body(method), file
  )
end