diff --git a/MANIFEST b/MANIFEST index c041876..62a919d 100644 --- a/MANIFEST +++ b/MANIFEST @@ -12,13 +12,14 @@ README.rdoc RUBYS Rakefile TODO +bin/rets4r examples/client_get_object.rb examples/client_login.rb -examples/client_mapper.rb examples/client_metadata.rb -examples/client_parser.rb examples/client_search.rb examples/settings.yml +features/commandline.feature +features/support/env.rb lib/rets4r.rb lib/rets4r/auth.rb lib/rets4r/client.rb diff --git a/Rakefile b/Rakefile index 5dbf2da..e81e4de 100755 --- a/Rakefile +++ b/Rakefile @@ -22,6 +22,10 @@ namespace :test do end end +require 'cucumber/rake/task' +Cucumber::Rake::Task.new +task :default => :cucumber + require 'rdoc/task' RDoc::Task.new do |rdoc| if File.exist?('VERSION.yml') diff --git a/bin/rets4r b/bin/rets4r new file mode 100755 index 0000000..a1f8cff --- /dev/null +++ b/bin/rets4r @@ -0,0 +1,70 @@ +#!/usr/bin/env ruby +# rets4r: All-singing, all-dancing RETS cure-all +require 'active_support/core_ext/hash' +require 'active_support/ordered_hash' +require 'logger' +require 'thor' +require 'yaml' + +require 'rets4r' + +module RETS4R + class CLI < Thor + + desc "parse", "take a RETS XML Document and print how it looks" + def parse(filename) + xml = File.open filename + transaction = RETS4R::ResponseDocument::Search.safe_parse(xml).validate!.results + transaction.each {|row| puts sorted_print(row) } + end + + desc "map", "take a RETS XML Document and map it pretty" + def map(filename) + load_settings + xml = File.open filename + mapper = RETS4R::ListingMapper.new + RETS4R::Loader.load(xml) do |record| + attributes = mapper.map(record) + puts sorted_print(attributes) + end + end + + desc "login", 'login to your rets server' + def login + settings = load_settings + + client = RETS4R::Client.new(settings[:url]) + # client.logger = Logger.new(STDOUT) + + login_result = client.login(settings[:username], settings[:password]) + + if login_result.success? + puts "We successfully logged into the RETS server!" + + # Print the action URL results (if any) + puts login_result.secondary_response + + client.logout + + puts "We just logged out of the server." + else + puts "We were unable to log into the RETS server." + puts "Please check that you have set the login variables correctly." + end + end + private + def load_settings(filename = 'settings.yml') + RETS4R::ListingService.configurations = YAML.load_file(filename) + RETS4R::ListingService.env = ENV['RETS4RENV'] || 'development' + RETS4R::ListingService.connection + end + def sorted_print(hash) + hash.to_a. + sort {|a,b| a.first.to_s <=> b.first.to_s }. + map {|pair| pair.join(': ') }. + join(', ') + end + end +end + +RETS4R::CLI.start diff --git a/examples/client_login.rb b/examples/client_login.rb deleted file mode 100755 index eb4beb7..0000000 --- a/examples/client_login.rb +++ /dev/null @@ -1,40 +0,0 @@ -#!/usr/bin/env ruby -# -# This is an example of how to use the RETS client to log in and out of a server. -# -# You will need to set the necessary variables below. -# -############################################################################################# - -$:.unshift 'lib' -require 'active_support/core_ext/hash' -require 'logger' -require 'yaml' - -require 'rets4r' - -# Settings -settings_file = File.expand_path(File.join(File.dirname(__FILE__), "settings.yml")) -env = ENV['LISTING_ENV'] || 'development' -settings = YAML.load_file(settings_file)[env].symbolize_keys - -############################################################################################# - -client = RETS4R::Client.new(settings[:url]) -client.logger = Logger.new(STDOUT) - -login_result = client.login(settings[:username], settings[:password]) - -if login_result.success? - puts "We successfully logged into the RETS server!" - - # Print the action URL results (if any) - puts login_result.secondary_response - - client.logout - - puts "We just logged out of the server." -else - puts "We were unable to log into the RETS server." - puts "Please check that you have set the login variables correctly." -end diff --git a/examples/client_mapper.rb b/examples/client_mapper.rb deleted file mode 100755 index 121d13e..0000000 --- a/examples/client_mapper.rb +++ /dev/null @@ -1,17 +0,0 @@ -#!/usr/bin/env ruby -$:.unshift 'lib' -require 'yaml' - -require 'rets4r' - -listing_service_config_file = File.expand_path(File.join(File.dirname(__FILE__), "settings.yml")) -RETS4R::ListingService.configurations = YAML.load_file(listing_service_config_file) -RETS4R::ListingService.env = ENV['RETS4RENV'] || 'development' - -xml = ARGF - -mapper = RETS4R::ListingMapper.new -RETS4R::Loader.load(xml) do |record| - attributes = mapper.map(record) - puts attributes.inspect -end diff --git a/examples/client_parser.rb b/examples/client_parser.rb deleted file mode 100755 index f594710..0000000 --- a/examples/client_parser.rb +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/env ruby -# client_parser.rb: take a RETS XML Document and print how it looks -$:.unshift 'lib' -require 'rets4r' - -xml = ARGF - -transaction = ResponseDocument.safe_parse(xml).validate!.results -transaction.response.each {|row| puts row.inspect } \ No newline at end of file diff --git a/features/commandline.feature b/features/commandline.feature new file mode 100644 index 0000000..7604727 --- /dev/null +++ b/features/commandline.feature @@ -0,0 +1,52 @@ +Feature: Command Line Tool + As a new rets4r user + I want a tool to show how to use my data + + Background: + Given a file named "settings.yml" with: + """ + development: + url: http://www.dis.com:6103/rets/login + username: Joe + password: Schmoe + select: + MLSNUM: :mls + AGENTLIST_FULLNAME: :agent_full_name + LISTPRICE: :list_price + """ + Given a file named "search_compact.xml" with: + """ + + + + MLSNUM LISTPRICE AGENTLIST_FULLNAME + 1 2 Steve + 4 5 Bill + + + """ + + Scenario: parse + When I run `rets4r parse search_compact.xml` + Then the output should contain: + """ + AGENTLIST_FULLNAME: Steve, LISTPRICE: 2, MLSNUM: 1 + AGENTLIST_FULLNAME: Bill, LISTPRICE: 5, MLSNUM: 4 + """ + + Scenario: Map + When I run `rets4r map search_compact.xml` + Then the output should contain: + """ + agent_full_name: Steve, list_price: 2, mls: 1 + agent_full_name: Bill, list_price: 5, mls: 4 + """ + + Scenario: Login + When I run `rets4r login` + Then the output should contain: + """ + We successfully logged into the RETS server! + nil + We just logged out of the server. + """ \ No newline at end of file diff --git a/features/support/env.rb b/features/support/env.rb new file mode 100644 index 0000000..fb0a661 --- /dev/null +++ b/features/support/env.rb @@ -0,0 +1 @@ +require 'aruba/cucumber' diff --git a/rets4r.gemspec b/rets4r.gemspec index f7f47ec..ca7c570 100644 --- a/rets4r.gemspec +++ b/rets4r.gemspec @@ -17,6 +17,9 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency 'nokogiri', '>= 1.3.2' spec.add_runtime_dependency 'activesupport', '>= 2.3.2' + spec.add_runtime_dependency 'thor' + spec.add_development_dependency 'aruba' + spec.add_development_dependency 'cucumber' spec.add_development_dependency 'i18n' spec.add_development_dependency 'mocha' spec.add_development_dependency 'rake' diff --git a/test/test_quality.rb b/test/test_quality.rb index cfddc6b..be80852 100755 --- a/test/test_quality.rb +++ b/test/test_quality.rb @@ -59,6 +59,6 @@ def check_for_extra_spaces(filename) end def code_file?(filename) - !(filename =~ /.xml/) + !(filename =~ /.xml|.feature/) end end