Class: Bixby::JsonResponse

Inherits:
Object
  • Object
show all
Includes:
Jsonify
Defined in:
lib/bixby_common/api/json_response.rb

Overview

Wraps a JSON Response

Constant Summary collapse

SUCCESS =
"success"
FAIL =
"fail"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Jsonify

included, #to_json

Methods included from Hashify

#to_hash

Constructor Details

#initialize(status = nil, message = nil, data = nil, code = nil) ⇒ JsonResponse

Create a new JsonResponse

Parameters:

  • status (String) (defaults to: nil)

    Status of operaiton (“success” or “fail”)

  • message (String) (defaults to: nil)

    Response message

  • data (Hash) (defaults to: nil)

    Response data as key/value pairs

  • code (FixNum) (defaults to: nil)

    Response code



25
26
27
28
29
30
# File 'lib/bixby_common/api/json_response.rb', line 25

def initialize(status = nil, message = nil, data = nil, code = nil)
  @status = status
  @message = message
  @data = data
  @code = code
end

Instance Attribute Details

#codeFixNum

Response code

Returns:

  • (FixNum)

    the current value of code



10
11
12
# File 'lib/bixby_common/api/json_response.rb', line 10

def code
  @code
end

#dataHash

Response data as key/value pairs

Returns:

  • (Hash)

    the current value of data



10
11
12
# File 'lib/bixby_common/api/json_response.rb', line 10

def data
  @data
end

#messageString

Response message

Returns:

  • (String)

    the current value of message



10
11
12
# File 'lib/bixby_common/api/json_response.rb', line 10

def message
  @message
end

#statusString

Status of operaiton (“success” or “fail”)

Returns:

  • (String)

    the current value of status



10
11
12
# File 'lib/bixby_common/api/json_response.rb', line 10

def status
  @status
end

Class Method Details

.bundle_not_found(bundle) ⇒ Object

Create a JsonResponse indicating “bundle not found”

Parameters:

  • bundle (String)

    Name of bundle



56
57
58
# File 'lib/bixby_common/api/json_response.rb', line 56

def self.bundle_not_found(bundle)
  new("fail", "bundle not found: #{bundle}", nil, 404)
end

.command_not_found(command) ⇒ Object

Create a JsonResponse indicating “command not found”

Parameters:

  • command (String)

    Name of command



63
64
65
# File 'lib/bixby_common/api/json_response.rb', line 63

def self.command_not_found(command)
  new("fail", "command not found: #{command}", nil, 404)
end

.invalid_request(msg = nil) ⇒ Object

Create a JsonResponse representing an invalid request

Parameters:

  • msg (String) (defaults to: nil)

    Optional message (default: “invalid request”)



49
50
51
# File 'lib/bixby_common/api/json_response.rb', line 49

def self.invalid_request(msg = nil)
  new("fail", (msg || "invalid request"), nil, 400)
end

Instance Method Details

#fail?Boolean

Was operation unsuccessful?

Returns:

  • (Boolean)

    True if @status != “success”



42
43
44
# File 'lib/bixby_common/api/json_response.rb', line 42

def fail?
  @status && @status == FAIL
end

#success?Boolean

Was operation successful?

Returns:

  • (Boolean)

    True if @status == “success”



35
36
37
# File 'lib/bixby_common/api/json_response.rb', line 35

def success?
  @status && @status == SUCCESS
end