Permalink
Browse files

Modified version to 0.6.3. Support to tags refactored. Include curl h…

…elper to improve support for objects without ActiveResource base like 'tag' which needs a parse in a html document from a httprequest.
  • Loading branch information...
1 parent 05606f7 commit b112523b0a0316afba2afd13daa7a65bda36abbf @luisbebop luisbebop committed Apr 22, 2009
View
@@ -8,7 +8,7 @@ require "fileutils"
require "rubygems"
require "rake/gempackagetask"
-VERSION = "0.6"
+VERSION = "0.6.3"
highrise_gemspec = Gem::Specification.new do |s|
s.name = "highrise"
View
@@ -2,7 +2,7 @@
Gem::Specification.new do |s|
s.name = %q{highrise}
- s.version = "0.6.2"
+ s.version = "0.6.3"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Marcos Tapaj\303\263s"]
@@ -11,7 +11,7 @@ Gem::Specification.new do |s|
s.description = %q{Find awesome stuff}
s.email = %q{tapajos@improveit.com.br}
s.extra_rdoc_files = ["README.mkdn"]
- s.files = ["README.mkdn", "Rakefile", "lib/highrise", "lib/highrise/base.rb", "lib/highrise/comment.rb", "lib/highrise/company.rb", "lib/highrise/email.rb", "lib/highrise/group.rb", "lib/highrise/kase.rb", "lib/highrise/membership.rb", "lib/highrise/note.rb", "lib/highrise/pagination.rb", "lib/highrise/person.rb", "lib/highrise/subject.rb", "lib/highrise/task.rb", "lib/highrise/user.rb", "lib/highrise/tag.rb", "lib/highrise/taggable.rb", "lib/highrise.rb", "spec/highrise", "spec/highrise/base_spec.rb", "spec/highrise/comment_spec.rb", "spec/highrise/company_spec.rb", "spec/highrise/email_spec.rb", "spec/highrise/group_spec.rb", "spec/highrise/kase_spec.rb", "spec/highrise/membership_spec.rb", "spec/highrise/note_spec.rb", "spec/highrise/pagination_spec.rb", "spec/highrise/person_spec.rb", "spec/highrise/subject_spec.rb", "spec/highrise/task_spec.rb", "spec/highrise/user_spec.rb"]
+ s.files = ["README.mkdn", "Rakefile", "lib/highrise", "lib/highrise/base.rb", "lib/highrise/comment.rb", "lib/highrise/company.rb", "lib/highrise/curlhelper.rb", "lib/highrise/curly.rb", "lib/highrise/email.rb", "lib/highrise/group.rb", "lib/highrise/kase.rb", "lib/highrise/membership.rb", "lib/highrise/note.rb", "lib/highrise/pagination.rb", "lib/highrise/person.rb", "lib/highrise/subject.rb", "lib/highrise/tag.rb", "lib/highrise/taggable.rb", "lib/highrise/task.rb", "lib/highrise/user.rb", "lib/highrise.rb", "spec/highrise", "spec/highrise/base_spec.rb", "spec/highrise/comment_spec.rb", "spec/highrise/companies/16883216.html", "spec/highrise/company_spec.rb", "spec/highrise/curlhelper_spec.rb", "spec/highrise/curly_spec.rb", "spec/highrise/email_spec.rb", "spec/highrise/group_spec.rb", "spec/highrise/kase_spec.rb", "spec/highrise/membership_spec.rb", "spec/highrise/note_spec.rb", "spec/highrise/pagination_spec.rb", "spec/highrise/people/16887003.html", "spec/highrise/person_spec.rb", "spec/highrise/subject_spec.rb", "spec/highrise/task_spec.rb", "spec/highrise/user_spec.rb"]
s.has_rdoc = true
s.homepage = %q{http://www.improveit.com.br/en/company/tapajos}
s.require_paths = ["lib"]
View
@@ -1,6 +1,16 @@
require 'rubygems'
require 'active_resource'
+require 'uri'
+require 'curb'
+gem 'activesupport', '>= 2.1'
+require 'active_support/basic_object'
+require 'hpricot'
+require 'iconv'
+
require File.dirname(__FILE__) + '/highrise/pagination'
+require File.dirname(__FILE__) + '/highrise/curly'
+require File.dirname(__FILE__) + '/highrise/curlhelper'
+require File.dirname(__FILE__) + '/highrise/taggable'
require File.dirname(__FILE__) + '/highrise/base'
require File.dirname(__FILE__) + '/highrise/subject'
require File.dirname(__FILE__) + '/highrise/comment'
@@ -14,4 +24,3 @@
require File.dirname(__FILE__) + '/highrise/task'
require File.dirname(__FILE__) + '/highrise/user'
require File.dirname(__FILE__) + '/highrise/tag'
-require File.dirname(__FILE__) + '/highrise/taggable'
View
@@ -0,0 +1,27 @@
+module Highrise
+ class CurlHelper
+
+ # thanks mislav ;) http://github.com/mislav/curly/tree/master
+
+ def initialize
+ @curly = Curly.new
+ @curly.http_auth_types = Curl::CURLAUTH_BASIC
+ end
+
+ def get_userpwd_from_url(url)
+ userpwd_from_url_regex = /(http|https):\/\/((?:[\w\.\-\+%!$&'\(\)*\+,;=]+:)*[\w\.\-\+%!$&'\(\)*\+,;=]+)?/
+ match = userpwd_from_url_regex.match(url)
+ match.captures[1] unless match.nil?
+ end
+
+ def get_document(url)
+ @curly.userpwd = get_userpwd_from_url(url)
+ @curly.get(url).doc
+ end
+
+ def get_document_from_id(collection_name_and_id)
+ get_document("#{Highrise::Base.site.to_s}#{collection_name_and_id}")
+ end
+
+ end
+end
View
@@ -0,0 +1,96 @@
+class Curly < ActiveSupport::BasicObject
+ attr_reader :uri
+
+ def initialize(uri = nil)
+ @curl = Curl::Easy.new
+ self.uri = uri
+ self.follow_location = true
+ yield self if block_given?
+ end
+
+ def uri=(obj)
+ case obj
+ when String
+ unless @uri
+ @uri = URI.parse(obj)
+ else
+ @uri += obj
+ end
+ when URI::HTTP
+ @uri = obj
+ when nil
+ return
+ else
+ raise "unsupported URI type (#{obj.class.name} given)"
+ end
+
+ self.url = @uri.to_s
+ end
+
+ def method_missing(method, *args, &block)
+ @curl.send(method, *args, &block)
+ end
+
+ def cookiejar=(filename)
+ self.enable_cookies = true
+ @curl.cookiejar = filename
+ end
+
+ def get(uri = nil)
+ self.uri = uri
+ http_get
+ raise "expected 2xx, got #{response_code} (GET #{url})" unless success?
+ self
+ end
+
+ def success?
+ response_code >= 200 and response_code < 300
+ end
+
+ def doc
+ Hpricot body_unicode
+ end
+
+ def encoding
+ return @encoding unless @encoding == false
+ @encoding = if body_str =~ /;\s*charset=([\w-]+)\s*['"]/
+ $1.downcase
+ else
+ false
+ end
+ end
+
+ def post(params)
+ fields = params.map do |key, value|
+ Curl::PostField.content(key.to_s, value.to_s)
+ end
+ http_post *fields
+ end
+
+ class Form
+ def initialize(element)
+ @node = element
+ end
+
+ def elements
+ @node.search('input, button, select, textarea')
+ end
+ end
+
+ protected
+
+ def body_unicode
+ body = body_str
+ if encoding and encoding != 'utf-8'
+ body = Iconv.conv('UTF-8', encoding, body)
+ end
+ body
+ end
+
+end
+
+Hpricot::Doc.class_eval do
+ def forms
+ search('form').map { |f| Curly::Form.new(f) }
+ end
+end
View
@@ -15,20 +15,6 @@ def name
"#{first_name} #{last_name}".strip
end
- def tags
- # thanks for the user Inka, http://forum.37signals.com/highrise/forums/15/topics/1312?page=2
- usertags = Array.new
- codigo = self.id
- tags = Highrise::Tag.find(:all)
- tags.each do |tag|
- id = tag.attributes["id"]
- users = Highrise::Tag.find(:all, :from => "/tags/#{id}").to_xml
- if users.scan(/#{codigo}/).size > 0
- usertags << tag.attributes["name"]
- end
- end
- usertags
- end
end
end
View
@@ -1,9 +1,15 @@
module Highrise
- class Tag < Base
- include Pagination
-
- def user
- Tag.find(:all, :from => "/tags/#{id}.xml")
- end
+ class Tag
+ attr_accessor :id, :name
+
+ def initialize(id=nil, name=nil)
+ self.id = id
+ self.name = name
+ end
+
+ def ==(object)
+ (object.instance_of?(self.class) && object.id == self.id && object.name == self.name)
+ end
+
end
end
View
@@ -1,9 +1,29 @@
module Highrise
module Taggable
+
+ # thanks for the user Inka, http://forum.37signals.com/highrise/forums/15/topics/1312?page=2
+
def tag!(tag_name)
- # thanks for the user Inka, http://forum.37signals.com/highrise/forums/15/topics/1312?page=2
self.post(:tags, :name => tag_name) unless tag_name.blank?
- true
end
+
+ def untag!(tag_name)
+ to_delete = self.tags.find{|t| t.name==tag_name} unless tag_name.blank?
+ self.delete("tags/#{to_delete.id}") unless to_delete.nil?
+ end
+
+ def tags
+ tags = []
+ get_document.search("#show_tags a").each{ |a|
+ tags << Highrise::Tag.new(a['href'].gsub(/.*\//, ''), a.inner_html) if a['class'] == "grey tag"
+ }
+ tags
+ end
+
+ def get_document
+ @curl_helper ||= CurlHelper.new
+ @doc = @curl_helper.get_document_from_id("#{self.class.collection_name}/#{self.id}")
+ end
+
end
end
Oops, something went wrong.

0 comments on commit b112523

Please sign in to comment.