class Apipie::Client::Generator
Attributes
doc[R]
resource[R]
resource_key[R]
Public Class Methods
destination_root(name, suffix)
click to toggle source
# File lib/apipie/client/generator.rb, line 32 def self.destination_root(name, suffix) File.join(FileUtils.pwd, "#{name}#{suffix}") end
new(*args)
click to toggle source
Calls superclass method
# File lib/apipie/client/generator.rb, line 23 def initialize(*args) super @doc = Apipie.to_json(version)[:docs] end
source_root()
click to toggle source
# File lib/apipie/client/generator.rb, line 28 def self.source_root File.expand_path("../template", __FILE__) end
start(client_name, subject = :all, suffix = '_client', version = nil)
click to toggle source
Calls superclass method
# File lib/apipie/client/generator.rb, line 36 def self.start(client_name, subject = :all, suffix = '_client', version = nil) name = client_name.parameterize.underscore suffix = suffix.parameterize.underscore super([name, subject, suffix, version], :destination_root => destination_root(name, suffix)) end
Public Instance Methods
all?()
click to toggle source
# File lib/apipie/client/generator.rb, line 42 def all? subject == :all end
generate_cli()
click to toggle source
# File lib/apipie/client/generator.rb, line 46 def generate_cli full_name = "#{name}#{suffix}" template("README.tt", "README") template("Gemfile.tt", "Gemfile") template("Rakefile.tt", "Rakefile") template("a_name.gemspec.tt", "#{full_name}.gemspec") template("lib/a_name.rb.tt", "lib/#{full_name}.rb") template("lib/a_name/version.rb.tt", "lib/#{full_name}/version.rb") create_file "lib/#{full_name}/documentation.json", JSON.dump(Apipie.to_json) copy_file "lib/a_name/config.yml", "lib/#{full_name}/config.yml" if all? template("bin/bin.rb.tt", "bin/#{full_name}") chmod("bin/#{full_name}", 0755) end doc[:resources].each do |key, resource| @resource_key, @resource = key, resource if all? template("lib/a_name/commands/cli.rb.tt", "lib/#{full_name}/commands/#{resource_name}.thor") end template("lib/a_name/resources/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 94 def api(method) method[:apis].first end
camelizer(string)
click to toggle source
# File lib/apipie/client/generator.rb, line 71 def camelizer(string) string = string.sub(/^[a-z\d]*/) { $&.capitalize } string.gsub(/(?:_|(\/))([a-z\d]*)/i) { "#{$2.capitalize}" } end
class_base()
click to toggle source
# File lib/apipie/client/generator.rb, line 76 def class_base @class_base ||= camelizer(name) end
class_suffix()
click to toggle source
# File lib/apipie/client/generator.rb, line 80 def class_suffix @class_suffix ||= camelizer(suffix) end
client_args(method)
click to toggle source
# File lib/apipie/client/generator.rb, line 102 def client_args(method) params_in_path(method).dup end
params_in_path(method)
click to toggle source
# File lib/apipie/client/generator.rb, line 98 def params_in_path(method) api(method)[:api_url].scan(/:([^\/]*)/).map(&:first) end
plaintext(text)
click to toggle source
# File lib/apipie/client/generator.rb, line 84 def plaintext(text) text.gsub(/<.*?>/, '').gsub("\n", ' ').strip end
resource_name()
click to toggle source
Resource related helper methods:
# File lib/apipie/client/generator.rb, line 90 def resource_name resource[:name].gsub(/\s/, "_").downcase.singularize end
substituted_url(method)
click to toggle source
# File lib/apipie/client/generator.rb, line 106 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 110 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(method)
click to toggle source
# File lib/apipie/client/generator.rb, line 116 def validation(method) stringify = lambda do |object| case object when Hash clone = object.dup object.keys.each { |key| clone[key.to_s] = stringify[clone.delete(key)] } clone when Array object.map { |value| stringify[value] } else object end end Apipie::Client::Base.construct_validation_hash(stringify[method]) end