From 2e64e492643920a57556c69b3dfdc327935f6d4f Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Tue, 14 Jan 2014 01:49:45 -0500 Subject: [PATCH] Real localeze, not the symlink --- app/models/localeze.rb | 85 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 84 insertions(+), 1 deletion(-) mode change 120000 => 100644 app/models/localeze.rb diff --git a/app/models/localeze.rb b/app/models/localeze.rb deleted file mode 120000 index dafa804..0000000 --- a/app/models/localeze.rb +++ /dev/null @@ -1 +0,0 @@ -/home/tsouza/code/ruby/localeze_api/localeze.rb \ No newline at end of file diff --git a/app/models/localeze.rb b/app/models/localeze.rb new file mode 100644 index 0000000..c018a18 --- /dev/null +++ b/app/models/localeze.rb @@ -0,0 +1,84 @@ +require 'cgi' +# To run, this needs the following gems +# 'savon', '~> 2.0' +# 'rails_config' +# 'gyoku', '~> 1.0'# +# +class LocalezeClient + def initialize + @client = Savon.client(wsdl: Settings.localeze_wsdl, namespace: Settings.localeze_wsdl, pretty_print_xml: true) + end + + # Check that a supplied listing is available to claim. + def check(listing) + value = record_to_xml(listing, true) + query = @client.call(:query, message: message(:add, value, :check)) + result = get_deep_value(query) + check_success?(result['ErrorCode']) ? true : get_errors(result) + end + + # Create the supplied listing. + def create(listing) + value = record_to_xml(listing, true) + query = @client.call(:query, message: message(:add, value, :create)) + result = get_deep_value(query) + create_success?(result['ErrorCode']) ? true : get_errors(result) # TODO[1] + end + + # Cache the categories + def categories + @_categories ||= categories! # Memoizing + end + + private + # Cached methods + # make a call to localeze to get a list of all the categories available + def categories! + query = @client.call(:query, message: message(:query, 'ACTIVEHEADINGS', :category)) + headings = get_heading(query) + headings['ActiveHeadings']['Heading'] + end + + # Not sure why, but Localeze returns either an ErrorMessage or a Validator with a Resolution if there's an error. + # This will get the error code regardless of where it is in one of these locations + def get_errors(result) + if result.has_key?('ErrorMessage') + return result['ErrorMessage'] + elsif result.has_key?('Validators') + return result['Validators']['Resolution'] # TODO[2] + end + end + + # The value that localeze gives us is very deep, this method + # cleans that up a little in the implementation depending on the element + def get_deep_value(query) + Hash.from_xml(query.to_hash[:query_response][:response][:result][:element][:value].to_s)['Response'] + end + + # This is a helper method to generate the giant dictionary you send as a message. + # Rather than needing to supply this dictionary every time, all you need to supply is the Action Key, + # the value to send, and the ElementId + def message(key, value, element) + {origination: { username: Settings.localeze_username, password: Settings.localeze_password }, + serviceId: Settings.localeze_serviceid, + transId: 1, + elements: {id: Settings.localeze_ids[element]}, + serviceKeys: {serviceKey: { id: Settings.localeze_ids[key], value: value} } + } + end + + # This will wrap a record hash into the xml format required by localeze, also escape if needed. + # The reason it doesn't just use to_xml, is because we needed the "Edition" attribute. + def record_to_xml(record, escape = false) + bmps = {'BPMSPost' => {'Record' => record }, :attributes! => { 'BPMSPost' => { 'Edition' => '1.1' }}} + Gyoku.xml(bmps, {:key_converter => :none}) + end + + # These two methods check that the error codes returned are Success codes. + def check_success? code + Settings[:localeze_check_success].include? code + end + def create_success? code + Settings[:localeze_create_success].include? code + end +end