From f251500aa410c789d43b79433bf73b3b2737715a Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Thu, 13 Feb 2014 15:44:56 -0500 Subject: [PATCH] added rake command --- Gemfile | 1 + Gemfile.lock | 8 +++++++ app/assets/javascripts/application.js | 6 ------ app/assets/stylesheets/rest_api.css | 31 +++++++++++++++++++++++++++ app/models/localeze_category.rb | 6 ++++++ app/models/localeze_client.rb | 11 +++++----- lib/tasks/localeze_categories.rake | 16 ++++++++++++++ 7 files changed, 67 insertions(+), 12 deletions(-) create mode 100644 app/assets/stylesheets/rest_api.css create mode 100644 lib/tasks/localeze_categories.rake diff --git a/Gemfile b/Gemfile index d4468d2..766d01e 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,7 @@ gem "meta_search", '>= 1.1.0.pre' gem "rails_config" gem "savon", "~> 2.0" gem "gyoku", "~> 1.0" +gem "pry" group :assets do gem "coffee-rails", "~> 3.2.1" diff --git a/Gemfile.lock b/Gemfile.lock index 25b4691..3f77b88 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -39,6 +39,7 @@ GEM railties (>= 3.0) sass-rails (>= 3.2) chunky_png (1.3.0) + coderay (1.1.0) coffee-rails (3.2.2) coffee-script (>= 2.2.0) railties (~> 3.2.0) @@ -81,6 +82,7 @@ GEM activerecord (~> 3.1) activesupport (~> 3.1) polyamorous (~> 0.5.0) + method_source (0.8.2) mime-types (1.25.1) mini_portile (0.5.2) multi_json (1.8.4) @@ -90,6 +92,10 @@ GEM polyamorous (0.5.0) activerecord (~> 3.0) polyglot (0.3.3) + pry (0.9.12.6) + coderay (~> 1.0) + method_source (~> 0.8) + slop (~> 3.4) rack (1.4.5) rack-cache (1.2) rack (>= 0.4) @@ -131,6 +137,7 @@ GEM nokogiri (>= 1.4.0) nori (~> 2.3.0) wasabi (~> 3.2.2) + slop (3.4.7) sprockets (2.2.2) hike (~> 1.2) multi_json (~> 1.0) @@ -167,6 +174,7 @@ DEPENDENCIES jquery-rails (= 2.3.0) maskedinput-rails meta_search (>= 1.1.0.pre) + pry rails (= 3.2.12) rails_config sass-rails (~> 3.2.3) diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 360ee6b..06265b4 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -15,11 +15,5 @@ //= require twitter/bootstrap //= require maskedinput //= require chosen-jquery -//= require handlebars -//= require ember -//= require ember-data -//= require_self -//= require listpush //= require_tree . -ListPushApp = Ember.Application.create(); diff --git a/app/assets/stylesheets/rest_api.css b/app/assets/stylesheets/rest_api.css new file mode 100644 index 0000000..1a80c72 --- /dev/null +++ b/app/assets/stylesheets/rest_api.css @@ -0,0 +1,31 @@ +body { background-color: #5f7395; color: #333; } + +body, p, ol, ul, td { + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; +} + +pre { + background-color: #eee; + padding: 10px; + font-size: 11px; +} + +a { color: #000; } +a:visited { color: #666; } +a:hover { color: #fff; background-color:#000; } + +#main { + background-color: #fff; + border: solid #000 1px; + margin: 5em; + height: 30em; + padding: 1em; +} + +#notice { + background-color: #e1facf; + border: solid #97C36d 1px; + padding: 0.5em; +} \ No newline at end of file diff --git a/app/models/localeze_category.rb b/app/models/localeze_category.rb index 5296f8b..43ef3dd 100644 --- a/app/models/localeze_category.rb +++ b/app/models/localeze_category.rb @@ -1,4 +1,10 @@ class LocalezeCategory < ActiveRecord::Base attr_accessible :name has_and_belongs_to_many :list_entries + + def self.find_or_create_by_name(name, &block) + obj = self.find_or_create_by_name( name ) || self.new(:name => name) + yield obj + obj.save + end end diff --git a/app/models/localeze_client.rb b/app/models/localeze_client.rb index 3cc6baf..336b86d 100644 --- a/app/models/localeze_client.rb +++ b/app/models/localeze_client.rb @@ -9,7 +9,7 @@ class LocalezeClient def check(listing) value = record_to_xml(listing, true) query = @client.call(:query, message: message(:add, value, :check)) - result = get_deep_value(query) + result = get_deep_value(query)['Response'] check_successful?(result['ErrorCode']) ? true : result['ErrorMessage'] end @@ -17,7 +17,7 @@ class LocalezeClient def create(listing) value = record_to_xml(listing, true) query = @client.call(:query, message: message(:add, value, :create)) - result = get_deep_value(query) + result = get_deep_value(query)['Response'] status = create_successful?(result) handle_status(status) end @@ -32,8 +32,7 @@ class LocalezeClient # 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'] + headings = get_deep_value(query)['ActiveHeadings']['Heading'] end # Not sure why, but Localeze returns either an ErrorMessage or a Validator with a Resolution if there's an error. @@ -49,9 +48,9 @@ class LocalezeClient # 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'] + Hash.from_xml(query.to_hash[:query_response][:response][:result][:element][:value].to_s) 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 diff --git a/lib/tasks/localeze_categories.rake b/lib/tasks/localeze_categories.rake new file mode 100644 index 0000000..aaa296f --- /dev/null +++ b/lib/tasks/localeze_categories.rake @@ -0,0 +1,16 @@ + namespace :localeze do + desc "Update and Get new LocalezeCategories" + task :import => :environment do + before = LocalezeCategory.count + categories = LocalezeClient.new.categories + ActiveRecord::Base.transaction do + categories.each do |cat| + category = LocalezeCategory.where(name: cat).first_or_create! + end + + if before < LocalezeCategory.count + puts "Imported #{LocalezeCategory.count - before} categories." + end + end + end + end \ No newline at end of file