Class: Swicky::ApiResult

Inherits:
Object show all
Defined in:
lib/swicky/api_result.rb

Overview

Helper class to encode API result codes for the controller

Constant Summary collapse

RESULTS =
{
  :success => 200,
  :illegal_parameter => 400,
  :error => 500
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result, message) ⇒ ApiResult

Returns a new instance of ApiResult.

Raises:

  • (ArgumentError)


15
16
17
18
19
# File 'lib/swicky/api_result.rb', line 15

def initialize(result, message)
  raise(ArgumentError, "Illegal Result #{result}") unless(RESULTS.keys.include?(result.to_sym))
  @result = result.to_sym
  @message = message
end

Instance Attribute Details

#messageObject

Returns the value of attribute message.



8
9
10
# File 'lib/swicky/api_result.rb', line 8

def message
  @message
end

#resultObject

Returns the value of attribute result.



8
9
10
# File 'lib/swicky/api_result.rb', line 8

def result
  @result
end

Instance Method Details

#http_statusObject



21
22
23
# File 'lib/swicky/api_result.rb', line 21

def http_status
  RESULTS[result]
end

#to_htmlObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/swicky/api_result.rb', line 40

def to_html
  html = ''
  builder = Builder::XmlMarkup.new(:target => html, :indent => 2)
  builder.declare! :DOCTYPE, :html, :PUBLIC, "-//W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
  builder.html do
    builder.head { builder.title("Swicky request result") }
    builder.body do
      builder.h1("Result Code")
      builder.p(result.to_s)
      builder.h1("Message")
      builder.p(message)
    end
  end
  html
end

#to_jsonObject



36
37
38
# File 'lib/swicky/api_result.rb', line 36

def to_json
  { :result => result, :message => message }.to_json
end

#to_xmlObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/swicky/api_result.rb', line 25

def to_xml
  xml = ''
  builder = Builder::XmlMarkup.new(:target => xml, :indent => 2)
  builder.instruct!
  builder.swicky_api do
    builder.result(result.to_s)
    builder.message(message)
  end
  xml
end