Skip to content

Commit

Permalink
Refactor for reading ease
Browse files Browse the repository at this point in the history
  • Loading branch information
kreopelle committed Jun 21, 2018
1 parent 83151b4 commit 1c05e1d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
2 changes: 1 addition & 1 deletion NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ CLI Essentials *
[X] Have a CLI for interfacing with the application
[X] Pull data from an external source
[X] Implement both list and detail views. The data provided must go at least one level deep, generally by showing the user a list of available data and then being able to drill down into a specific item
[] The application is pretty DRY
[X] The application is pretty DRY

Ruby Essentials *
[X] Basic control flow - how "if" statements work
Expand Down
1 change: 0 additions & 1 deletion lib/asana_cli_gem/asana.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class AsanaCliGem::Asana

attr_accessor :name, :sanskrit, :summary, :tip, :url
@@all = []

Expand Down
17 changes: 12 additions & 5 deletions lib/asana_cli_gem/cli.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class AsanaCliGem::CLI
attr_accessor :scraper
attr_reader :poses
attr_reader :poses, :scraper

def call
welcome
Expand All @@ -24,32 +23,40 @@ def poses

def list_poses
puts "Strengthening Yoga Poses:"
poses.each_with_index do |pose, i|
puts "#{i+1}. #{pose.name}"

poses.each_with_index do |pose, index|
puts "#{index+1}. #{pose.name}"
end
end

def menu
puts "Please enter the number of the pose you'd like to learn more about, type list to see all poses, or exit to end program."
input = gets.strip.downcase

if input != "exit"

if input.to_i > 0 && input.to_i <= poses.size

input = input.to_i - 1
selected_pose = poses[input]

scraper.asana_details(selected_pose.url) if selected_pose.summary == nil

puts "Name: #{selected_pose.name}"
puts "Sanskrit Name: #{selected_pose.sanskrit}" if selected_pose.sanskrit
puts selected_pose.summary
puts "Beginner's Tip: #{selected_pose.tip}" if selected_pose.tip
puts "To learn more, visit: #{selected_pose.url}"

elsif input == "list"
list_poses

else
puts "My apologies, I didn't recognize that input."
end

menu
end
binding.pry
end

def goodbye
Expand Down
25 changes: 12 additions & 13 deletions lib/asana_cli_gem/scraper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,28 @@ def initialize(url = nil)

def pose_collector
doc = Nokogiri::HTML(open('https://www.yogajournal.com/poses/types/strength'))
doc.css('#lyra-wrapper > div.m-page-wrapper > div.m-advertisement-off-canvas--pusher > section > div.m-page > section.m-tile-hub.m-component-stack.mm-component-stack--is-stacked')
.css('a.m-card--header').collect do |pose|

new_pose = AsanaCliGem::Asana.new
new_pose.name = pose.css('h2').text
new_pose.url = "https://www.yogajournal.com#{pose.attribute("href").value}"
doc.css('#lyra-wrapper > div.m-page-wrapper > div.m-advertisement-off-canvas--pusher > section > div.m-page > section.m-tile-hub.m-component-stack.mm-component-stack--is-stacked')
.css('a.m-card--header').collect do |pose|
new_pose = AsanaCliGem::Asana.new
new_pose.name = pose.css('h2').text
new_pose.url = "https://www.yogajournal.com#{pose.attribute("href").value}"

new_pose.save
end
new_pose.save
end
end

def asana_details(url)
pose_scraper = Nokogiri::HTML(open(url))

extra_attributes = {}
optional_attributes = {}
pose_scraper.css('div.m-detail--body h3').each do |attribute|
extra_attributes[attribute.text] = attribute.css('+p').text
optional_attributes[attribute.text] = attribute.css('+p').text
end

updated_pose = AsanaCliGem::Asana.all.detect {|pose| pose.url == url }
updated_pose.summary = pose_scraper.css('div.m-detail-header--dek').text
updated_pose.sanskrit = extra_attributes["Sanskrit Name"] if extra_attributes["Sanskrit Name"]
updated_pose.tip = extra_attributes["Beginner's Tip"] if extra_attributes["Beginner's Tip"]

updated_pose.sanskrit = optional_attributes["Sanskrit Name"] if optional_attributes["Sanskrit Name"]
updated_pose.tip = optional_attributes["Beginner's Tip"] if optional_attributes["Beginner's Tip"]
end

end

0 comments on commit 1c05e1d

Please sign in to comment.