Skip to content

Instantly share code, notes, and snippets.

@agnellvj
agnellvj / api.markdown
Created September 12, 2011 20:16 — forked from jcasimir/api.markdown
Exposing an API in Rails 3

Exposing an API

APIs are becoming an essential feature of modern web applications. Rails does a good job of helping your application provide an API using the same MVC structure you're accustomed to.

In the Controller

Let's work with the following example controller:

class ArticlesController < ApplicationController
@agnellvj
agnellvj / tire_http_clients_benchmark.rb
Created September 9, 2011 01:13 — forked from karmi/tire_http_clients_benchmark.rb
Benchmark Tire gem with RestClient and Curb HTTP Clients
#
# A basic, synthetic benchmark of the impact HTTP client has
# on the speed of talking to ElasticSearch in the Tire gem.
#
# In general, Curb seems to be more then two times faster the RestClient, in some cases it's three
# to five times faster. I wonder if keep-alive has anything to do with it, but it probably does.
#
# Run me with:
# $ git clone git://github.com/karmi/tire.git
# $ cd tire
data = ["1", "11", "111", "2", "22", "222", "3", "4", "5"]
y = (1..9).inject({}) { |memo, data| memo[data.to_s] = 0; memo }
data.each { |z| y[z.chr] = y.fetch(z.chr, 0) + 1 }
puts y
y = {};(1..9).map { |x| y[x.to_s] = 0 }
data.each { |z| y[z.chr] = y.fetch(z.chr, 0) + 1 }
puts y
curl localhost:9200/_search -d '{
"query" : {
"filtered" : {
"query" : {"match_all" : {}},
"filter" : {
"and" : [
{
"range" : {
"file_size" : {"from" : 10, "to" : 10000}
}
$ git clone github:lenary/guides.git
Cloning into guides...
remote: Counting objects: 255, done.
remote: Compressing objects: 100% (216/216), done.
remote: Total 255 (delta 111), reused 163 (delta 35)
Receiving objects: 100% (255/255), 1.49 MiB | 564 KiB/s, done.
Resolving deltas: 100% (111/111), done.
$ cd guides
$ git remote -v
@agnellvj
agnellvj / Gemfile
Created January 11, 2011 16:06
load specific gems for specific platforms.
source :gemcutter
eval File.read(File.expand_path("../config/bundler/general.gemfile", __FILE__))
unless RUBY_PLATFORM[/mswin/] || RUBY_PLATFORM[/mingw/]
eval File.read(File.expand_path("../config/bundler/unix.gemfile", __FILE__))
end
$(document).ready(function(){
$('.other_submit').click(function(){
this.form.action = this.getAttribute('data-action')
})
})
@agnellvj
agnellvj / a.rb
Created May 20, 2014 19:04 — forked from ahoward/a.rb
# fork it
#
# make it print all true with only ONE LINE OF CODE
class A
def A.foo
@foo ||= (
if self == A
'42.0'
else