tf2r 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/tf2r/api.rb +4 -1
- data/lib/tf2r/raffle.rb +31 -6
- data/lib/tf2r/scraper.rb +4 -6
- data/lib/tf2r/text_helpers.rb +16 -0
- data/lib/tf2r/version.rb +1 -1
- data/spec/raffle_spec.rb +2 -6
- data/spec/scraper_spec.rb +3 -3
- metadata +2 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: f93befc5ee498a05c0f863dca2156747a184e75b
         | 
| 4 | 
            +
              data.tar.gz: 88a9ee5bb47d1eb9725065277f53bc251f309f24
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 1a6339383a2d14a9cc3da5bb378d0f2fbced0d31f7790ea47b311542ec554cb74d0855c6e7fd12493b8e2deb7415d17f2d41d257d4ad5ba78cb451d4da694f81
         | 
| 7 | 
            +
              data.tar.gz: 8816069f87359a8b4efee8c4dbc6292a23885e16e86a288b7c1c5a3a570ac08f4c87bf2d7bf66270d9e9404af1497679bb5811a2786f1b9a28e37cb2205206c3
         | 
    
        data/Gemfile.lock
    CHANGED
    
    
    
        data/lib/tf2r/api.rb
    CHANGED
    
    | @@ -5,6 +5,9 @@ module TF2R | |
| 5 5 | 
             
              class API
         | 
| 6 6 | 
             
                HOST = 'http://tf2r.com'
         | 
| 7 7 | 
             
                ENDPOINT = '/job.php'
         | 
| 8 | 
            +
                # newentry on a checkraffle job can only return up to 2500 entries. This is
         | 
| 9 | 
            +
                # a problem for any raffles with a maximum entries greater than 2500.
         | 
| 10 | 
            +
                MAX_ENTRY_RESPONSE_COUNT = 2500
         | 
| 8 11 |  | 
| 9 12 | 
             
                def self.raffle_info(link_snippet)
         | 
