Ruby: configure_with method - ruby

I occasionally see configure_with in Ruby code but can't find any documentation for this method despite several Google searches.
For an example of configure_with see
Where to place/access config file in gem?
Any suggestions?

Here are some references:
http://rubydoc.info/github/sferik/rails_admin/RailsAdmin/Config.configure_with
http://www.ruby-doc.org/gems/docs/r/rack_gyazo-0.1.2/Rack/Gyazo.html#method-c-configure_with
http://ruby-doc.org/gems/docs/h/heroku_mongo_watcher-0.3.0/HerokuMongoWatcher/Configuration.html
Since you didn't provide nearly enough information to narrow it down, it falls on you.

Related

How to write a filter with the MediaWiki AbuseFilter extension

Hello there,
just a "quick" question - I already installed the mediawiki properly - same with the extension itself, all working properly.
The thing is that Mediawiki extension page (https://www.mediawiki.org/wiki/Extension:AbuseFilter) won't tell me much about HOW to write a code for a filter, and google searches didn't return any valuable data like code block examples.
I'd be overjoyed if somebody could provide me a working code for the filter, even as simple as one for replacing typical f-bomb for the word "flowers", or whatever, since strReplace does nothing on it's own and I have no idea how to handle things.
Thanks in advance for any suggestions. :)
The official manual is here. For real-life examples, just go to Special:AbuseFilter on a wiki that's using it and see the code of public filters. For example, on English Wikipedia.

Ruby gem for retrieving details/information on a torrent via info hash

Is there a ruby gem that I can use with Ruby or Ruby on Rails that accepts an info hash and returns information on the torrent? Like seeders, leachers, size, etc.?
If not is there any other way I can get this information using Ruby? Is there an API that I can easily digest?
Thanks in advance.
Take a look at the thepiratebay.
Although, it seems like it's not maintained actively anymore. But, should solve your problem.
You can find a torrent:
ThePirateBay::Torrent.find("123123123")
Also, you can get all the seeders, leechers and size:
ThePirateBay::SortBy::Size # Size, largest first
ThePirateBay::SortBy::Seeders # Most seeders first
ThePirateBay::SortBy::Leechers # Most leechers first
So, why not giving it a try?
It really depends what torrents you are talking about. Different torrent trackers have different APIs.
You might want to dig into specific tracker API (please be mindful these ones are not Ruby APIs):
https://getstrike.net/api/
https://www.npmjs.com/package/thepiratebay

What is the difference between Ruby's 'open-uri' and 'Net:HTTP' gems?

It seems like both of these gems perform very similar tasks. Can anyone give examples of where one gem would be more useful than the other? I don't have specific code that I'm referring to, I'm more wondering about general use cases for each gem. I know this is a short question, I will fill in the blanks upon request. Thanks.
The reason they look like they perform similar tasks is OpenURI is a wrapper for Net::HTTP, Net::HTTPS, and Net::FTP.
Usually, unless you feel you need a lower level interface, using OpenURI is better as you can get by with less code. Using OpenURI you can open a URL/URI and treat it as a file.
See: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/open-uri/rdoc/OpenURI.html
and http://ruby-doc.org/stdlib-1.9.3//libdoc/net/http/rdoc/Net.html
I just found out that open does follow redirections, while Net::HTTP doesn't, which is an important difference.
For example, open('http://www.stackoverflow.com') { |content| puts content.read } will display the proper HTML after following the redirection, while Net::HTTP.get(URI('http://www.stackoverflow.com')) will show the redirection message and 302 status code.

How to detect if an element exists in Watir

I'm relatively new to Watir but can find no good documentation (examples) regarding how to check if an element exists. There are the API specs, of course, but these make precious little sense to me if I don't find an example.
I've tried both combinations but nothing seems to work...
if browser.image (:src "/media/images/icons/reviewertools/editreview.jpg").exists
then...
if browser.image (:src "/media/images/icons/reviewertools/editreview.jpg").exists?
then...
If anyone has a concrete suggestion as per how to implement this, please help! Thanks!
It seems you are missing a comma between parameters.
Should be
if browser.image(:src, "/media/images/icons/reviewertools/editreview.jpg").exists?
Also you can find this page useful in future to know what attributes are supported.
The code you posted should work just fine.
Edit: Oops, wrong. As Katmoon pointed out, there is a missing comma.
browser.image(:src "/media/images/icons/reviewertools/editreview.jpg").exists?
One problem you may get caught up in is if the browser variable you specified is actually an element that doesn't exist.
e.g.
b = Watir::IE.start(ipAddress)
b.frame(:name, "doesntExist).image(:src "/media/images/icons/reviewertools/editreview.jpg").exists?
The above code will throw a Watir::UnknownFrameException. You can get around this by first verifying the frame exists or by surrounding the code in a begin/rescue block.
Seems like you are using it correctly. Here is an old RDoc of Watir.
Does it not work because Watir cannot find it? Hard to tell because there is no source or link to the page that is being tested. I think that I only use image.exists?. In general, errors that come from when the image exists but is not found are:
The how is not compatible with the element type. There is a cheatsheet to help you see which object types can be found with different attributes here.
The what is not correct. You may have to play with that a little bit. Consider trying a regex string to match it such as browser.image(:src, /editreview.jpg/). As a last resort, maybe use element_by_xpath, but there are maintenance costs with that.
The location is not correct. Maybe the element is in a frame or something like that. browser.frame("detail").image(:src, /editreview.jpg/).
Try those, but please let me know what worked. One more thing, what are you checking for? If it's part of the test criteria, you can handle it that way. If you need to click on it, then forget the .exists? and just click on it. Ruby will let you know if it's not there. If you need it to be grace, learn about begin/rescue.
Good luck,
Dave

Using Rack::Session::Datamapper

mkristgan's rack_datamapper gem says that it "can be wrapped to be used in a specific environement, i.e. Rack::Session::Datamapper".
Unfortunately, I don't know quite enough about Ruby to accomplish this task yet –Modules/Classes in Ruby are still above my head (coming from PHP).
Can anyone offer assistance with using rack_datamapper to implement Rack::Session::Datamapper?
You probably don't want to do this anyway.
The answer below is great, but upon closer consideration, I realized I shouldn't do it anyway. Instead, I'm placing the user_id, ip and first name (for convenience) in a cookie and protecting it.
This gem should help:
In Sinatra just add:
use Rack::Session::Moneta,
store: Moneta.new(:DataMapper, setup: (ENV['DATABASE_URL'] || "sqlite://#{Dir.pwd}/development.db"))
and use session[] object at will.

Resources