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'))
|
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
||||||
require File.join(dir, 'web')
|
require File.join(dir, 'web')
|
||||||
|
require 'pp'
|
||||||
config = YAML::load(File.read(File.join(ENV['HOME'], '.twitter')))
|
config = YAML::load(File.read(File.join(ENV['HOME'], '.twitter')))
|
||||||
|
|
||||||
class Twitter
|
class Twitter
|
||||||
include Web
|
include Web
|
||||||
|
|
||||||
base_uri 'twitter.com'
|
base_uri 'twitter.com'
|
||||||
|
format :xml
|
||||||
|
|
||||||
|
entity :status
|
||||||
|
entity :user
|
||||||
end
|
end
|
||||||
|
|
||||||
Twitter.basic_auth config['email'], config['password']
|
Twitter.basic_auth config['email'], config['password']
|
||||||
puts Twitter.get('/statuses/user_timeline.json')
|
pp Twitter.get('/statuses/user_timeline.xml', :entity => :status)
|
||||||
|
|
||||||
# puts Twitter.post('/direct_messages/new.xml', :query => {:user => 'jnunemaker', :text => 'Hello from Web'})
|
|
||||||
# puts Twitter.response.code
|
|
22
lib/web.rb
22
lib/web.rb
|
@ -1,15 +1,21 @@
|
||||||
require 'net/http'
|
require 'net/http'
|
||||||
require 'net/https'
|
require 'net/https'
|
||||||
require 'uri'
|
require 'uri'
|
||||||
|
require 'ostruct'
|
||||||
require 'rubygems'
|
require 'rubygems'
|
||||||
require 'active_support'
|
require 'active_support'
|
||||||
|
|
||||||
$:.unshift(File.dirname(__FILE__)) unless
|
$:.unshift(File.dirname(__FILE__)) unless
|
||||||
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
$:.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
|
module Web
|
||||||
def self.included(base)
|
def self.included(base)
|
||||||
base.extend ClassMethods
|
base.extend ClassMethods
|
||||||
|
base.send(:include, Web::Entities)
|
||||||
end
|
end
|
||||||
|
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
|
@ -73,8 +79,15 @@ module Web
|
||||||
request.body = options[:body] unless options[:body].blank?
|
request.body = options[:body] unless options[:body].blank?
|
||||||
request.initialize_http_header headers.merge(options[:headers] || {})
|
request.initialize_http_header headers.merge(options[:headers] || {})
|
||||||
request.basic_auth(@auth[:username], @auth[:password]) if @auth
|
request.basic_auth(@auth[:username], @auth[:password]) if @auth
|
||||||
@response = http.start() { |conn| conn.request(request) }
|
@response = http.start() { |conn| conn.request(request) }
|
||||||
@response.body
|
|
||||||
|
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
|
end
|
||||||
|
|
||||||
# Makes it so uri is sure to parse stuff like google.com with the http
|
# Makes it so uri is sure to parse stuff like google.com with the http
|
||||||
|
@ -82,7 +95,4 @@ module Web
|
||||||
str =~ /^https?:\/\// ? str : "http#{'s' if str.include?(':443')}://#{str}"
|
str =~ /^https?:\/\// ? str : "http#{'s' if str.include?(':443')}://#{str}"
|
||||||
end
|
end
|
||||||
end
|
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