Permalink
Browse files

Changes the Mash gem for the Hashie gem. This is the Mash gem but ren…

…amed to not clash with extlib
  • Loading branch information...
1 parent a582086 commit 365f8378b45c93ed6219ac49afec5c7f7eb85fe6 @hassox hassox committed Nov 13, 2009
Showing with 99 additions and 99 deletions.
  1. +15 −15 lib/twitter.rb
  2. +23 −23 lib/twitter/request.rb
  3. +23 −23 lib/twitter/search.rb
  4. +6 −6 lib/twitter/trends.rb
  5. +32 −32 test/twitter/request_test.rb
View
@@ -4,50 +4,50 @@
gem 'oauth', '~> 0.3.5'
require 'oauth'
-gem 'mash', '~> 0.0.3'
-require 'mash'
+gem 'hashie', '~> 0.1.3'
+require 'hashie'
gem 'httparty', '~> 0.4.3'
require 'httparty'
module Twitter
class TwitterError < StandardError
attr_reader :data
-
+
def initialize(data)
@data = data
super
end
end
-
+
class RateLimitExceeded < TwitterError; end
class Unauthorized < TwitterError; end
class General < TwitterError; end
-
+
class Unavailable < StandardError; end
class InformTwitter < StandardError; end
class NotFound < StandardError; end
-
-
+
+
def self.firehose
response = HTTParty.get('http://twitter.com/statuses/public_timeline.json', :format => :json)
- response.map { |tweet| Mash.new(tweet) }
+ response.map { |tweet| Hashie::Mash.new(tweet) }
end
-
+
def self.user(id)
response = HTTParty.get("http://twitter.com/users/show/#{id}.json", :format => :json)
- Mash.new(response)
+ Hashie::Mash.new(response)
end
-
+
def self.status(id)
response = HTTParty.get("http://twitter.com/statuses/show/#{id}.json", :format => :json)
- Mash.new(response)
+ Hashie::Mash.new(response)
end
-
+
def self.friend_ids(id)
HTTParty.get("http://twitter.com/friends/ids/#{id}.json", :format => :json)
end
-
+
def self.follower_ids(id)
HTTParty.get("http://twitter.com/followers/ids/#{id}.json", :format => :json)
end
@@ -60,4 +60,4 @@ def self.follower_ids(id)
require File.join(directory, 'twitter', 'request')
require File.join(directory, 'twitter', 'base')
require File.join(directory, 'twitter', 'search')
-require File.join(directory, 'twitter', 'trends')
+require File.join(directory, 'twitter', 'trends')
View
@@ -1,70 +1,70 @@
module Twitter
class Request
extend Forwardable
-
+
def self.get(client, path, options={})
new(client, :get, path, options).perform
end
-
+
def self.post(client, path, options={})
new(client, :post, path, options).perform
end
-
+
def self.put(client, path, options={})
new(client, :put, path, options).perform
end
-
+
def self.delete(client, path, options={})
new(client, :delete, path, options).perform
end
-
+
attr_reader :client, :method, :path, :options
-
+
def_delegators :client, :get, :post, :put, :delete
-
+
def initialize(client, method, path, options={})
@client, @method, @path, @options = client, method, path, {:mash => true}.merge(options)
end
-
+
def uri
@uri ||= begin
uri = URI.parse(path)
-
+
if options[:query] && options[:query] != {}
uri.query = to_query(options[:query])
end
-
+
uri.to_s
end
end
-
+
def perform
make_friendly(send("perform_#{method}"))
end
-
+
private
def perform_get
send(:get, uri, options[:headers])
end
-
+
def perform_post
send(:post, uri, options[:body], options[:headers])
end
-
+
def perform_put
send(:put, uri, options[:body], options[:headers])
end
-
+
def perform_delete
send(:delete, uri, options[:headers])
end
-
+
def make_friendly(response)
raise_errors(response)
data = parse(response)
options[:mash] ? mash(data) : data
end
-
+
def raise_errors(response)
case response.code.to_i
when 400
@@ -84,11 +84,11 @@ def raise_errors(response)
raise Unavailable, "(#{response.code}): #{response.message}"
end
end
-
+
def parse(response)
Crack::JSON.parse(response.body)
end
-
+
def mash(obj)
if obj.is_a?(Array)
obj.map { |item| make_mash_with_consistent_hash(item) }
@@ -101,18 +101,18 @@ def mash(obj)
# Lame workaround for the fact that mash doesn't hash correctly
def make_mash_with_consistent_hash(obj)
- m = Mash.new(obj)
+ m = Hashie::Mash.new(obj)
def m.hash
inspect.hash
end
return m
end
-
+
def to_query(options)
- options.inject([]) do |collection, opt|
+ options.inject([]) do |collection, opt|
collection << "#{opt[0]}=#{opt[1]}"
collection
end * '&'
end
end
-end
+end
View
@@ -2,110 +2,110 @@ module Twitter
class Search
include HTTParty
include Enumerable
-
+
attr_reader :result, :query
-
+
def initialize(q=nil, options={})
@options = options
clear
containing(q) if q && q.strip != ''
end
-
+
def user_agent
@options[:user_agent] || 'Ruby Twitter Gem'
end
-
+
def from(user)
@query[:q] << "from:#{user}"
self
end
-
+
def to(user)
@query[:q] << "to:#{user}"
self
end
-
+
def referencing(user)
@query[:q] << "@#{user}"
self
end
alias :references :referencing
alias :ref :referencing
-
+
def containing(word)
@query[:q] << "#{word}"
self
end
alias :contains :containing
-
+
# adds filtering based on hash tag ie: #twitter
def hashed(tag)
@query[:q] << "##{tag}"
self
end
-
+
# lang must be ISO 639-1 code ie: en, fr, de, ja, etc.
#
- # when I tried en it limited my results a lot and took
- # out several tweets that were english so i'd avoid
+ # when I tried en it limited my results a lot and took
+ # out several tweets that were english so i'd avoid
# this unless you really want it
def lang(lang)
@query[:lang] = lang
self
end
-
+
# Limits the number of results per page
def per_page(num)
@query[:rpp] = num
self
end
-
+
# Which page of results to fetch
def page(num)
@query[:page] = num
self
end
-
- # Only searches tweets since a given id.
+
+ # Only searches tweets since a given id.
# Recommended to use this when possible.
def since(since_id)
@query[:since_id] = since_id
self
end
-
+
# Search tweets by latitude, longitude, and a given range.
# Ranges like 25km and 50mi work.
def geocode(lat, long, range)
@query[:geocode] = [lat, long, range].join(',')
self
end
-
+
def max(id)
@query[:max_id] = id
self
end
-
+
# Clears all the query filters to make a new search
def clear
@fetch = nil
@query = {}
@query[:q] = []
self
end
-
+
def fetch(force=false)
if @fetch.nil? || force
query = @query.dup
query[:q] = query[:q].join(' ')
response = self.class.get('http://search.twitter.com/search.json', :query => query, :format => :json, :headers => {'User-Agent' => user_agent})
- @fetch = Mash.new(response)
+ @fetch = Hashie::Mash.new(response)
end
-
+
@fetch
end
-
+
def each
fetch()['results'].each { |r| yield r }
end
end
-end
+end
View
@@ -3,27 +3,27 @@ class Trends
include HTTParty
base_uri 'search.twitter.com/trends'
format :json
-
+
# :exclude => 'hashtags' to exclude hashtags
def self.current(options={})
mashup(get('/current.json', :query => options))
end
-
+
# :exclude => 'hashtags' to exclude hashtags
# :date => yyyy-mm-dd for specific date
def self.daily(options={})
mashup(get('/daily.json', :query => options))
end
-
+
# :exclude => 'hashtags' to exclude hashtags
# :date => yyyy-mm-dd for specific date
def self.weekly(options={})
mashup(get('/weekly.json', :query => options))
end
-
+
private
def self.mashup(response)
- response['trends'].values.flatten.map { |t| Mash.new(t) }
+ response['trends'].values.flatten.map { |t| Hashie::Mash.new(t) }
end
end
-end
+end
Oops, something went wrong.

1 comment on commit 365f837

Alright! I think this closes out a few open issues, no?

Please sign in to comment.