diff --git a/app/controllers/neopets_users_controller.rb b/app/controllers/neopets_users_controller.rb new file mode 100644 index 00000000..e9c4fc2d --- /dev/null +++ b/app/controllers/neopets_users_controller.rb @@ -0,0 +1,37 @@ +class NeopetsUsersController < ApplicationController + before_filter :authenticate_user!, :build_neopets_user + + rescue_from NeopetsUser::NotFound, :with => :not_found + + def new + @neopets_user.username = current_user.neopets_username + end + + def create + @neopets_user.username = params[:neopets_user][:username] + @neopets_user.load! + @neopets_user.save_hangers! + + message = "Success! We loaded user \"#{@neopets_user.username}\"" + unless @neopets_user.hangers.empty? + message << " and added #{@neopets_user.hangers.size} items." + else + message << ", but already had all of this data recorded." + end + + flash[:success] = message + redirect_to user_closet_hangers_path(current_user) + end + + protected + + def build_neopets_user + @neopets_user = NeopetsUser.new current_user + end + + def not_found + flash.now[:alert] = "Could not find user \"#{@neopets_user.username}\". Did you spell it correctly?" + render :action => :new + end +end + diff --git a/app/models/neopets_user.rb b/app/models/neopets_user.rb new file mode 100644 index 00000000..e7df0789 --- /dev/null +++ b/app/models/neopets_user.rb @@ -0,0 +1,58 @@ +require 'open-uri' + +class NeopetsUser + include ActiveModel::Conversion + extend ActiveModel::Naming + + attr_accessor :username + attr_reader :hangers + + def initialize(app_user) + @app_user = app_user + end + + def load! + doc = Nokogiri::HTML(open(url)) + + unless pets_wrapper = doc.at('#userneopets') + raise NotFound, "Could not find user #{username}" + end + + pets = pets_wrapper.css('a[href^="/petlookup.phtml"]').map do |link| + name = link['href'].split('=').last + Pet.find_or_initialize_by_name(name) + end + + items = pets.each(&:load!).map(&:items).flatten + item_ids = items.map(&:id) + + existing_hanger_item_ids = @app_user.closet_hangers.select(:item_id).where(:item_id => item_ids).map(&:item_id) + + @hangers = [] + items.each do |item| + next if existing_hanger_item_ids.include?(item.id) + hanger = @app_user.closet_hangers.build + hanger.item = item + hanger.quantity = 1 + @hangers << hanger + end + end + + def save_hangers! + ClosetHanger.transaction { @hangers.each(&:save!) } + end + + def persisted? + false + end + + protected + + URL_PREFIX = 'http://www.neopets.com/userlookup.phtml?user=' + def url + URL_PREFIX + @username + end + + class NotFound < RuntimeError;end +end + diff --git a/app/stylesheets/closet_hangers/_index.sass b/app/stylesheets/closet_hangers/_index.sass index 66f9c501..18178b3f 100644 --- a/app/stylesheets/closet_hangers/_index.sass +++ b/app/stylesheets/closet_hangers/_index.sass @@ -90,6 +90,7 @@ body.closet_hangers-index display: none #closet-hangers-extras + font-size: 85% margin: bottom: 2em top: 2em @@ -100,7 +101,6 @@ body.closet_hangers-index margin: 0 0.5em #closet-hangers-share - font-size: 85% margin-bottom: 1em label diff --git a/app/stylesheets/neopets_users/_form.sass b/app/stylesheets/neopets_users/_form.sass new file mode 100644 index 00000000..d1de9226 --- /dev/null +++ b/app/stylesheets/neopets_users/_form.sass @@ -0,0 +1,13 @@ +body.neopets_users-new, body.neopets_users-create + +secondary-nav + + #neopets-user-form + clear: both + + label + font-weight: bold + margin-right: 1em + + &:after + content: ":" + diff --git a/app/stylesheets/screen.sass b/app/stylesheets/screen.sass index 289e15f3..c4f32713 100644 --- a/app/stylesheets/screen.sass +++ b/app/stylesheets/screen.sass @@ -10,6 +10,7 @@ @import closet_hangers/petpage @import closet_lists/form @import neopets_pages/new +@import neopets_users/form @import contributions/index @import items @import items/index diff --git a/app/views/closet_hangers/index.html.haml b/app/views/closet_hangers/index.html.haml index 27e27efa..d4aca491 100644 --- a/app/views/closet_hangers/index.html.haml +++ b/app/views/closet_hangers/index.html.haml @@ -78,6 +78,7 @@ = link_to "Import from closet", new_closet_page_path = link_to "Import from SDB", new_safety_deposit_page_path + = link_to "Import from pets", new_neopets_user_path = link_to "Export to petpage", petpage_user_closet_hangers_path(@user) diff --git a/app/views/neopets_users/new.html.haml b/app/views/neopets_users/new.html.haml new file mode 100644 index 00000000..5b3e6f41 --- /dev/null +++ b/app/views/neopets_users/new.html.haml @@ -0,0 +1,17 @@ +- title 'Import from pets' +- secondary_nav do + = link_to 'Back to Your Items', user_closet_hangers_path(current_user), :class => 'button' + += form_for @neopets_user, :html => {:id => 'neopets-user-form'} do |f| + %p + Since the items your pets are wearing don't show up in the Neopets closet, + we have this tool to help you import those items without a second thought. + + %p + Just enter your Neopets username in the box below, then we'll load all + your pets and import what they're wearing. Have fun! + + = f.label :username, 'Neopets Username' + = f.text_field :username + = f.submit "Import pets" + diff --git a/config/routes.rb b/config/routes.rb index a0b6dc4e..64be32bb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -36,6 +36,8 @@ OpenneoImpressItems::Application.routes.draw do |map| resources :safety_deposit_pages, :only => [:new, :create], :controller => 'neopets_pages', :path => 'sdb/pages', :type => 'sdb' + + resources :neopets_users, :only => [:new, :create], :path => 'neopets-users' end match '/users/current-user/outfits' => 'outfits#index', :as => :current_user_outfits diff --git a/public/stylesheets/compiled/screen.css b/public/stylesheets/compiled/screen.css index a0359c42..72e7bc6f 100644 --- a/public/stylesheets/compiled/screen.css +++ b/public/stylesheets/compiled/screen.css @@ -751,11 +751,12 @@ body.closet_hangers-index #closet-hangers-help.hidden { } /* line 92, ../../../app/stylesheets/closet_hangers/_index.sass */ body.closet_hangers-index #closet-hangers-extras { + font-size: 85%; margin-bottom: 2em; margin-top: 2em; text-align: center; } -/* line 98, ../../../app/stylesheets/closet_hangers/_index.sass */ +/* line 99, ../../../app/stylesheets/closet_hangers/_index.sass */ body.closet_hangers-index #closet-hangers-extras a { /* http://www.zurb.com/blog_uploads/0000/0617/buttons-03.html */ -moz-border-radius: 5px; @@ -787,9 +788,8 @@ body.closet_hangers-index #closet-hangers-extras a:hover { body.closet_hangers-index #closet-hangers-extras a:active { top: 1px; } -/* line 102, ../../../app/stylesheets/closet_hangers/_index.sass */ +/* line 103, ../../../app/stylesheets/closet_hangers/_index.sass */ body.closet_hangers-index #closet-hangers-share { - font-size: 85%; margin-bottom: 1em; } /* line 106, ../../../app/stylesheets/closet_hangers/_index.sass */ @@ -1461,6 +1461,34 @@ body.neopets_pages-new ol p, body.neopets_pages-create ol p { margin: 0; } +/* line 2, ../../../app/stylesheets/partials/_secondary_nav.sass */ +body.neopets_users-new #title, body.neopets_users-create #title { + float: left; + margin-right: 0.5em; +} +/* line 6, ../../../app/stylesheets/partials/_secondary_nav.sass */ +body.neopets_users-new .flash, body.neopets_users-create .flash { + clear: both; +} +/* line 9, ../../../app/stylesheets/partials/_secondary_nav.sass */ +body.neopets_users-new #secondary-nav, body.neopets_users-create #secondary-nav { + display: block; + margin-top: 0.75em; +} +/* line 4, ../../../app/stylesheets/neopets_users/_form.sass */ +body.neopets_users-new #neopets-user-form, body.neopets_users-create #neopets-user-form { + clear: both; +} +/* line 7, ../../../app/stylesheets/neopets_users/_form.sass */ +body.neopets_users-new #neopets-user-form label, body.neopets_users-create #neopets-user-form label { + font-weight: bold; + margin-right: 1em; +} +/* line 11, ../../../app/stylesheets/neopets_users/_form.sass */ +body.neopets_users-new #neopets-user-form label:after, body.neopets_users-create #neopets-user-form label:after { + content: ":"; +} + /* line 1, ../../../app/stylesheets/contributions/_index.sass */ body.contributions-index { text-align: center;