class Conclas::Utils::JsonMaker

Public Class Methods

new(hash_data) click to toggle source
# File lib/rb_conclas/utils/json_maker.rb, line 12
def initialize(hash_data)
    @data = hash_data
end

Public Instance Methods

get_json() click to toggle source
# File lib/rb_conclas/utils/json_maker.rb, line 103
def get_json
  validate_contents
  check_urls
  @data.to_json
end

Private Instance Methods

check_urls() click to toggle source
# File lib/rb_conclas/utils/json_maker.rb, line 83
def check_urls
  # check urls match with regex
  if @data.key?(:callback)
    if REGEX =~ @data["callback"] then
      raise Exceptions::URLInvalidException, "Invalid url callback"
    end
  end

  @data[:contents].each do |item|
    if item.key?("url")
      if not REGEX =~ item["url"]
        url = item["url"]
        raise Exceptions::URLInvalidException, "Invalid url #{url}"
      end
    end
  end
end
validate_contents() click to toggle source
# File lib/rb_conclas/utils/json_maker.rb, line 54
def validate_contents
  # Method to validate contents node
  @data[:contents].each do |item|
    if !item.key?("url") && !item.key?("doc")
      raise Exceptions::ContentsKeyException, "The contents is not valid."
    end
    if item.key?("url") && item.key?("doc")
      raise Exceptions::ContentsKeyException, "There can be the key doc in url in the same hash"
    end

    if item.key?("url")
      if StringHelper.is_empty item["url"]
        raise Exceptions::EmptyParameterException, "The parameter 'url' needs a value"
      end
      if not item["url"].is_a?(String)
        raise Exceptions::ParameterTypeError, "Url must be type string"
      end
    end

    if item.key?("doc")
      if item["doc"].size == 0
        raise Exceptions::ContentsKeyException, "Key contents invalid"
      else
        validate_key_doc(item["doc"])
      end
    end
  end
end
validate_key_doc(key_doc) click to toggle source

private methods

# File lib/rb_conclas/utils/json_maker.rb, line 17
def validate_key_doc(key_doc)
  # Validate key "doc"
  key_doc.each do |key, value|
    if key != "short_text" && key != "long_text" && key != "brands"
      raise Exceptions::ContentsKeyException, 'Invalid key "doc".'
    end
    if StringHelper.is_empty value
      raise Exceptions::EmptyParameterException, "The parameter '#{value}' needs a value"
    end
  end

  if key_doc.key?("short_text") && key_doc.key?("long_text")
    doc_len = key_doc["short_text"].length + key_doc["long_text"].length
    if doc_len < 25
      raise Exceptions::DocMinException, "The doc is not valid."
    end
  end
  # check short_text
  if key_doc.key?("short_text")
    if key_doc["short_text"].length > 1000
      raise Exceptions::DocMaxException, "The short_text doc is not valid."
    end
  end
  #check long_text
  if key_doc.key?("long_text")
    if key_doc["long_text"].length > 20_000
      raise Exceptions::DocMaxException, "The long_text doc is not valid."
    end
  end

  if key_doc.key?("brands")
    if key_doc["long_text"].length > 5000
      raise Exceptions::DocMaxException, "The brands doc is not valid."
    end
  end
end