Class: Grapethor::Endpoint

Inherits:
Thor::Group
  • Object
show all
Includes:
Utils, Thor::Actions
Defined in:
lib/grapethor/generators/endpoint.rb

Constant Summary

Constants included from Utils

Utils::ATTRS_MAP, Utils::CONFIG_FILENAME, Utils::TEST_FRAMEWORK_DIRNAME

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#api_versionObject (readonly)

Returns the value of attribute api_version.



9
10
11
# File 'lib/grapethor/generators/endpoint.rb', line 9

def api_version
  @api_version
end

#app_pathObject (readonly)

Returns the value of attribute app_path.



9
10
11
# File 'lib/grapethor/generators/endpoint.rb', line 9

def app_path
  @app_path
end

#end_descObject (readonly)

Returns the value of attribute end_desc.



9
10
11
# File 'lib/grapethor/generators/endpoint.rb', line 9

def end_desc
  @end_desc
end

#end_methodObject (readonly)

Returns the value of attribute end_method.



9
10
11
# File 'lib/grapethor/generators/endpoint.rb', line 9

def end_method
  @end_method
end

#end_nameObject (readonly)

Returns the value of attribute end_name.



9
10
11
# File 'lib/grapethor/generators/endpoint.rb', line 9

def end_name
  @end_name
end

#end_paramsObject (readonly)

Returns the value of attribute end_params.



9
10
11
# File 'lib/grapethor/generators/endpoint.rb', line 9

def end_params
  @end_params
end

#end_queryObject (readonly)

Returns the value of attribute end_query.



9
10
11
# File 'lib/grapethor/generators/endpoint.rb', line 9

def end_query
  @end_query
end

#end_query_sampleObject (readonly)

Returns the value of attribute end_query_sample.



9
10
11
# File 'lib/grapethor/generators/endpoint.rb', line 9

def end_query_sample
  @end_query_sample
end

#end_query_with_paramsObject (readonly)

Returns the value of attribute end_query_with_params.



9
10
11
# File 'lib/grapethor/generators/endpoint.rb', line 9

def end_query_with_params
  @end_query_with_params
end

#end_resourceObject (readonly)

Returns the value of attribute end_resource.



9
10
11
# File 'lib/grapethor/generators/endpoint.rb', line 9

def end_resource
  @end_resource
end

#request_pathObject (readonly)

Returns the value of attribute request_path.



9
10
11
# File 'lib/grapethor/generators/endpoint.rb', line 9

def request_path
  @request_path
end

#request_path_sampleObject (readonly)

Returns the value of attribute request_path_sample.



9
10
11
# File 'lib/grapethor/generators/endpoint.rb', line 9

def request_path_sample
  @request_path_sample
end

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/grapethor/generators/endpoint.rb', line 24

def self.exit_on_failure?
  true
end

.source_rootObject



28
29
30
# File 'lib/grapethor/generators/endpoint.rb', line 28

def self.source_root
  File.join(__dir__, '..')
end

Instance Method Details

#create_endpointObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/grapethor/generators/endpoint.rb', line 102

def create_endpoint
  report("Creating new endpoint...") do
    unless File.exist?("#{app_path}/api/#{api_version}/#{end_resource_plural}.rb")
      directory "templates/endpoint/api/\%api_version\%", "#{app_path}/api/#{api_version}"
    end

    insert_into_file "#{app_path}/api/#{api_version}/#{end_resource_plural}.rb",
                     end_content,
                     after: "resource :#{end_resource.pluralize} do\n"

    insert_into_file "#{app_path}/api/#{api_version}/base.rb",
                     "\s\s\s\smount API#{api_version}::#{end_resource.classify.pluralize}\n",
                     before: "\s\s\s\s# mount API#{api_version}::<ResourceOrEndpointClass>\n"

    unless File.exist?("#{app_path}/#{test_dirname}/api/#{api_version}/#{end_resource_plural}_#{test_dirname}.rb")
      directory "templates/endpoint_#{app_test_framework}", app_path
    end

    insert_into_file "#{app_path}/#{test_dirname}/api/#{api_version}/#{end_resource_plural}_#{test_dirname}.rb",
                     end_test_content,
                     after: "\s\s### grapethor works here ###\n"
  end
end

#parse_args_and_optsObject



73
74
75
76
77
78
79
80
81
82
# File 'lib/grapethor/generators/endpoint.rb', line 73

def parse_args_and_opts
  @end_resource          = resource.downcase.singularize
  @end_name              = name.downcase.singularize
  @end_method            = options[:method].upcase
  @end_params            = options[:params]
  @end_query             = options[:query].map { |k, v| [k, v.downcase] }.to_h
  @end_query_with_params = options[:params].merge(options[:query]).map { |k, v| [k, v.downcase] }.to_h
  @api_version           = options[:version].downcase
  @app_path              = options[:path]
end

#prepare_endpointObject



85
86
87
88
89
90
91
# File 'lib/grapethor/generators/endpoint.rb', line 85

def prepare_endpoint
  prepare_request_path

  prepare_request_path_sample

  @end_desc = options[:desc].empty? ? "#{end_method} #{request_path}" : options[:desc]
end

#validate_target_apiObject



94
95
96
97
98
99
# File 'lib/grapethor/generators/endpoint.rb', line 94

def validate_target_api
  unless api_version_exists?
    alert "API version '#{api_version}' does not exist!"
    exit
  end
end