# Copyright (c) 2009 Douglas Willcocks # # Permission is hereby granted, free of charge, to any person # obtaining a copy of this software and associated documentation # files (the "Software"), to deal in the Software without # restriction, including without limitation the rights to use, # copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the # Software is furnished to do so, subject to the following # conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR # OTHER DEALINGS IN THE SOFTWARE. # Provide a simple wrapper interface to Instapaper require 'net/http' require 'net/https' require 'uri' module Instapaper class Base include Protocol include Constants include Request include Response include Exceptions # Instatiate a new Instapaper object that will act as the adaptor the Instapaper # API. # # Usernames are mandatory, but passwords are optional as on Instapaper def initialize(username, password = nil) @username = username @password = password @protocol = PROTOCOLS[:secure] end # Add a URL to Instapaper # # See Base#get_add_request def add(url_data, options = {}) req = get_add_request(url_data,options) return send_request(req) end # Create a request to add a URL to instapaper # # Takes a hash of options: # # :title # plain text, no HTML, UTF-8 # :selection # plain text, no HTML, UTF-8 # :auto_title # Replaces title. Instapaper will crawl the URL to detect a title. # Use this if you do not know a title for the URL, such as when it’s an # inline link somewhere that hasn’t been opened. # Defaults to 1 # def get_add_request(url_data, options = {}) raise InvalidRequestDetailsException unless options.is_a?(Hash) options[:url] = url_data AddURLRequest.new(options,@username,@password) end # Test the authentication credentials def authenticate req = AuthenticationRequest.new(@username,@password) return send_request(req) end # Do the actual communication with the server def send_request(request) request.use_clear_protocol! unless secure? request.prepare url = URI.parse(request.url) req = Net::HTTP::Post.new(url.path) req.set_form_data(request.post_data) http = Net::HTTP.new(url.host,url.port) http.use_ssl = secure? # Instapaper triggers an invalid SSL, so there's no way of telling # if it's being spoofed. May as well just turn off warnings. http.verify_mode = OpenSSL::SSL::VERIFY_NONE res = http.request(req) return ResponseBuilder.build(res,request) end end end