Class: Wit::REST::BodyJson

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/wit_ruby/rest/bodyjson.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(possible_hash = nil) ⇒ BodyJson

Mainly generates instance variable to store values.

Parameters:

  • possible_hash (Hash) (defaults to: nil)

    used for OpenStruct if necessary



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/wit_ruby/rest/bodyjson.rb', line 15

def initialize(possible_hash=nil)
  ## Initialize instance variable for values
  if( !possible_hash.nil? && possible_hash.has_key?("values") )
    @values = possible_hash["values"]
    ## Delete values from it and pass it to OpenStruct constructor
    new_hash = possible_hash.clone
    deleted_value = new_hash.delete("values")
    new_hash_to_os = new_hash
  else
    @values = Array.new
    new_hash_to_os = possible_hash
  end
  super(new_hash_to_os)
end

Instance Attribute Details

#valuesObject

Allows for reading for values instance variable



10
11
12
# File 'lib/wit_ruby/rest/bodyjson.rb', line 10

def values
  @values
end

Instance Method Details

#add_expression(value, *args) ⇒ Wit::REST::BodyJson

Used to add an expression given a value.

Parameters:

  • value (String)

    value that will have a new expression

  • args (Array)

    new expressions for the value

Returns:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/wit_ruby/rest/bodyjson.rb', line 56

def add_expression(value, *args)
  ## Look for it, and insert new expressions if found.
  ## If not found, raise error
  @values.each do |value_hash|
    if value_hash["value"] == value ## Found it and insert.
      ## Set union for arrays, removes duplicates
      value_hash["expressions"] = value_hash["expressions"] | args
    else ## Not found and raise error
      raise NotFound.new("The value, \"#{value}\", cannot be found.")
    end
  end

  return self
end

#add_value(value, *args) ⇒ Wit::REST::BodyJson

TODO:

include metadata

Used to add value for an entity

Parameters:

  • value (String)

    a possible value

  • args (Array)

    posible expressions for the given value

Returns:



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/wit_ruby/rest/bodyjson.rb', line 37

def add_value(value, *args)
  ## Check to see if the value already exists
  @values.each do |value_hash|
    if value_hash["value"] == value
      raise ValueAlreadyExists.new("The current value being inserted, \"#{value}\", already exists.")
    end
  end
  ## Adds it if it isn't there with the given expressions
  @values << {"value" => value,
              "expressions" => args
             }
  return self
end

#jsonString

Used to convert the current hash to JSON

Returns:

  • (String)

    JSON string of the hash



82
83
84
85
86
# File 'lib/wit_ruby/rest/bodyjson.rb', line 82

def json
  ## Use the current to_h method and MultiJson convert it
  MultiJson.dump(self.to_h)

end

#one_expression_to_json(value_add) ⇒ String

Used to properly convert the first expression of the given value to JSON.

Returns:

  • (String)

    JSON string of the expression.



121
122
123
124
125
126
127
128
129
# File 'lib/wit_ruby/rest/bodyjson.rb', line 121

def one_expression_to_json(value_add)
  @values.each do |value|
    if value["value"] == value_add
      ## Generate new hash with the given first expression and dump it.
      expression_hash = {"expression" => value["expressions"][0]}
      return MultiJson.dump(expression_hash)
    end
  end
end

#one_value_to_jsonString

Used to properly convert the value in instance to JSON specifically for value calls.

Returns:

  • (String)

    JSON string of the one hash value.



113
114
115
116
# File 'lib/wit_ruby/rest/bodyjson.rb', line 113

def one_value_to_json
  ## Array of one hash, and convert it to JSON
  MultiJson.dump(@values[0])
end

#to_hHash

Used to overide current to_h method for OpenStruct. Returns a hash with string equivalent for symbols and adds the current instance variable

Returns:

  • (Hash)

    converted hash of the given BodyJson



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/wit_ruby/rest/bodyjson.rb', line 93

def to_h
  ## Use to_h on OpenStruct to get the current hash in the OpenStruct inheritance
  ## Depending on version of ruby, to_h might not be supported, so instead use table method
  ## to get the table instance variable
  current_os_hash = self.table
  ## Convert symbols to strings
  converted_hash = current_os_hash.reduce({}) do |memo, (k, v)|
    memo.merge({ k.to_s => v})
  end

  ## Merge values instance to this converted hash.
  converted_hash["values"] = self.values

  ## Return it.
  return converted_hash
end