Real localeze, not the symlink
This commit is contained in:
parent
410ca058ce
commit
2e64e49264
@ -1 +0,0 @@
|
||||
/home/tsouza/code/ruby/localeze_api/localeze.rb
|
84
app/models/localeze.rb
Normal file
84
app/models/localeze.rb
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user