Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor balance retriever #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ def initialize(config)

def retrieve
PayWithMe::Models::Balance.new do |b|
if error = nokogiri_doc.at_xpath('//input[@name="ERROR"]/@value')
b.error! error.value
else
nokogiri_doc.xpath('//input').each do |input|
account = input.attributes['name'].value
balance = input.attributes['value'].value.to_f
b.account account, with_balance: balance
with_nokogiri_doc(response) do |nokogiri_doc|
if (error = nokogiri_doc.at_xpath('//input[@name="ERROR"]/@value'))
b.error! error.value
else
nokogiri_doc.xpath('//input').each do |input|
account = input.attributes['name'].value
balance = input.attributes['value'].value.to_f
b.account account, with_balance: balance
end
end
end
end
Expand All @@ -26,11 +28,7 @@ def retrieve
private

def response
return @response if defined? @response

uri = URI(ENDPOINT)
uri.query = URI.encode_www_form(params)
@response = Net::HTTP.get_response(uri)
Net::HTTP.get_response(URI(ENDPOINT).tap { |uri| uri.query = URI.encode_www_form(params) })
end

def params
Expand All @@ -40,11 +38,11 @@ def params
}
end

def nokogiri_doc
@nokogiri_doc ||= Nokogiri::HTML(response.body)
def with_nokogiri_doc(response)
yield Nokogiri::HTML(response.body)
end
end
end
end
end
end
end