Class: Quandl::Command::Presenter::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/quandl/command/presenter/record.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Record

Returns a new instance of Record.



9
10
11
12
# File 'lib/quandl/command/presenter/record.rb', line 9

def initialize(*args)
  self.options = args.extract_options!
  self.object = args.first
end

Instance Attribute Details

#objectObject

Returns the value of attribute object.



7
8
9
# File 'lib/quandl/command/presenter/record.rb', line 7

def object
  @object
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/quandl/command/presenter/record.rb', line 7

def options
  @options
end

Instance Method Details

#as_jsonObject



58
59
60
61
62
63
64
# File 'lib/quandl/command/presenter/record.rb', line 58

def as_json
  json = { status: status, id: id, detail: detail, message: message }
  json[:attributes] = object.attributes if object.respond_to?(:attributes)
  json[:metadata] = object. if object.respond_to?(:metadata)
  json[:errors] = object.error_messages if object.respond_to?(:error_messages)
  json
end

#as_pipesObject



54
55
56
# File 'lib/quandl/command/presenter/record.rb', line 54

def as_pipes
  [ status, id, detail, message ]
end

#as_stderrObject



32
33
34
35
36
37
38
39
40
# File 'lib/quandl/command/presenter/record.rb', line 32

def as_stderr
  output = ''
  output += "#{id}\n" if id.present?
  output += "#{detail}\n" if detail.present?
  output += errors if errors.present?
  output += object.to_s if object.kind_of?(Exception)
  output += object.backtrace.join("\n") if object.respond_to?(:backtrace) && options[:trace] == true
  output += "\n---\n"
end

#detailObject



88
89
# File 'lib/quandl/command/presenter/record.rb', line 88

def detail
end

#doc?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/quandl/command/presenter/record.rb', line 105

def doc?
  options[:type] == :doc
end

#docs?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/quandl/command/presenter/record.rb', line 101

def docs?
  options[:type] == :docs
end

#errorsObject



95
96
97
98
99
# File 'lib/quandl/command/presenter/record.rb', line 95

def errors
  messages = object.try(:error_messages) || []
  # extract response errors
  Presenter.pretty_errors(messages)
end

#errors_without_spacesObject



91
92
93
# File 'lib/quandl/command/presenter/record.rb', line 91

def errors_without_spaces
  errors.to_s.gsub("\n", ' ')
end

#idObject



80
81
82
# File 'lib/quandl/command/presenter/record.rb', line 80

def id
  object.try(:id)
end

#messageObject



84
85
86
# File 'lib/quandl/command/presenter/record.rb', line 84

def message
  errors_without_spaces
end

#statusObject



74
75
76
77
78
# File 'lib/quandl/command/presenter/record.rb', line 74

def status
  return Quandl::Client::HTTP_STATUS_CODES[422] if object.try(:errors).try(:present?)
  return object.try(:human_status) if object.respond_to?(:human_status) && object.human_status.present?
  Quandl::Client::HTTP_STATUS_CODES[404]
end

#to_csvObject



46
47
48
# File 'lib/quandl/command/presenter/record.rb', line 46

def to_csv
  object.try(:to_csv)
end

#to_format(f) ⇒ Object

supported formats



16
17
18
# File 'lib/quandl/command/presenter/record.rb', line 16

def to_format(f)
  self.send("to_#{f}") if self.respond_to?("to_#{f}")
end

#to_jsonObject



50
51
52
# File 'lib/quandl/command/presenter/record.rb', line 50

def to_json
  as_json.try(:to_json)
end

#to_pipesObject



21
22
23
24
25
26
# File 'lib/quandl/command/presenter/record.rb', line 21

def to_pipes
  column = options[:output_column] if options.present? && options.is_a?(Hash)
  pipes = as_pipes.dup.compact
  pipes = [ pipes[ column.to_i ] ] if column.present? && column.to_s.numeric?
  pipes.join(" | ")
end

#to_qdfObject



42
43
44
# File 'lib/quandl/command/presenter/record.rb', line 42

def to_qdf
  object.try(:to_qdf)
end

#to_stderrObject



28
29
30
# File 'lib/quandl/command/presenter/record.rb', line 28

def to_stderr
  as_stderr
end

#valid?Boolean

default attribute mappings

Returns:

  • (Boolean)


68
69
70
71
72
# File 'lib/quandl/command/presenter/record.rb', line 68

def valid?
  return true if object.nil?
  return true if [200,201,404].include?(object.try(:status))
  false
end