And the connection to the database in lib / config / environment.rb :
- adapter: sqlite3
- database: lib / db / development.sqlite3
require 'rubygems' require 'active_record' require 'yaml' # dbconfig = YAML :: load ( File . open ( File . join ( File . dirname ( __FILE__ ) , 'database.yml' ) ) ) # () ActiveRecord::Base . logger = Logger . new ( STDERR ) # Simple logging utility. logger.rb -- standart lib # ActiveRecord::Base . establish_connection ( dbconfig )
require 'rubygems' require 'active_record' require 'yaml' # dbconfig = YAML :: load ( File . open ( File . join ( File . dirname ( __FILE__ ) , 'database.yml' ) ) ) # () ActiveRecord::Base . logger = Logger . new ( STDERR ) # Simple logging utility. logger.rb -- standart lib # ActiveRecord::Base . establish_connection ( dbconfig )
require 'rubygems' require 'active_record' require 'yaml' # dbconfig = YAML :: load ( File . open ( File . join ( File . dirname ( __FILE__ ) , 'database.yml' ) ) ) # () ActiveRecord::Base . logger = Logger . new ( STDERR ) # Simple logging utility. logger.rb -- standart lib # ActiveRecord::Base . establish_connection ( dbconfig )
require 'rubygems' require 'active_record' require 'yaml' # dbconfig = YAML :: load ( File . open ( File . join ( File . dirname ( __FILE__ ) , 'database.yml' ) ) ) # () ActiveRecord::Base . logger = Logger . new ( STDERR ) # Simple logging utility. logger.rb -- standart lib # ActiveRecord::Base . establish_connection ( dbconfig )
require 'rubygems' require 'active_record' require 'yaml' # dbconfig = YAML :: load ( File . open ( File . join ( File . dirname ( __FILE__ ) , 'database.yml' ) ) ) # () ActiveRecord::Base . logger = Logger . new ( STDERR ) # Simple logging utility. logger.rb -- standart lib # ActiveRecord::Base . establish_connection ( dbconfig )
require 'rubygems' require 'active_record' require 'yaml' # dbconfig = YAML :: load ( File . open ( File . join ( File . dirname ( __FILE__ ) , 'database.yml' ) ) ) # () ActiveRecord::Base . logger = Logger . new ( STDERR ) # Simple logging utility. logger.rb -- standart lib # ActiveRecord::Base . establish_connection ( dbconfig )
require 'rubygems' require 'active_record' require 'yaml' # dbconfig = YAML :: load ( File . open ( File . join ( File . dirname ( __FILE__ ) , 'database.yml' ) ) ) # () ActiveRecord::Base . logger = Logger . new ( STDERR ) # Simple logging utility. logger.rb -- standart lib # ActiveRecord::Base . establish_connection ( dbconfig )
require 'rubygems' require 'active_record' require 'yaml' # dbconfig = YAML :: load ( File . open ( File . join ( File . dirname ( __FILE__ ) , 'database.yml' ) ) ) # () ActiveRecord::Base . logger = Logger . new ( STDERR ) # Simple logging utility. logger.rb -- standart lib # ActiveRecord::Base . establish_connection ( dbconfig )
require 'rubygems' require 'active_record' require 'yaml' # dbconfig = YAML :: load ( File . open ( File . join ( File . dirname ( __FILE__ ) , 'database.yml' ) ) ) # () ActiveRecord::Base . logger = Logger . new ( STDERR ) # Simple logging utility. logger.rb -- standart lib # ActiveRecord::Base . establish_connection ( dbconfig )
To perform the migration, create a rake file.
- class CreateMoonRiseTimes < ActiveRecord :: Migration
- def self . up
- create_table : moon_rise_times do | t |
- t. string : day
- t. string : time
- end
- end
- def self . down
- drop_table : moon_rise_times
- end
- end
- require 'lib / config / environment.rb'
- Documentation on rake docs.rubyrake.org
- # namespace - rake.rubyforge.org/classes/Rake/NameSpace.html
- namespace : db do
- desc "Migrate the database"
- task : migrate do
- # performing all migrations from lib / db / migrate,
- # method takes parameters: migrate (migrations_path, target_version = nil)
- # in our case
- # migrations_path = lib / db / migrate
- # target_version = ENV ["VERSION"]? ENV ["VERSION"]. To_i: nil
- # migration starts as rake db: migrate VERSION = version_number
- ActiveRecord :: Migrator . migrate ( 'lib / db / migrate' , ENV [ "VERSION" ] ? ENV [ "VERSION" ] . to_i : nil )
- end
- end
- $ rake db: migrate version = 1
- ( in / home / data / projects / ActiveRecord-without-Rails )
- == CreateMoonRiseTimes: migrating ============================================
- - create_table ( : moon_rise_times )
- SQL ( 1.0ms ) CREATE TABLE "moon_rise_times" ( "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "day" varchar ( 255 ) , "time" varchar ( 255 ) )
- - > 0.0019s
- == CreateMoonRiseTimes: migrated ( 0.0020s ) ===================================
- SQL ( 0.1ms ) INSERT INTO schema_migrations ( version ) VALUES ( '1' )
- # A file with a small script for testing the work of nokogiri.
- $ KCODE = "u"
- require 'jcode'
- require 'rubygems'
- require 'open-uri'
- require 'iconv'
- require 'nokogiri'
- # Download html document
- source = open ( "pogoda.yandex.ru/26063/details/" )
- # Create a nokogiri object from the loaded document
- data = Nokogiri :: HTML ( source )
- # Extract all html table rows using the search method and simple xpath expression
- # wiki.github.com/tenderlove/nokogiri
- = begin
- Fragment of the source html-file, in which you need to find the date of the rising of the moon :)
- <th class = "date" rowspan = "4"> <b title = ""> 9 </ b> <span> July </ span> </ th>
- = end
- data_html = data. search ( "th [@class = date] // b" ) . first . inner_html + "" + data. search ( "th [@class = date] // span" ) . first . inner_html
- puts data_html
- = begin
- A fragment of the source html file, in which you need to find the time of the rising of the moon on the day defined above :)
- <td class = "dawn-dark" rowspan = "4">
- <dl>
- <dt> Sunrise </ dt>
- <dd> 04:52 </ dd>
- <dt> Sunset </ dt>
- <dd> 23:15 </ dd>
- </ dl>
- <img src = "// i.yandex.st/weather/i/moon/07.gif" alt = "The Waning Moon" title = "The Waning Moon">
- </ td>
- = end
- time_html = data. search ( "td [@class = dawn-dark] // dl // dd" ) . first . inner_html
- puts time_html
- $ ruby lib / test_search.rb
- July 10
- 04: 53
- $ KCODE = "u"
- require 'jcode'
- require 'rubygems'
- require 'active_record'
- require 'yaml'
- require 'logger'
- require 'open-uri'
- require 'iconv'
- require 'nokogiri'
- # load the environment.rb file settings and connect to the database
- require file . join ( File . dirname ( __FILE__ ) , 'config / environment.rb' )
- # Create a MoonRiseTime class that wraps the moon_rise_times table
- # Table cells: day - data type, rise_times - time type
- #
- class MoonRiseTime < ActiveRecord :: Base
- end
- MoonRiseTime. create do | moon_rise_time |
- # Download html document
- source = open ( "pogoda.yandex.ru/26063/details/" )
- # Create a nokogiri object from the loaded document
- data = Nokogiri :: HTML ( source )
- # Extract all html table rows using the search method and simple xpath expression
- # nokogiri.org/Nokogiri/XML/Node.html#method-i-inner_html
- #: TODO: Cast data types to data and time
- # Find the original html date
- data_moon_rise = data. search ( "th [@class = date] // b" ) . first . inner_html
- month_moon_rise = data. search ( "th [@class = date] // span" ) . first . inner_html
- # pass the data found to the methods of the database wrapper class
- moon_rise_time. day = data_moon_rise + "" + month_moon_rise
- moon_rise_time. time = data. search ( "td [@class = dawn-dark] // dl // dd" ) . first . inner_html
- end
- ruby lib / main.rb
- MoonRiseTime Create ( 0.4ms ) INSERT INTO "moon_rise_times" ( "time" , "day" ) VALUES ( '04: 53 ' , '10 July' )
- # File with a small script for testing
- # extracting data from the moon_rise_times table.
- # load the configuration file and connect to the database in the file environment.rb
- require file . join ( File . dirname ( __FILE__ ) , 'config / environment.rb' )
- # Create a MoonRiseTime class that wraps the moon_rise_times table
- # Table cells: day, time - type string
- class MoonRiseTime < ActiveRecord :: Base
- end
- # Extract all records from the database in fetch_result
- fetch_result = MoonRiseTime. all
- # Displaying fetch_result
- fetch_result. each do | result_item |
- puts result_item. id
- puts result_item. time
- puts result_item. day
- end
- $ ruby lib / test_fetch_data.rb
- MoonRiseTime Load ( 0.5ms ) SELECT * FROM "moon_rise_times"
- one
- 04: 53
- July 10
Source: https://habr.com/ru/post/98751/