forked from OpenNeo/impress
ta da, proper importing refactor
This commit is contained in:
parent
deb0aa90f0
commit
b9e3d7bff5
2 changed files with 53 additions and 23 deletions
|
@ -8,7 +8,7 @@ class NeopetsPageImportTasksController < ApplicationController
|
||||||
rescue_from NeopetsPage::ParseError, with: :on_parse_error
|
rescue_from NeopetsPage::ParseError, with: :on_parse_error
|
||||||
|
|
||||||
def create
|
def create
|
||||||
neopets_page = NeopetsPage.new(params[:page_type], params[:expected_index], params[:neopets_page][:source])
|
neopets_page = NeopetsPage.new(params[:page_type], params[:expected_index].to_i, params[:neopets_page][:source])
|
||||||
|
|
||||||
@import_task = neopets_page.build_import_task(current_user, params[:neopets_page_import_task][:list_id])
|
@import_task = neopets_page.build_import_task(current_user, params[:neopets_page_import_task][:list_id])
|
||||||
|
|
||||||
|
@ -60,7 +60,7 @@ class NeopetsPageImportTasksController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def new
|
def new
|
||||||
neopets_page = NeopetsPage.new(params[:page_type], params[:expected_index], nil)
|
neopets_page = NeopetsPage.new(params[:page_type], params[:expected_index].to_i, nil)
|
||||||
@import_task = neopets_page.build_import_task(current_user, params[:list_id])
|
@import_task = neopets_page.build_import_task(current_user, params[:list_id])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -81,8 +81,10 @@ class NeopetsPage
|
||||||
|
|
||||||
|
|
||||||
class Parser
|
class Parser
|
||||||
def initialize(selectors)
|
def initialize(params)
|
||||||
@selectors = selectors
|
@selectors = params.fetch(:selectors)
|
||||||
|
@parse_id = params.fetch(:parse_id, lambda { |id| id })
|
||||||
|
@parse_index = params.fetch(:parse_index, lambda { |index| index })
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -106,13 +108,12 @@ class NeopetsPage
|
||||||
|
|
||||||
|
|
||||||
def find_id(row)
|
def find_id(row)
|
||||||
element(:item_remove, row)['name']
|
@parse_id.call(element(:item_remove, row)['name'])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def find_index(page_selector)
|
def find_index(page_selector)
|
||||||
Rails.logger.debug("index: #{element(:selected, page_selector)}")
|
@parse_index.call(element(:selected, page_selector)['value'].to_i)
|
||||||
element(:selected, page_selector)['value'].to_i
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -159,19 +160,24 @@ class NeopetsPage
|
||||||
|
|
||||||
|
|
||||||
class Type
|
class Type
|
||||||
attr_reader :name, :parser
|
attr_reader :parser
|
||||||
delegate :parse, to: :parser
|
delegate :parse, to: :parser
|
||||||
|
|
||||||
|
|
||||||
def initialize(name, url_template, parser)
|
def initialize(params)
|
||||||
@name = name
|
@get_name = params.fetch(:get_name)
|
||||||
@url_template = url_template
|
@get_url = params.fetch(:get_url)
|
||||||
@parser = parser
|
@parser = params.fetch(:parser)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
def name
|
||||||
|
@get_name.call
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def url(index)
|
def url(index)
|
||||||
@url_template % index
|
@get_url.call(index)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -179,16 +185,40 @@ class NeopetsPage
|
||||||
|
|
||||||
TYPES = {
|
TYPES = {
|
||||||
'closet' => Type.new(
|
'closet' => Type.new(
|
||||||
I18n.translate('neopets_page_import_tasks.names.closet'),
|
get_name: lambda { I18n.translate('neopets_page_import_tasks.names.closet') },
|
||||||
'http://www.neopets.com/closet.phtml?per_page=50&page=%u',
|
get_url: lambda { |index| "http://www.neopets.com/closet.phtml?per_page=50&page=#{index}" },
|
||||||
Parser.new(
|
parser: Parser.new(
|
||||||
items: "form[action=\"process_closet.phtml\"] tr[bgcolor!=silver][bgcolor!=\"#E4E4E4\"]",
|
selectors: {
|
||||||
item_thumbnail: "img",
|
items: "form[action=\"process_closet.phtml\"] tr[bgcolor!=silver][bgcolor!=\"#E4E4E4\"]",
|
||||||
item_name: "td:nth-child(2)",
|
item_thumbnail: "img",
|
||||||
item_quantity: "td:nth-child(5)",
|
item_name: "td:nth-child(2)",
|
||||||
item_remove: "input",
|
item_quantity: "td:nth-child(5)",
|
||||||
page_select: "select[name=page]",
|
item_remove: "input",
|
||||||
selected: "option[selected]"
|
page_select: "select[name=page]",
|
||||||
|
selected: "option[selected]"
|
||||||
|
}
|
||||||
|
)
|
||||||
|
),
|
||||||
|
'safety_deposit' => Type.new(
|
||||||
|
get_name: lambda { I18n.translate('neopets_page_import_tasks.names.safety_deposit') },
|
||||||
|
get_url: lambda { |index| "http://www.neopets.com/safetydeposit.phtml?offset=#{(index - 1) * 30}" },
|
||||||
|
parser: Parser.new(
|
||||||
|
selectors: {
|
||||||
|
items: "#content tr[bgcolor=\"#DFEAF7\"]",
|
||||||
|
item_thumbnail: "img",
|
||||||
|
item_name: "td:nth-child(2)",
|
||||||
|
item_quantity: "td:nth-child(5)",
|
||||||
|
item_remove: "input",
|
||||||
|
page_select: "select[name=offset]",
|
||||||
|
selected: "option[selected]"
|
||||||
|
},
|
||||||
|
parse_id: lambda { |id|
|
||||||
|
unless match = id.match(/\[([0-9]+)\]/)
|
||||||
|
raise ParseError, "Remove Item input name format was unexpected: #{id}.inspect"
|
||||||
|
end
|
||||||
|
match[1]
|
||||||
|
},
|
||||||
|
parse_index: lambda { |offset| offset / 30 + 1 }
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue