Skip to content

Instantly share code, notes, and snippets.

@chebyte
Created October 30, 2012 19:50
Show Gist options
  • Save chebyte/3982580 to your computer and use it in GitHub Desktop.
Save chebyte/3982580 to your computer and use it in GitHub Desktop.
###API CODE###
$:.push(File.dirname(__FILE__) + "../lib")
require "rack/api"
require 'json'
class ProxyResponse
def initialize(app)
@app = app
end
def call(env)
status, headers, response = @app.call(env)
response = [JSON.load(response.first)]
[status, headers, response]
end
end
class ProxyApi < Rack::API
prefix "api"
use ProxyResponse
ActiveRecord::Base.include_root_in_json = false
helper do
end
# basic_auth do |user, pass|
# user == "admin" && pass == "test"
# end
version :v1 do
get "proxies/list" do
Proxy.available.all.to_json(:only => [:ip, :port, :updated_at])
end
get "proxies/for_buy" do
Proxy.for_buy
end
get "proxies/for_search" do
Proxy.available.all.to_json(:only => [:ip, :port, :updated_at])
end
post "proxies/import" do
proxy = {:ip => params[:ip], :port => params[:port], :source => params[:source],
:type_proxy => params[:type], :priority => params[:priority]}
Resque.enqueue(Proxies::Import, proxy)
end
post "proxies/delete" do
proxy = {:ip => params[:ip], :port => params[:port], :type => "false_delete"}
Resque.enqueue(Proxies::Delete, proxy)
end
end
end
###CLIENT CODE###
module Argot
class ClientApi
def import(proxy)
access.post import_url, proxy
end
def delete(proxy)
access.post delete_url, proxy
end
def list
access.get list_url
end
def list_url
"/api/v1/proxies/list"
end
def import_url
"/api/v1/proxies/import"
end
def delete_url
"/api/v1/proxies/delete"
end
def access
@conn ||= Faraday.new(:url => base_url) do |faraday|
faraday.request :url_encoded
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
end
end
def base_url
"http://neuroproxy.cloudfoundry.com"
end
end
end
### BACKTRACE ###
irb(main):019:0> c = Argot::Client.new
=> #<Argot::Client:0x007f8bff366dd8>
irb(main):020:0> c.for_buy.body
=> "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">\n<HTML>\n <HEAD><TITLE>Internal Server Error</TITLE></HEAD>\n <BODY>\n <H1>Internal Server Error</H1>\n undefined method `bytesize' for #&lt;Hash:0x007fbcb37cb510&gt;\n <HR>\n <ADDRESS>\n WEBrick/1.3.1 (Ruby/1.9.2/2011-07-09) at\n 127.0.0.1:3000\n </ADDRESS>\n </BODY>\n</HTML>\n"
irb(main):021:0>
Started GET "/api/v1/proxies/for_buy" for 127.0.0.1 at 2012-10-30 16:49:18 -0300
Proxy Load (0.3ms) SELECT `proxies`.* FROM `proxies` ORDER BY proxies.id DESC LIMIT 1
[2012-10-30 16:49:18] ERROR NoMethodError: undefined method `bytesize' for #<Hash:0x007fbcb37cb510>
/Users/chebyte/Developer/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/rack-1.2.5/lib/rack/utils.rb:276:in `bytesize'
/Users/chebyte/Developer/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/rack-1.2.5/lib/rack/content_length.rb:22:in `block in call'
/Users/chebyte/Developer/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/rack-1.2.5/lib/rack/content_length.rb:22:in `each'
/Users/chebyte/Developer/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/rack-1.2.5/lib/rack/content_length.rb:22:in `inject'
/Users/chebyte/Developer/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/rack-1.2.5/lib/rack/content_length.rb:22:in `call'
/Users/chebyte/Developer/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/rack-1.2.5/lib/rack/handler/webrick.rb:52:in `service'
/Users/chebyte/Developer/.rbenv/versions/1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/Users/chebyte/Developer/.rbenv/versions/1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/Users/chebyte/Developer/.rbenv/versions/1.9.2-p290/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment