Sha256: c4d814405e30adaaf6a488f763e68db334d64597ef3495bfbc2800c14949516e

Contents?: true

Size: 1.53 KB

Versions: 4

Compression:

Stored size: 1.53 KB

Contents

require 'curb'
require 'crack'

module ShareChecker
  class Provider
    attr_reader :name
    
    def initialize(link, name = "noname", options = {})
      @link = link
      @name = name
      @options = options.symbolize_keys
    end
    
    def check
      response = self.class.get([url, @link].join)
      return nil unless response
      
      parse(response)
    end
    
    def parse(response)
      raise NotImplementedError, "Must be overwritten in subclasses"
    end
    
    def url
      raise NotImplementedError, "Must be overwritten in subclasses"
    end
    
    def parse_xml(body)
      begin
        content = Crack::XML.parse(body)
      rescue Exception => e
        puts "#{@name} error parse xml: #{body}, #{e.message}, #{@link}"
        content = nil
      end
      
      return content
    end
    
    def parse_json(body)
      begin
        content = Crack::JSON.parse(body)
      rescue Exception => e
        puts "#{@name} error parse xml: #{body}, #{e.message}, #{@link}"
        content = nil
      end
      
      return content
    end
    
    def method_missing(method_name, *args, &block)
      if @options.key?(method_name.to_sym)
        @options[method_name.to_sym]
      else
        super
      end
    end
    
    class << self
      def get(url)
        c = Curl::Easy.new(url) do |curl| 
          curl.headers["User-Agent"] = "Mozilla/5.0 (X11; Linux i686; rv:2.0.1) Gecko/20100101 Firefox/6.0.0"
          curl.verbose = false
        end

        c.perform
        c.body_str
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
share_checker-0.1.5 lib/share_checker/provider.rb
share_checker-0.1.4 lib/share_checker/provider.rb
share_checker-0.1.3 lib/share_checker/provider.rb
share_checker-0.1.2 lib/share_checker/provider.rb