Skip to content

Instantly share code, notes, and snippets.

@Heavyblade
Created June 6, 2012 22:02
Show Gist options
  • Save Heavyblade/2885111 to your computer and use it in GitHub Desktop.
Save Heavyblade/2885111 to your computer and use it in GitHub Desktop.
protected
def keymanager_call(method, additional_params = {})
params = {
'clientapp[client_app_name]' => self.name,
'clientapp[type]' => self.app_type,
'clientapp[consumerid]' => self.consumer_id,
'clientapp[att_guid]' => self.user.att_uid,
'clientapp[description]' => self.description,
'clientapp[api_group_name]' => ENV['KEYMANAGER_API_GROUP'],
'clientapp[voice_app]' => self.voice_app,
'clientapp[voice_app_type]' => self.voice_app_type,
'clientapp[voice_url]' => self.voice_url,
'clientapp[messaging_url]' => self.messaging_url,
'clientapp[description]' => self.description,
'clientapp[public]' => self.public?,
'clientapp[url]' => self.website,
'clientapp[support_url]' => self.support_url,
'clientapp[callback_url]' => self.callback_url
}
params.merge!(addtional_params) unless addtional_params.blank?
RestClient.get(ENV['KEYMANAGER_PROXY_URI'] + "/consumer/clientapp/#{method}", :params => params )
end
def create_auth_record
return true if ["test","development"].include?(Rails.env) || !self.key.to_s.empty?
begin
response = Hash.from_xml keymanager_call "register"
self.key = response['clientapp']["consumer"]["key"]
self.secret = response['clientapp']["consumer"]["secret"]
@no_update = true
self.save
true
rescue RestClient::InternalServerError => e
result = Nokogiri::XML(e.http_body)
Rails.logger.debug e.http_body
if result.xpath('//consumer/clientapp/error/message').text =~ /Name has already been taken/
errors.add(:name, :not_unique)
else
errors[:base] << "Unable to connect to key manager. Please try again later"
end
end
end
def update_auth_record
return true if ["test","development"].include?(Rails.env) or @no_update
begin
host = "#{Rails.env.production? ? "https" : "http"}://#{ENV['ATT_APIMATRIX_HOST']}#{":3000" unless Rails.env.production?}"
keymanager_call "update", { 'clientapp[icon_medium]' => (host + self.icon.url(:medium)),
'clientapp[icon_thumb]' => (host + self.icon.url(:thumb))}
true
rescue RestClient::InternalServerError => e
result = Nokogiri::XML(e.http_body)
if result.xpath('//consumer/clientapp/error/message').text =~ /Name has already been taken/
errors.add(:name, :not_unique)
else
errors[:base] << "Unable to connect to key manager. Please try again later"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment