mirror of
https://github.com/jnunemaker/httparty
synced 2023-03-27 23:23:07 -04:00
Put in first wave of parsing. Supports json and xml.
This commit is contained in:
parent
8e1437850a
commit
de9b4fb6ba
3 changed files with 86 additions and 10 deletions
|
@ -1,14 +1,17 @@
|
|||
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
||||
require File.join(dir, 'web')
|
||||
require 'pp'
|
||||
config = YAML::load(File.read(File.join(ENV['HOME'], '.twitter')))
|
||||
|
||||
class Twitter
|
||||
include Web
|
||||
|
||||
base_uri 'twitter.com'
|
||||
format :xml
|
||||
|
||||
entity :status
|
||||
entity :user
|
||||
end
|
||||
|
||||
Twitter.basic_auth config['email'], config['password']
|
||||
puts Twitter.get('/statuses/user_timeline.json')
|
||||
|
||||
# puts Twitter.post('/direct_messages/new.xml', :query => {:user => 'jnunemaker', :text => 'Hello from Web'})
|
||||
# puts Twitter.response.code
|
||||
pp Twitter.get('/statuses/user_timeline.xml', :entity => :status)
|
20
lib/web.rb
20
lib/web.rb
|
@ -1,15 +1,21 @@
|
|||
require 'net/http'
|
||||
require 'net/https'
|
||||
require 'uri'
|
||||
require 'ostruct'
|
||||
require 'rubygems'
|
||||
require 'active_support'
|
||||
|
||||
$:.unshift(File.dirname(__FILE__)) unless
|
||||
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
||||
|
||||
|
||||
dir = File.expand_path(File.join(File.dirname(__FILE__), 'web'))
|
||||
require dir + '/entities'
|
||||
|
||||
module Web
|
||||
def self.included(base)
|
||||
base.extend ClassMethods
|
||||
base.send(:include, Web::Entities)
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
|
@ -73,8 +79,15 @@ module Web
|
|||
request.body = options[:body] unless options[:body].blank?
|
||||
request.initialize_http_header headers.merge(options[:headers] || {})
|
||||
request.basic_auth(@auth[:username], @auth[:password]) if @auth
|
||||
@response = http.start() { |conn| conn.request(request) }
|
||||
@response.body
|
||||
@response = http.start() { |conn| conn.request(request) }
|
||||
|
||||
if !options[:entity] || options[:entity].blank?
|
||||
@response.body
|
||||
else
|
||||
entity = @entities.detect { |e| e.name.to_s == options[:entity].to_s }
|
||||
raise 'Entity not found' if entity.blank?
|
||||
entity.parse(@response.body)
|
||||
end
|
||||
end
|
||||
|
||||
# Makes it so uri is sure to parse stuff like google.com with the http
|
||||
|
@ -83,6 +96,3 @@ module Web
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
dir = File.expand_path(File.join(File.dirname(__FILE__), 'web'))
|
||||
require dir + '/entity'
|
63
lib/web/entities.rb
Normal file
63
lib/web/entities.rb
Normal file
|
@ -0,0 +1,63 @@
|
|||
module Web
|
||||
module Entities
|
||||
def self.included(base)
|
||||
base.extend ClassMethods
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def format(f)
|
||||
return @format unless f
|
||||
@format = f.to_s
|
||||
end
|
||||
|
||||
def entity(name, &block)
|
||||
@entities ||= []
|
||||
entity = Entity.new(name)
|
||||
yield(entity) if block_given?
|
||||
if entity.format.blank? && !@format.blank?
|
||||
entity.format = @format
|
||||
end
|
||||
@entities << entity
|
||||
end
|
||||
end
|
||||
|
||||
class Entity
|
||||
attr_accessor :name
|
||||
|
||||
def initialize(name)
|
||||
@name = name
|
||||
end
|
||||
|
||||
def format(f=nil)
|
||||
return @format if f.blank?
|
||||
self.format = f.to_s
|
||||
end
|
||||
|
||||
def format=(f)
|
||||
raise 'Unsupported format' unless %w[xml json].include?(f.to_s)
|
||||
@format = f.to_s
|
||||
end
|
||||
|
||||
def attributes(*names)
|
||||
return @attributes if names.blank?
|
||||
@attributes = names.flatten
|
||||
end
|
||||
|
||||
def has_one(*items)
|
||||
@has_ones = items.flatten
|
||||
end
|
||||
|
||||
def parse(body)
|
||||
send("from_#{format}", body)
|
||||
end
|
||||
|
||||
def from_xml(body)
|
||||
Hash.from_xml(body)
|
||||
end
|
||||
|
||||
def from_json(json)
|
||||
ActiveSupport::JSON.decode(json)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Reference in a new issue