Skip to content

Commit

Permalink
Changes the Mash gem for the Hashie gem. This is the Mash gem but ren…
Browse files Browse the repository at this point in the history
…amed to not clash with extlib
  • Loading branch information
Daniel Neighman committed Nov 13, 2009
1 parent a582086 commit 365f837
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 99 deletions.
30 changes: 15 additions & 15 deletions lib/twitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
46 changes: 23 additions & 23 deletions lib/twitter/request.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) }
Expand All @@ -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
46 changes: 23 additions & 23 deletions lib/twitter/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 6 additions & 6 deletions lib/twitter/trends.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading

1 comment on commit 365f837

@jerodsanto
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Please sign in to comment.