import items from pets

This commit is contained in:
Emi Matchu 2011-08-03 11:35:06 -04:00
parent bad1eb13a5
commit 2398f34071
9 changed files with 161 additions and 4 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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: ":"

View file

@ -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

View file

@ -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)

View file

@ -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"

View file

@ -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

View file

@ -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;