Class: Gotasku::Problem
- Inherits:
-
Object
- Object
- Gotasku::Problem
- Defined in:
- lib/gotasku/problem.rb
Constant Summary collapse
- @@uri =
"http://www.goproblems.com/"
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
-
.last_problem_id ⇒ Object
get the id of the last problem on goproblems.com.
Instance Method Summary collapse
- #blank? ⇒ Boolean
- #data ⇒ Object
-
#difficulty ⇒ Object
strip problem difficulty from goproblems.com.
-
#display ⇒ Object
print out information on problem on screen.
-
#initialize(options = {}) ⇒ Problem
constructor
initializes with a hash of options.
-
#rating ⇒ Object
strip problem rating from goproblems.com.
-
#save ⇒ Object
saves the sgf.
-
#sgf ⇒ Object
access sgf or strip problem sgf from goproblems.com.
-
#tree ⇒ Object
access or get the tree from the Parser.
-
#type ⇒ Object
strip problem type from goproblems.com.
-
#upload ⇒ Object
this method is not finished uploads sgf to goproblems.com.
Constructor Details
#initialize(options = {}) ⇒ Problem
initializes with a hash of options
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/gotasku/problem.rb', line 8 def initialize( = {}) @id = ["id"] @sgf = ["sgf"] if @sgf # checks if sgf is a sgf file, and reads it if it is if @sgf =~ /\.sgf/ && File.exists?(@sgf) @sgf = File.open(@sgf, 'r').read end @data ||= {} end # overrides sgf and id if they are provided in the option data if ["data"] @data = ["data"] @id = @data["id"] || @id @sgf = @data["sgf"] || @sgf end end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
5 6 7 |
# File 'lib/gotasku/problem.rb', line 5 def id @id end |
Class Method Details
.last_problem_id ⇒ Object
get the id of the last problem on goproblems.com
30 31 32 33 34 35 36 |
# File 'lib/gotasku/problem.rb', line 30 def self.last_problem_id @@last_problem_id ||= begin doc = Nokogiri::HTML(open('http://www.goproblems.com/').read) link = doc.css('td a').select {|a| a[:href] =~ /^\/\d+\z/}[0] link[:href].slice(/\d+/).to_i end end |
Instance Method Details
#blank? ⇒ Boolean
111 112 113 |
# File 'lib/gotasku/problem.rb', line 111 def blank? sgf == '(;)' end |
#data ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/gotasku/problem.rb', line 115 def data @data ||= begin # get sgf from go problems doc = Nokogiri::HTML(open("#{@@uri}#{@id}").read) sgf_text = doc.css("div#player-container").text.gsub(/\r?\n/, '') raise Gotasku::NotFound if sgf_text.empty? difficulty = Gotasku::DifficultyString.new( doc.css("div.difficulty a").text) type = doc.css("div.prob_genre").text = Gotasku::RatingString.new( doc.css("div.probstars")[0][:class]).convert { "id" => @id, "sgf" => sgf_text, "difficulty" => difficulty, "diff_num" => difficulty.convert, "type" => type, "rating" => } rescue Gotasku::NotFound # puts "This problem cannot be found" # at the moment, this seems the best solution, not ideal though # because it allows for problems to be saved even if they are not # found {sgf: '(;)'} rescue SocketError puts "Poor internet connection" {sgf: '(;)'} end end |
#difficulty ⇒ Object
strip problem difficulty from goproblems.com
49 50 51 |
# File 'lib/gotasku/problem.rb', line 49 def difficulty data["difficulty"] end |
#display ⇒ Object
print out information on problem on screen
39 40 41 |
# File 'lib/gotasku/problem.rb', line 39 def display Gotasku::Display.show(self) end |
#rating ⇒ Object
strip problem rating from goproblems.com
59 60 61 |
# File 'lib/gotasku/problem.rb', line 59 def data["rating"] end |
#save ⇒ Object
saves the sgf
69 70 71 |
# File 'lib/gotasku/problem.rb', line 69 def save tree.save("#{id || Time.now.to_i}.sgf") end |
#sgf ⇒ Object
access sgf or strip problem sgf from goproblems.com
44 45 46 |
# File 'lib/gotasku/problem.rb', line 44 def sgf @sgf ||= data["sgf"] end |
#tree ⇒ Object
access or get the tree from the Parser
64 65 66 |
# File 'lib/gotasku/problem.rb', line 64 def tree @tree ||= Gotasku::Parser.new.parse(sgf) end |
#type ⇒ Object
strip problem type from goproblems.com
54 55 56 |
# File 'lib/gotasku/problem.rb', line 54 def type data["type"] end |
#upload ⇒ Object
this method is not finished uploads sgf to goproblems.com
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/gotasku/problem.rb', line 75 def upload # don't upload a problem that came from goproblems unless @id sess = Gotasku::Session.current add_info_page = sess.link_with(href: /add/).click add_page = add_info_page.link_with(text: /direct/).click add_page.form_with( action: 'addtest.php3') do |f| source_field = f.field_with(name: "source") source_field.value = '' name_field = f.field_with(name: "name") name_field.value = '' sgf_field = f.field_with(name: "sgf") sgf_field.value = @sgf intro_field = f.field_with(name: "intro") intro_field.value = '' type_select = f.field_with(name: "genre") type_select.value = 'elementary' group_select = f.field_with(name: "usergroup[]") group_select.value = ['none'] @submit = f.submit end # after submit, goes to flash app to confirm and complete # the problem end end |