Posted by Dan Sosedoff
on September 30, 2010
I really like public paste service – pastie.org. Too bad it does not have public API.
The most common user case for me – drop some files. 1 or 2 usually. So i have to copy and paste file contents into the form, select private and then send link to someone. Too many actions i think.
I decided to spend some time and create console tool and simplified api based on html extractions.
Source: http://github.com/sosedoff/pastie
Just install it:
$ sudo gem install pastie-api
And use it (multiple files are supported):
$ pastie file
$ pastie file1 file2 ... fileN
Also, API access:
require 'rubygems'
require 'pastie-api'
# Create a new private paste
p = Pastie.create('Test string')
# Create a new public paste
p = Pastie.create('Hello!', false)
# View paste details
puts "Paste ID: #{p.id}"
puts "Paste Key: #{p.key}"
puts "URL: #{p.link}"
puts "Raw link: #{p.raw_link}"
# Find existing paste
p = Pastie.get(1234567) # find by paste's ID
p = Pastie.get('abcdefabcdef') # find by paste's private code
Planning to add local pastes history and auto-paste from clipboard.
Posted by Dan Sosedoff
on September 22, 2010
Here is a small tooltip how to setup maintenance page for Rails applications under nginx. I usually put offline.txt into public directory while making any changes or testing.
Solution works with any type of backend server (passenger/php-fpm/upstream)
Sample vhost config:
server {
listen 80;
server_name HOSTNAME;
location / {
root /path/to/your/public/dir;
passenger_enabled on;
rails_env production;
if (-f $document_root/offline.txt) {
return 503;
}
}
location /maintenance.html {
root /path/to/your/public/dir;
}
error_page 503 /maintenance.html;
}
Pastie: http://pastie.org/private/pgnhvnkj4uxe4mkgnhvqw
Posted by Dan Sosedoff
on September 17, 2010
What do you know about rapid development?
I guess something. Almost every developer experienced moments when you or someone have really interesting idea and wanted to jump on it right away. One of the obstacles – tools you use to achieve it. And the web framework is the first of them. Why? Because if you want to create something cool without being stuck on technology level you should use things that you dont really notice while making your app. Of course it varies from person-to-person, but just imagine yourself in traffic jam driving stick-shift car, switching gears like crazy all the time.
Back to webdev, with my experience i can only say that the best tool for rapid development is Sinatra. Its so small and powerful that i got hooked on it since first day i found it. Almost all my personal/work projects on early stages have been developed with Sinatra. Another big plus – small and usefull extensions, big players like AR or DataMapper. It was extracted from Merb project, another small and powerful web framework i like to work with. Its like a one-file web application. Perfect for super-fast development. Wanna do simple music downloads page? No problem. API – again, no problem. Deployment is simple and similar to most of frameworks, as it built on top of Rack: thin, passenger (nginx/apache).
What do you know about rapid development?
I say – i love it.
Posted by Dan Sosedoff
on September 06, 2010
Sphinx is a really powerful tool for a full-text database search. It is the perfect option as a search engine on your website’s data.
In default mode it works as a regular tcp server and has multiple native language bindings for php, ruby, c, etc. But its another outstanding feature is MySQL Protocol Connectoin and SphinxQL, which is similar to native mysql query language.
So, ok. Lets say we have N documents with M attributes. Attributes could be different: string, integer, double, boolean. Out objective is to perform attribute aggregation based on specified search term (user-defined, etc). That will give us full information on data selected only by search term. Its only use-case when you really need to get these aggregate fields. Next part is tricky and not really efficient.
First of all, you have to setup Sphinx search daemon instance using different configuration file (it could not run both). Another problem – you have to setup another data sources and index files, Sphinx puts a lock on all used-right-now files.
Lets assume we have a database of books. We need to build a form with sliders which could be used as user-friendly search filter. All we need is to get a list of min and max attributes values. But there is a problem: sometimes, while working with sphinx you might find yourself trying to use it like you usually do with regular RDMS. Unfortunately, sphinx has a different design. Basically, sphinx has one primary field which presents in each search request – DocumentID. Its an unique id that represents your data ID, which makes it harder to product aggregate data. And there is no way to get rid of that field.
The whole idea of our aggregation – using boolean match mode with no weighting performed at all. In that case all results will have weight field = 1. That will give us ability to group all the results by weight field, rejecting the DocumentID field.
Here is the sample query:
SELECT
MIN(reviews) AS min_reviews, MAX(reviews) AS max_reviews,
MIN(pages) AS min_pages, MAX(pages) AS max_pages,
MIN(pub_year) AS min_date, MAX(pub_year) AS max_date,
@weight AS w
FROM
INDEX_NAME
WHERE
MATCH('SEARCH_TERM') AND pages > 30
GROUP BY w OPTION ranker = none
The result of this query will be one row with field alias names. Thats’s it.
All statements are fully customizable. Just check full SphinxQL reference for details.