Sha256: e58ee2dec91776d6c671b8092d64c229064a0bf7e3f98f430b30a5f7c86722c2

Contents?: true

Size: 1.7 KB

Versions: 1

Compression:

Stored size: 1.7 KB

Contents

require 'rubygems'
require 'httparty'
require 'open-uri'

module Glue

  VERSION = '1.1.1'
  DOMAIN  = 'gluenow.com'

  class AuthError < StandardError;                   end
  class FormatHelper; include HTTParty; format :xml; end

  class API < Glue::FormatHelper
  
    POST   = '/api/post'
    USER   = '/api/user'
    
    attr_accessor :site

    def initialize subdomain, user, pass
      raise  AuthError, 'Username, Password or Account subdomain is blank.' \
        if subdomain.empty? || user.empty? || pass.empty?
      @auth   = { :username => user, :password => pass }
      @site   = "#{subdomain}.#{DOMAIN}"
      self.class.base_uri @site
    end

    def valid_site?
      login_page.match(/<body[^>]*id=["']login["']/) ? true : false
    end

    def user_info
      response = self.class.get(
        USER,
        { :basic_auth => @auth }
      )
      response['rsp'] ? response : {}
    end

    def post title, body, *opts
      response = self.class.get(
        POST,
        { :query        =>  {
            :title      =>  title,
            :body       =>  body,
            :draft      =>  opts.include?( :draft  )  ,
            :author     =>  opts.include?( :author )  },
          :basic_auth   =>  @auth 
        }
      )
      response['rsp'] ? response : {}
    end
 
  private
    
    def login_page
      open("http://#{@site}").read
    end

  end
  
  class RSS < Glue::FormatHelper

    NEWS = '/news/rss'
    
    def initialize subdomain
      raise  AuthError, 'Account Subdomain is missing or blank' if subdomain.empty?
      self.class.base_uri "#{subdomain}.#{DOMAIN}"
    end
    
    def feed limit=999, page=1
      self.class.get("#{NEWS}?limit=#{limit}&page=#{page}")
    end
  
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
glue-1.1.1 lib/glue.rb