Skip to content

Instantly share code, notes, and snippets.

@regedarek
Created February 15, 2012 17:56
Show Gist options
  • Save regedarek/1837794 to your computer and use it in GitHub Desktop.
Save regedarek/1837794 to your computer and use it in GitHub Desktop.
require 'faraday'
require 'forwardable'
module Soup
class Agent
extend Forwardable
def initialize(domain = 'https://www.soup.io/')
@agent ||= faraday(domain)
end
def_delegators :@agent, :get, :post, :response_headers, :body, :status
def faraday(domain)
Faraday.new(url: domain) do |builder|
builder.use Faraday::Request::UrlEncoded
builder.use Faraday::Response::Logger
builder.use Faraday::Adapter::NetHttp
end
end
end
end
require 'cgi'
module Soup
class Client
attr_accessor :login, :password, :domain, :agent, :session_id, :soup_user_id
def initialize(login, password)
@login = login
@password = password
@agent = Soup::Agent.new
end
def get_session_id(page)
cookie = page.headers["set-cookie"]
cookie = CGI::Cookie.parse(cookie)
@session_id = cookie["soup_session_id"].first
@soup_user_id = cookie["soup_user_id"].first
end
def redirect_session
agent.faraday("http://www.soup.io").get do |req|
req.url "/remote/generate"
req.params['referer'] = "http://www.soup.io/everyone"
req.params['host'] = "#{@login}.soup.io"
req.params['redirect_to'] = "/"
req.headers["Cookie"] = "soup_session_id=#{@session_id}"
end.headers['location']
end
def login
post = { login: @login, password: @password, commit: 'Log in' }
get_session_id(@agent.post('/login', post))
get_session_id(@agent.get(redirect_session))
end
def get_default_request
{
'post[source]' => '',
'post[body]' => '',
'post[id]' => '',
'post[parent_id]' => '',
'post[original_id]' => '',
'post[edited_after_repost]' => '',
'redirect' => '',
'commit' => 'Save'
}
end
def post_submit(request)
agent = @agent.faraday("http://#{@login}.soup.io")
agent.post('/save', request) do |req|
req.headers["Cookie"] = "soup_session_id=#{session_id}"
end
end
def new_link(url, title = '', description = '')
request = get_default_request()
request['post[type]'] = 'PostLink'
request['post[source]'] = url
request['post[title]'] = title
request['post[body]'] = description
post_submit(request)
end
def new_image(url, description = '')
request = get_default_request()
request['post[type]'] = 'PostImage'
request['post[url]'] = url
request['post[source]'] = url
request['post[body]'] = description
post_submit(request)
end
def new_text(text, title = '')
request = get_default_request()
request['post[type]'] = 'PostRegular'
request['post[title]'] = title
request['post[body]'] = text
post_submit(request)
end
def new_quote(quote, source)
request = get_default_request()
request['post[type]'] = 'PostQuote'
request['post[body]'] = quote
request['post[title]'] = source
post_submit(request)
end
def new_video(youtube_url, description = '')
request = get_default_request()
request['post[type]'] = 'PostVideo'
request['post[embedcode_or_url]'] = youtube_url
request['post[body]'] = description
post_submit(request)
end
end
end
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "Soup Agent" do
before do
@client = Soup::Client.new("login", "password")
end
it "should connect to soup.io" do
@agent.host.should == "www.soup.io"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment