lib/muddyit/base.rb in muddyit_fu-0.2.9 vs lib/muddyit/base.rb in muddyit_fu-0.2.10

- old
+ new

@@ -6,11 +6,11 @@ class Base class_attr_accessor :http_open_timeout class_attr_accessor :http_read_timeout attr_accessor :rest_endpoint - attr_reader :consumer_key, :consumer_secret, :access_token, :access_token_secret + attr_reader :consumer_key, :consumer_secret, :access_token, :access_token_secret, :username, :password, :auth_type @@http_open_timeout = 120 @@http_read_timeout = 120 REST_ENDPOINT = 'http://www.muddy.it' @@ -48,29 +48,38 @@ # access_token_secret: DDD # def initialize(config_hash_or_file) if config_hash_or_file.is_a? Hash config_hash_or_file.nested_symbolize_keys! + @username = config_hash_or_file[:username] + @password = config_hash_or_file[:password] @consumer_key = config_hash_or_file[:consumer_key] @consumer_secret = config_hash_or_file[:consumer_secret] @access_token = config_hash_or_file[:access_token] @access_token_secret = config_hash_or_file[:access_token_secret] @rest_endpoint = config_hash_or_file.has_key?(:rest_endpoint) ? config_hash_or_file[:rest_endpoint] : REST_ENDPOINT - raise 'config_hash must contain consumer_key and consumer_secret' unless @consumer_key and @consumer_secret else config = YAML.load_file(config_hash_or_file) config.nested_symbolize_keys! + @username = config[:username] + @password = config[:password] @consumer_key = config[:consumer_key] @consumer_secret = config[:consumer_secret] @access_token = config[:access_token] @access_token_secret = config[:access_token_secret] @rest_endpoint = config.has_key?(:rest_endpoint) ? config[:rest_endpoint] : REST_ENDPOINT - raise 'config file must contain consumer_key and consumer_secret' unless @consumer_key and @consumer_secret end - @consumer = ::OAuth::Consumer.new(@consumer_key, @consumer_secret, {:site=>@rest_endpoint}) - @accesstoken = ::OAuth::AccessToken.new(@consumer, @access_token, @access_token_secret) + if !@consumer_key.nil? + @auth_type = :oauth + @consumer = ::OAuth::Consumer.new(@consumer_key, @consumer_secret, {:site=>@rest_endpoint}) + @accesstoken = ::OAuth::AccessToken.new(@consumer, @access_token, @access_token_secret) + elsif !@username.nil? + @auth_type = :basic + else + raise "unable to find authentication credentials" + end end # sends a request to the muddyit REST api # @@ -83,19 +92,26 @@ # hash of query parameters, you do not need to include access_key_id, secret_access_key because these are added automatically # def send_request(api_url, http_method = :get, opts = {}, body = nil) raise 'no api_url supplied' unless api_url - res = request_over_http(api_url, http_method, opts, body) - # Strip any js wrapping methods + + res = nil + case @auth_type + when :oauth + res = oauth_request_over_http(api_url, http_method, opts, body) + when :basic + res = basic_request_over_http(api_url, http_method, opts, body) + end case res when Net::HTTPSuccess, Net::HTTPRedirection case res.body when " " return res when /^.+\((.+)\)$/ + # Strip any js callback method return JSON.parse($1) else return JSON.parse(res.body) end when Net::HTTPNotFound @@ -106,14 +122,33 @@ end # creates and/or returns the Muddyit::Collections object def collections() @collections ||= Muddyit::Collections.new(self) end + # A mirror of the pages.create method, but for one off, non-stored, quick extraction + def extract(doc={}, options={}) + + # Ensure we get content_data as well + options[:include_content] = true + + # Ensure we have encoded the identifier and URI + unless doc[:uri] || doc[:text] + raise + end + + body = { :page => doc.merge!(:options => options) } + + api_url = "/extract" + response = self.send_request(api_url, :post, {}, body.to_json) + return Muddyit::Collections::Collection::Pages::Page.new(self, response) + + end + protected # For easier testing. You can mock this method with a XML file you re expecting to receive - def request_over_http(api_url, http_method, opts, body) + def oauth_request_over_http(api_url, http_method, opts, body) http_opts = { "Accept" => "application/json", "Content-Type" => "application/json", "User-Agent" => "muddyit_fu" } query_string = opts.to_a.map {|x| x.join("=")}.join("&") case http_method @@ -127,10 +162,68 @@ when :delete @accesstoken.delete(api_url, http_opts) else raise 'invalid http method specified' end - + end + + def basic_request_over_http(path, http_method, opts, data) + + http_opts = { "Accept" => "application/json", "Content-Type" => "application/json", "User-Agent" => "muddyit_fu" } + query_string = opts.to_a.map {|x| x.join("=")}.join("&") + + u = URI.parse(@rest_endpoint+path) + + if [:post, :put].include?(http_method) + data.reject! { |k,v| v.nil? } if data.is_a?(Hash) + end + + headers = http_opts + + case http_method + when :post + request = Net::HTTP::Post.new(path,headers) + request.basic_auth @username, @password + request["Content-Length"] = 0 # Default to 0 + when :put + request = Net::HTTP::Put.new(path,headers) + request.basic_auth @username, @password + request["Content-Length"] = 0 # Default to 0 + when :get + request = Net::HTTP::Get.new(path,headers) + request.basic_auth @username, @password + when :delete + request = Net::HTTP::Delete.new(path,headers) + request.basic_auth @username, @password + when :head + request = Net::HTTP::Head.new(path,headers) + request.basic_auth @username, @password + else + raise ArgumentError, "Don't know how to handle http_method: :#{http_method.to_s}" + end + + if data.is_a?(Hash) + request.set_form_data(data) + elsif data + if data.respond_to?(:read) + request.body_stream = data + if data.respond_to?(:length) + request["Content-Length"] = data.length + elsif data.respond_to?(:stat) && data.stat.respond_to?(:size) + request["Content-Length"] = data.stat.size + else + raise ArgumentError, "Don't know how to send a body_stream that doesn't respond to .length or .stat.size" + end + else + request.body = data.to_s + request["Content-Length"] = request.body.length + end + end + + http = Net::HTTP.new(u.host, u.port) + #http.open_timeout = self.http_open_timeout unless self.http_open_timeout.nil? + #http.read_timeout = self.http_read_timeout unless self.http_read_timeout.nil? + http.start { |http| http.request(request) } end end end \ No newline at end of file