class Apipie::Client::Generator

Attributes

doc[R]
resource[R]

Public Instance Methods

all?() click to toggle source
# File lib/apipie/client/generator.rb, line 39
def all?
  subject == :all
end
generate_cli() click to toggle source
# File lib/apipie/client/generator.rb, line 43
def generate_cli
  full_name = "#{name}#{suffix}"
  template("README.tt", "README")
  template("Gemfile.tt", "Gemfile")
  template("Rakefile.tt", "Rakefile")
  template("client.gemspec.tt", "#{full_name}.gemspec")
  template("client.rb.tt", "lib/#{full_name}.rb")
  template("base.rb.tt", "lib/#{full_name}/base.rb")
  template("rest_client_oauth.rb.tt", "lib/#{full_name}/rest_client_oauth.rb")
  template("version.rb.tt", "lib/#{full_name}/version.rb")
  if all?
    template("bin.rb.tt", "bin/#{full_name}")
    chmod("bin/#{full_name}", 0755)
    template("cli_command.rb.tt", "lib/#{full_name}/cli_command.rb")
  end
  doc[:resources].each do |key, resource|
    @resource = resource
    if all?
      template("cli.rb.tt", "lib/#{full_name}/commands/#{resource_name}.thor")
    end
    template("resource.rb.tt", "lib/#{full_name}/resources/#{resource_name}.rb")
  end
end

Protected Instance Methods

api(method) click to toggle source
# File lib/apipie/client/generator.rb, line 92
def api(method)
  method[:apis].first
end
camelizer(string) click to toggle source
# File lib/apipie/client/generator.rb, line 69
def camelizer(string)
  string = string.sub(%r^[a-z\d]*/) { $&.capitalize }
  string.gsub(%r(?:_|(\/))([a-z\d]*)/) { "#{$2.capitalize}" }
end
class_base() click to toggle source
# File lib/apipie/client/generator.rb, line 74
def class_base
  @class_base ||= camelizer(name)
end
class_suffix() click to toggle source
# File lib/apipie/client/generator.rb, line 78
def class_suffix
  @class_suffix ||= camelizer(suffix)
end
client_args(method) click to toggle source
# File lib/apipie/client/generator.rb, line 100
def client_args(method)
  client_args = params_in_path(method).dup
  client_args << "params = {}" if method[:params].any?
  client_args
end
params_in_path(method) click to toggle source
# File lib/apipie/client/generator.rb, line 96
def params_in_path(method)
  api(method)[:api_url].scan(%r:([^\/]*)/).map(&:first)
end
plaintext(text) click to toggle source
# File lib/apipie/client/generator.rb, line 82
def plaintext(text)
  text.gsub(%r<.*?>/, '').gsub("\n",' ').strip
end
resource_name() click to toggle source

Resource related helper methods:

# File lib/apipie/client/generator.rb, line 88
def resource_name
  resource[:name].gsub(%r\s/,"_").downcase.singularize
end
substituted_url(method) click to toggle source
# File lib/apipie/client/generator.rb, line 116
def substituted_url(method)
  params_in_path(method).reduce(api(method)[:api_url]) { |u, p| u.sub(":#{p}","\#{#{p}}")}
end
transformation_hash(method) click to toggle source
# File lib/apipie/client/generator.rb, line 120
def transformation_hash(method)
  method[:params].find_all { |p| p[:expected_type] == "hash" && !p[:params].nil? }.reduce({}) do |h, p|
    h.update(p[:name] => p[:params].map { |pp| pp[:name] })
  end
end
validation_hash(method) click to toggle source
# File lib/apipie/client/generator.rb, line 106
def validation_hash(method)
  if method[:params].any? { |p| p[:params] }
    method[:params].reduce({}) do |h, p|
      h.update(p[:name] => (p[:params] ? p[:params].map { |pp| pp[:name] } : nil))
    end
  else
    method[:params].map { |p| p[:name] }
  end
end

Public Class Methods

destination_root(name, suffix) click to toggle source
# File lib/apipie/client/generator.rb, line 29
def self.destination_root(name, suffix)
  File.join(FileUtils.pwd, "#{name}#{suffix}")
end
new(*args) click to toggle source
# File lib/apipie/client/generator.rb, line 20
def initialize(*args)
  super
  @doc = Apipie.to_json()[:docs]
end
source_root() click to toggle source
# File lib/apipie/client/generator.rb, line 25
def self.source_root
  File.expand_path("../template", __FILE__)
end
start(client_name, subject = :all, suffix = '_client') click to toggle source
# File lib/apipie/client/generator.rb, line 33
def self.start(client_name, subject = :all, suffix = '_client')
  name = client_name.parameterize.underscore
  suffix = suffix.parameterize.underscore
  super([name, subject, suffix], :destination_root => destination_root(name, suffix))
end