Skip to content

Commit

Permalink
Changed inner interface to io.Reader
Browse files Browse the repository at this point in the history
  • Loading branch information
demyanovs committed Jul 25, 2024
1 parent 724aa8d commit 05d2d39
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions robotstxt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package robotstxt
import (
"bufio"
"errors"
"io"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -57,9 +58,8 @@ var (
// FromResponse creates a new instance of RobotsData from an HTTP response.
func FromResponse(resp *http.Response) (*RobotsData, error) {
r := RobotsData{}
scanner := bufio.NewScanner(resp.Body)

err := r.parseRules(scanner)
err := r.parseRules(resp.Body)
if err != nil {
return nil, err
}
Expand All @@ -70,11 +70,9 @@ func FromResponse(resp *http.Response) (*RobotsData, error) {
// FromString creates a new instance of RobotsData from string.
func FromString(text string) (*RobotsData, error) {
r := RobotsData{}
reader := strings.NewReader(text)

re := strings.NewReader(text)
scanner := bufio.NewScanner(re)

err := r.parseRules(scanner)
err := r.parseRules(reader)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -139,13 +137,14 @@ func (rb *RobotsData) applicableRules(userAgent string) []Rule {
return []Rule{}
}

func (rb *RobotsData) parseRules(scanner *bufio.Scanner) error {
func (rb *RobotsData) parseRules(reader io.Reader) error {
rb.UserAgents = make(map[string]UserAgent)

var currentUserAgent string
rules := make(map[string][]Rule)
delays := make(map[string]*int)

scanner := bufio.NewScanner(reader)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
rule, val := parseLine(line)
Expand Down

0 comments on commit 05d2d39

Please sign in to comment.