Permalink
Browse files

Replaced the generate_definitions with a Rails generator

  • Loading branch information...
brewski committed Jun 29, 2012
1 parent e212693 commit adead3063d823827f621c30236473524fb5f8a1e
Showing with 47 additions and 29 deletions.
  1. +8 −7 README.md
  2. +0 −21 lib/fedex/tasks/fedex.rake
  3. +1 −1 lib/fedex/version.rb
  4. +38 −0 lib/generators/fedex/generate_definitions_generator.rb
View
@@ -10,27 +10,28 @@ This gem requires a large number of classes to communicate with FedEx. These cl
Once you have the WSDL files, you will need to create the ruby classes used in the SOAP requests. This is a one time process that can be handled by the gem.
#### Rails
-This gem includes a rake task to generate the class definitions and create an initializer to load them if you are using it from within a Rails application. Simply save the FedEx wsdl files to a directory (lib/fedex/web_services/wsdl in this example) and include the gem in your Gemfile:
+This gem includes a generator to create the class definitions and an initializer to load them if you are using it from within a Rails application. Simply save the FedEx wsdl files to a directory (lib/fedex/web_services/wsdl in this example) and include the gem in your Gemfile:
```ruby
gem 'fedex-web-services', :require => 'fedex'
```
-Then run the following rake task
+Then run the following generator:
- $ WSDL_DIR=lib/fedex/web_services/wsdl rake fedex:generate_definitions
+ $ rails g fedex:generate_definitions
+ Added /Users/brewski/nutsonline.com/git/kernelweb/lib/fedex/web_services/definitions
+ create config/initializers/fedex.rb
-You should see output looking like this.
+For more details, run the generator with the --help option:
- Added lib/fedex/web_services/definitions/
- Added config/initializers/fedex.rb
+ $ rails g fedex:generate_definitions --help
#### Manual creation
You can also manually generate the class files. To to this, run the following command:
```ruby
require 'fedex'
-Fedex::WebServices::Definitions.generate_definitions('lib', *Dir.glob('lib/web_services/wsdls/*.wsdl'))
+Fedex::WebServices::Definitions.generate_definitions('lib', *Dir.glob('lib/fedex/web_services/wsdl*.wsdl'))
```
This will create the directory lib/fedex/web_services/definitions/ with the FedEx web services class definitions in it. After you have created the classes, simply include the following lines in your application to load them:
View
@@ -1,21 +0,0 @@
-namespace :fedex do
- desc "Generate the ruby definition files from the FedEx wsdl files"
- task :generate_definitions do
- wsdl_dir = ENV['WSDL_DIR']
- if (!Dir.exist?(wsdl_dir || ""))
- puts "Set the WSDL_DIR environment variable to location of the Fedex wsdl files"
- else
- Fedex::WebServices::Definitions.generate_definitions(
- File.join(Rails.root, 'lib'),
- *Dir.glob(File.join(ENV['WSDL_DIR'], '*.wsdl'))
- )
-
- File.open(File.join(Rails.root, 'config', 'initializers', 'fedex.rb'), "w") do |file|
- file.puts "Fedex::WebServices::Definitions.load_definitions('lib')"
- end
-
- puts "Added lib/fedex/web_services/definitions/"
- puts "Added config/initializers/fedex.rb"
- end
- end
-end
View
@@ -1,3 +1,3 @@
module Fedex
- VERSION = "1.1.47"
+ VERSION = "1.1.48"
end
@@ -0,0 +1,38 @@
+module Fedex
+ module Generators
+ class GenerateDefinitionsGenerator < ::Rails::Generators::Base
+ FEDEX_INITIALIZER_FILE = File.join(Rails.root, 'config', 'initializers', 'fedex.rb')
+
+ class_option :wsdl_dir,
+ default: File.join(%w(lib fedex web_services wsdl)),
+ type: :string,
+ desc: "directory where the fedex wsld files are stored"
+
+
+ class_option :skip_initializer,
+ default: false,
+ type: :boolean,
+ desc: "install a rails initializer to load the wsdl definitions"
+
+ desc "Generate the FedEx WebServices class definitions and create an initializer to load them"
+
+ def install
+ wsdl_dir = options[:wsdl_dir]
+ wsdl_files = Dir.glob(File.join(wsdl_dir, '*.wsdl'))
+ target_lib_dir = File.join(Rails.root, 'lib')
+
+ if (wsdl_files.empty?)
+ say("Could not find any wsdl files in #{wsdl_dir}", :red)
+ else
+ Fedex::WebServices::Definitions.generate_definitions(target_lib_dir, *wsdl_files)
+ say("Added #{File.join(target_lib_dir, Fedex::WebServices::Definitions::MODULE_PATH)}", :green)
+
+ unless (options[:skip_initializer])
+ create_file(FEDEX_INITIALIZER_FILE,
+ "Fedex::WebServices::Definitions.load_definitions('#{target_lib_dir}')")
+ end
+ end
+ end
+ end
+ end
+end

0 comments on commit adead30

Please sign in to comment.