WEBrick DocumentRoot for MP4s - ruby

I have a very simply ruby script to Serve the file system from one computer over the local host to my other PCs. I want MP4s to be watchable directly in the browser window. I've added the mime_type for mp4's, which has set the content-type to video/mp4 as required instead of the default octet-stream, however the videos are still unplayable?
require 'webrick'
mime_types = WEBrick::HTTPUtils::DefaultMimeTypes
mime_types.store 'mp4', 'video/mp4'
server = WEBrick::HTTPServer.new(:Port => 12345,
:SSLEnable => false,
:DocumentRoot => '/',
:AccessLog => [],
:MimeTypes => mime_types)
trap 'INT' do server.shutdown end
server.start
The server indicates it's OK:
But the video isn't play-able?:
How does one fix this issue? Is this a WEbrick issue?

Related

How to connect to FTP via SOCKS5 proxy with Ruby?

I'm trying to connect to FTP via SOCKS5 proxy using ruby's library Net::FTP. Documentation says to set env variable SOCKS_SERVER in order to connect through proxy (http://ruby-doc.org/stdlib-2.0.0/libdoc/net/ftp/rdoc/Net/FTP.html#method-i-connect), but it seems like it does not work.
Code I'm running is this:
irb(main):054:0> ftp = Net::FTP.new
=> #<Net::FTP:0x007efd08c73768 #mon_owner=nil, #mon_count=0, #mon_mutex=#<Thread::Mutex:0x007efd08c73718>, #binary=true, #passive=true, #debug_mode=false, #resume=false, #sock=#<Net::FTP::NullSocket:0x007efd08c736f0>, #logged_in=false, #open_timeout=nil, #read_timeout=60>
irb(main):056:0> ENV['SOCKS_SERVER'] = 'host:port'
=> "host:port"
irb(main):055:0> ftp.connect('test.rebex.net')
=> nil
irb(main):057:0> ftp.login('demo', 'password')
=> true
irb(main):058:0> ftp.ls
=> ["10-27-15 03:46PM <DIR> pub", "04-08-14 03:09PM 403 readme.txt"]
When I look to proxy logs I can not see any requests going through.
What I'm doing wrong or does anybody have an example how to achieve that?
If your on Windows computer you'll need to use dress_socks gem and Monkeypath:
$socks_server = '127.0.0.1'
$socks_port = '9090'
require 'dress_socks'
class Net::FTP
def open_socket(host, port) # :nodoc:
# puts "opening socket #{#host}:#{port}"
return DressSocks::Socket.new(#host, port,
socks_server: $socks_server, socks_port: $socks_port)
end
end

Upload file to server and delete original using NET::SCP gem in Ruby

I'm using the net/scp gem to upload a file from my machine to a remote Linux server.
require 'net/ssh'
require 'net/scp'
Net::SCP.upload!(10.125.0.0,
user,
local_path,
remote_dir,
:ssh => { :password => psw,
:key_data => keys})
This works perfectly, however I'd like to be able to 'move' the file instead of effectively copying it across.
Is there some scpparameter that will delete the original file?
Net::SCP doesn't have any such parameter:
https://github.com/net-ssh/net-scp/blob/master/lib/net/scp.rb#L259-L267
Since the only difference between a copy and a move is just a final deletion of the source, you should do this manually after upload!.
File.delete(local_path)
You most likely will want to make sure that Net::SCP has finished before doing so:
require 'net/ssh'
require 'net/scp'
channel = Net::SCP.upload!(10.125.0.0,
user,
local_path,
remote_dir,
:ssh => { :password => psw,
:key_data => keys})
channel.wait
File.delete(local_path)

Get Postgres-activerecord setup to work both locally and in Heroku

I'm automating DB creation (with a Rakefile in a Sinatra App).
I would like to be able to run the rakefile from my Linux user "pete" (eg pete#pete_laptop: /path $ rake db:create) AND from Heroku.
It comes down to the settings in my config/database.rb:
ActiveRecord::Base.establish_connection(
:adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
:host => db.host,
:port => db.port,
# pete#ubuntu_14.04_laptop--------
# :username => 'pete',
# :password => 'password',
# OR
# heroku -----------------
# :username => db.user,
# :password => db.password,
:database => DB_NAME,
:encoding => 'utf8'
)
If I use the pete#ubuntu_laptop settings, the database works in localhost but not in Heroku,
If I use the heroku settings, the database works in localhost but not in Heroku.
How can I setup this file/my ubuntu laptop so that the app works both on localhost & in Heroku?
Cheers,
Pete
You can use environment variables which can be accessed like ENV["PG_USER"] in Ruby. If you want to use it in a yml file you can put it in erb tags <%= ENV["PG_USER"] %> and render it with erb before passing it to your config.
You can set environment variables in your .bashrc or you can use something like the dotenv gem. On heroku you can set environment variables like heroku config:set PG_USER=postgres.
But check whether this is really necessary for heroku. In Rails, for instance, heroku provides a database configuration, so there is no need to configure it.
oK! With help got it working!
used:
ActiveRecord::Base.establish_connection(
:adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
:host => db.host,
:port => db.port,
:username => ENV['PG_USER'] || db.user,
:password => ENV['PG_PASSWORD'] || db.password,
:database => DB_NAME,
:encoding => 'utf8'
)
AND
added this to my shell-rc (in my case ~/.zshrc)
export PG_USER='pete'
export PG_PASSWORD='password'
now in my local environment the username & password pick up the ENV['PG...] variables from the terminal I launch the app in.
Note: 'export' is important - without it the variables don't get sent to the app's 'ENV'

How to create a config file for multiple environments in Ruby?

I don't want to confuse you so what I want to do is the following:
I have three environments:
www.env1.com
www.env2.com
www.env3.com
I want to create something to define the setup phase according the environment over which I want to run the scripts, that is:
Current set-up:
def setup
#verification_errors = []
#selenium = Selenium::Client::Driver.new(
:host => "localhost",
:port => 4444,
:browser => "*firefox C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe",
:url => "www.env1.com",
:timeout_in_second => 60
)
#selenium.start_new_browser_session
end
What I want:
def setup
#verification_errors = []
#selenium = Selenium::Client::Driver.new(
:host => "localhost",
:port => 4444,
:browser => "*firefox C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe",
**:url => This parameter configurable from a file or other source.**
:timeout_in_second => 60
)
#selenium.start_new_browser_session
end
If this is possible I can switch environments without having to re-write all test cases.
Hope you can help me out, I really need to do this.
YAML is a great data serialization language for handling configuration information. It comes with Ruby so you only have to do:
require 'yaml'
to load it in, then something like:
configuration = YAML::load_file('path/to/yamldata.yaml')
All your configuration data will be available inside the configuration variable.
Generally I create a stub for my YAML files by writing some Ruby code, defining the configuration hash that contains it, then telling YAML to generate the file for me. See the docs for load_file and dump for ways to do that.
For something like you're doing I'd create a hash like:
configuration = {
'env1' => "www.env1.com",
'env2' => "www.env2.com",
'env3' => "www.env3.com",
}
Using YAML::dump(configuration) returns:
---
env1: www.env1.com
env2: www.env2.com
env3: www.env3.com
which you'd want to write to your .yaml file, then load later at run-time and access it like:
#selenium = Selenium::Client::Driver.new(
:host => "localhost",
:port => 4444,
:browser => "*firefox C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe",
:timeout_in_second => 60
:url => configuration['env1'],
)
You can replace 'env1' with the other keys to use env2 or env3.
Rails uses YAML to make one file handle the development, test and production information for an application. At work I use it to do similar things, where one file contains our development and production environmental information for apps, plus the definitions of some hashes we need to maintain, but don't want to have to modify the code to do so.

How do I process a URL in ruby to extract the component parts (scheme, username, password, host, etc)?

I'm trying create a program using ruby (and Net::SSH) to connect to servers and perform some tasks. The details of the server are to be provided as something like:
ssh://user:pass#host:port (for a host that does not yet have SSH keys)
or
user#host
Net::SSH expects the following format:
Net::SSH.start('host', 'user', :password => "password")
Is there are gem/stdlib that can process the URL into this format? Or a simple regex that can match the different parts?
Note: I'm aware of, and use, capistrano but in this case I need lower level control.
Both URI and Addressable::URI can parse URLs and let you break them down into their components.
URI is included in Ruby's Standard Library, which is nice, but Addressable::URI has more features, and is what I use when I have to do a lot of work on URLs.
require 'addressable/uri'
uri = Addressable::URI.parse('ssh://user:pass#www.example.com:81')
uri.host # => "www.example.com"
uri.user # => "user"
uri.password # => "pass"
uri.scheme # => "ssh"
uri.port # => 81
require 'uri'
uri = URI.parse('ssh://user:pass#www.example.com:81')
uri.host # => "www.example.com"
uri.user # => "user"
uri.password # => "pass"
uri.scheme # => "ssh"
uri.port # => 81

Resources