| 10 13 | 
             
                  params = {checkraffle: true, rid: link_snippet[1..-1],
         | 
| @@ -14,7 +17,7 @@ module TF2R | |
| 14 17 |  | 
| 15 18 | 
             
                private
         | 
| 16 19 |  | 
| 17 | 
            -
                # Make this sane with something like httparty
         | 
| 20 | 
            +
                # TODO: Make this sane with something like httparty
         | 
| 18 21 | 
             
                def self.request(params)
         | 
| 19 22 | 
             
                  uri = URI.parse(HOST)
         | 
| 20 23 | 
             
                  http = Net::HTTP.new(uri.host, uri.port)
         | 
    
        data/lib/tf2r/raffle.rb
    CHANGED
    
    | @@ -1,16 +1,18 @@ | |
| 1 1 | 
             
            module TF2R
         | 
| 2 2 | 
             
              # This class provides a simple wrapper around grabbing information for a
         | 
| 3 3 | 
             
              # raffle from the Scraper and API.
         | 
| 4 | 
            -
              # TODO:  | 
| 4 | 
            +
              # TODO: document
         | 
| 5 5 | 
             
              class Raffle
         | 
| 6 6 | 
             
                include TF2R::TextHelpers
         | 
| 7 7 |  | 
| 8 | 
            -
                attr_reader :link_snippet | 
| 8 | 
            +
                attr_reader :link_snippet
         | 
| 9 9 |  | 
| 10 | 
            -
                def initialize(link_snippet | 
| 10 | 
            +
                def initialize(link_snippet)
         | 
| 11 11 | 
             
                  @link_snippet = link_snippet
         | 
| 12 | 
            -
             | 
| 13 | 
            -
                  @ | 
| 12 | 
            +
             | 
| 13 | 
            +
                  @scraper = Scraper.new
         | 
| 14 | 
            +
                  populate_raffle_info
         | 
| 15 | 
            +
                  get_full_participants if max_entries > API::MAX_ENTRY_RESPONSE_COUNT
         | 
| 14 16 | 
             
                end
         | 
| 15 17 |  | 
| 16 18 | 
             
                def info
         | 
| @@ -25,12 +27,35 @@ module TF2R | |
| 25 27 | 
             
                  @scraper_info[1]
         | 
| 26 28 | 
             
                end
         | 
| 27 29 |  | 
| 30 | 
            +
                # TODO: test raffles with max_entries greater than 2500
         | 
| 28 31 | 
             
                def participants
         | 
| 29 | 
            -
                   | 
| 32 | 
            +
                  if max_entries > 2500
         | 
| 33 | 
            +
                    @normalized_participants ||= normalize_entries(@api_info['newentry'])
         | 
| 34 | 
            +
                  else
         | 
| 35 | 
            +
                    @full_participants
         | 
| 36 | 
            +
                  end
         | 
| 30 37 | 
             
                end
         | 
| 31 38 |  | 
| 32 39 | 
             
                private
         | 
| 33 40 |  | 
| 41 | 
            +
                def populate_raffle_info
         | 
| 42 | 
            +
                  threads = []
         | 
| 43 | 
            +
                  threads << Thread.new do
         | 
| 44 | 
            +
                    @api_info = API.raffle_info(@link_snippet)
         | 
| 45 | 
            +
                  end
         | 
| 46 | 
            +
                  threads << Thread.new do
         | 
| 47 | 
            +
                    page = @scraper.fetch(raffle_link(@link_snippet))
         | 
| 48 | 
            +
                    @scraper_info = @scraper.scrape_raffle(page)
         | 
| 49 | 
            +
                  end
         | 
| 50 | 
            +
             | 
| 51 | 
            +
                  threads.each { |t| t.join }
         | 
| 52 | 
            +
                end
         | 
| 53 | 
            +
             | 
| 54 | 
            +
                def get_full_participants
         | 
| 55 | 
            +
                  page = @scraper.fetch(raffle_link_full(@link_snippet))
         | 
| 56 | 
            +
                  @full_participants = @scraper.scrape_raffle_for_participants(page)
         | 
| 57 | 
            +
                end
         | 
| 58 | 
            +
             | 
| 34 59 | 
             
                def normalize_entries(entries)
         | 
| 35 60 | 
             
                  entries.map do |entry|
         | 
| 36 61 | 
             
                    entry[:steam_id] = extract_steam_id entry.delete('link')
         | 
    
        data/lib/tf2r/scraper.rb
    CHANGED
    
    | @@ -13,7 +13,7 @@ module TF2R | |
| 13 13 | 
             
                # @option opts [String] :user_agent a custom User-Agent header content
         | 
| 14 14 | 
             
                # @option opts [File] :cookies_txt a cookies.txt to load the Mechanize
         | 
| 15 15 | 
             
                #   agent with
         | 
| 16 | 
            -
                def initialize(options)
         | 
| 16 | 
            +
                def initialize(options={})
         | 
| 17 17 | 
             
                  @mech = Mechanize.new { |mech|
         | 
| 18 18 | 
             
                    mech.user_agent = options[:user_agent] || "TF2R::Scraper #{VERSION}"
         | 
| 19 19 | 
             
                  }
         | 
| @@ -156,11 +156,11 @@ module TF2R | |
| 156 156 | 
             
                   start_time: start_time, end_time: end_time}
         | 
| 157 157 | 
             
                end
         | 
| 158 158 |  | 
| 159 | 
            -
                # @deprecated Please use {#TF2R::API} instead. The API provides a far more
         | 
| 160 | 
            -
                # efficient and reliable method of retrieving the entry list.
         | 
| 161 | 
            -
                #
         | 
| 162 159 | 
             
                # Scrapes a raffle page for all the participants.
         | 
| 163 160 | 
             
                #
         | 
| 161 | 
            +
                # This should rarely be used. This will only be necessary in the case that
         | 
| 162 | 
            +
                # a raffle has maximum entries greater than 2500.
         | 
| 163 | 
            +
                #
         | 
| 164 164 | 
             
                # @param page [Mechanize::Page] the raffle page.
         | 
| 165 165 | 
             
                # @return [Array] contains Hashes representing each of the participants,
         | 
| 166 166 | 
             
                # in chronological order (first entered to last).
         | 
| @@ -168,8 +168,6 @@ module TF2R | |
| 168 168 | 
             
                #   * :username (+String+) — the participant's username.
         | 
| 169 169 | 
             
                #   * :colour (+String+) — hex colour code of the participant's username.
         | 
| 170 170 | 
             
                def scrape_raffle_for_participants(page)
         | 
| 171 | 
            -
                  warn '[DEPRECATION] `scrape_raffle_for_participants` is deprecated. '\
         | 
| 172 | 
            -
                        'Please use `TF2R::API` instead.'
         | 
| 173 171 | 
             
                  participants = []
         | 
| 174 172 | 
             
                  participant_divs = page.parser.css('.pentry')
         | 
| 175 173 | 
             
                  participant_divs.each do |participant|
         | 
    
        data/lib/tf2r/text_helpers.rb
    CHANGED
    
    | @@ -1,4 +1,5 @@ | |
| 1 1 | 
             
            module TF2R
         | 
| 2 | 
            +
              # TODO: document
         | 
| 2 3 | 
             
              module TextHelpers
         | 
| 3 4 | 
             
                # Extracts a hex colour code.
         | 
| 4 5 | 
             
                #
         | 
| @@ -30,5 +31,20 @@ module TF2R | |
| 30 31 | 
             
                def extract_steam_id(href)
         | 
| 31 32 | 
             
                  /http:\/\/tf2r.com\/user\/(\d+)\.html/.match(href)[1].to_i
         | 
| 32 33 | 
             
                end
         | 
| 34 | 
            +
             | 
| 35 | 
            +
                # Generates the TF2R link for a raffle given a link snippet.
         | 
| 36 | 
            +
                #
         | 
| 37 | 
            +
                # @example
         | 
| 38 | 
            +
                #   raffle_link('kabc123') => 'http://tf2r.com/kabc123.html'
         | 
| 39 | 
            +
                #
         | 
| 40 | 
            +
                # @param link_snippet [String] the raffle link snippet.
         | 
| 41 | 
            +
                # @return [String] the raffle link.
         | 
| 42 | 
            +
                def raffle_link(link_snippet)
         | 
| 43 | 
            +
                  link = "http://tf2r.com/#{link_snippet}.html"
         | 
| 44 | 
            +
                end
         | 
| 45 | 
            +
             | 
| 46 | 
            +
                def raffle_link_full(link_snippet)
         | 
| 47 | 
            +
                  "#{raffle_link(link_snippet)}?full"
         | 
| 48 | 
            +
                end
         | 
| 33 49 | 
             
              end
         | 
| 34 50 | 
             
            end
         | 
    
        data/lib/tf2r/version.rb
    CHANGED
    
    
    
        data/spec/raffle_spec.rb
    CHANGED
    
    | @@ -1,15 +1,11 @@ | |
| 1 1 | 
             
            require 'spec_helper'
         | 
| 2 2 |  | 
| 3 3 | 
             
            describe TF2R::Raffle do
         | 
| 4 | 
            -
              let(: | 
| 5 | 
            -
              let(:scraper_info) {
         | 
| 4 | 
            +
              let(:raffle) {
         | 
| 6 5 | 
             
                VCR.use_cassette('kstzcbd') do
         | 
| 7 | 
            -
                   | 
| 6 | 
            +
                  raffle = TF2R::Raffle.new('kstzcbd')
         | 
| 8 7 | 
             
                end
         | 
| 9 8 | 
             
              }
         | 
| 10 | 
            -
              let(:raffle) {
         | 
| 11 | 
            -
                TF2R::Raffle.new('kstzcbd', TF2R::API.raffle_info('kstzcbd'), scraper_info)
         | 
| 12 | 
            -
              }
         | 
| 13 9 |  | 
| 14 10 | 
             
              describe '#info' do
         | 
| 15 11 | 
             
                let(:result) { raffle.info }
         | 
    
        data/spec/scraper_spec.rb
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            require 'spec_helper'
         | 
| 2 2 |  | 
| 3 3 | 
             
            describe TF2R::Scraper do
         | 
| 4 | 
            -
              let(:scraper) { TF2R::Scraper.new( | 
| 4 | 
            +
              let(:scraper) { TF2R::Scraper.new() }
         | 
| 5 5 | 
             
              let(:raffle_page) {
         | 
| 6 6 | 
             
                VCR.use_cassette('raffle_kstzcbd') do
         | 
| 7 7 | 
             
                  scraper.fetch('http://tf2r.com/kstzcbd.html')
         | 
| @@ -10,14 +10,14 @@ describe TF2R::Scraper do | |
| 10 10 |  | 
| 11 11 | 
             
              it 'is instantiable' do
         | 
| 12 12 | 
             
                expect{
         | 
| 13 | 
            -
                  TF2R::Scraper.new( | 
| 13 | 
            +
                  TF2R::Scraper.new()
         | 
| 14 14 | 
             
                }.not_to raise_error
         | 
| 15 15 | 
             
              end
         | 
| 16 16 |  | 
| 17 17 | 
             
              describe '#new' do
         | 
| 18 18 | 
             
                context 'no options are given' do
         | 
| 19 19 | 
             
                  it 'creates an agent with default user agent if none is specified' do
         | 
| 20 | 
            -
                    scraper = TF2R::Scraper.new( | 
| 20 | 
            +
                    scraper = TF2R::Scraper.new()
         | 
| 21 21 | 
             
                    expect(
         | 
| 22 22 | 
             
                      scraper.instance_variable_get(:@mech).user_agent
         | 
| 23 23 | 
             
                    ).to eql("TF2R::Scraper #{TF2R::VERSION}")
         | 
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: tf2r
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0. | 
| 4 | 
            +
              version: 0.3.0
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Justin Kim
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2014-08- | 
| 11 | 
            +
            date: 2014-08-18 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: bundler
         |