class HerokuAPIStub::Generator

Public Class Methods

new(doc=nil) click to toggle source
# File lib/heroku_api_stub/generator.rb, line 3
def initialize(doc=nil)
  @doc = doc || read_default
end

Public Instance Methods

run() click to toggle source
# File lib/heroku_api_stub/generator.rb, line 7
def run
  @app = Sinatra.new(ServiceStub)
  @doc["resources"].each do |_, resource|
    example = build_example(resource["attributes"])
    resource["actions"].each do |name, action|
      method = action["method"]
      path   = action["path"]
      status = action["statuses"][0]
      required_params, optional_params = build_param_logic(action)
      # "{app}" to ":app"
      path.gsub!(/{([a-z_]*)}/, ':\1')
      #puts "method=#{method} path=#{path}"
      @app.send(method.downcase, path) do
        require_params!(required_params) if required_params
        validate_params!(optional_params) if optional_params
        if name == "List"
          [status, MultiJson.encode([example], pretty: true)]
        else
          [status, MultiJson.encode(example, pretty: true)]
        end
      end
    end
  end
  @app
end

Private Instance Methods

build_example(attributes) click to toggle source
# File lib/heroku_api_stub/generator.rb, line 35
def build_example(attributes)
  example = {}
  attributes.each do |name, info|
    next if !info["serialized"]
    keys = name.split(":")
    hash = if (leading_keys = keys[0...-1]).size > 0
      initialize_subhashes(example, leading_keys)
    else
      example
    end
    hash[keys.last] = info["example"]
  end
  example
end
build_param_logic(action) click to toggle source
# File lib/heroku_api_stub/generator.rb, line 50
def build_param_logic(action)
  required_params =
    action["attributes"] && action["attributes"]["required"]
  optional_params =
    action["attributes"] && action["attributes"]["optional"]
  if required_params && !optional_params
    optional_params = required_params
  end
  [required_params, optional_params]
end
initialize_subhashes(hash, keys) click to toggle source

returns the last subhash that was initialized

# File lib/heroku_api_stub/generator.rb, line 62
def initialize_subhashes(hash, keys)
  key = keys.shift
  subhash = hash[key] || (hash[key] = {})
  if keys.size > 0
    initialize_subhashes(subhash, keys)
  else
    subhash
  end
end
read_default() click to toggle source
# File lib/heroku_api_stub/generator.rb, line 72
def read_default
  path = File.expand_path("../../../data/doc.json", __FILE__)
  MultiJson.decode(File.read(path))
end