Class: TinyGrabber

Inherits:
Object
  • Object
show all
Defined in:
lib/tiny_grabber.rb,
lib/tiny_grabber/version.rb

Overview

Main class for TinyGrabber

Defined Under Namespace

Classes: Agent

Constant Summary collapse

VERSION =

Version number

"0.2.2"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTinyGrabber

Initialize a new TinyGrabber user agent.



17
18
19
# File 'lib/tiny_grabber.rb', line 17

def initialize
  @agent = TinyGrabber::Agent.new
end

Class Method Details

.get(url, config = {}) ⇒ Object

Singleton > HTTP::GET request

Parameters:

  • url

    Resource link

  • headers

    Request header



51
52
53
54
# File 'lib/tiny_grabber.rb', line 51

def self.get url, config = {}
  initialize  config
  @agent.fetch url, :get
end

.initialize(config = {}) ⇒ Object

Singleton > Initialize a new TinyGrabber user agent.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/tiny_grabber.rb', line 24

def self.initialize config = {}
  @agent = TinyGrabber::Agent.new

  @agent.debug = config[:debug] if config[:debug]
  @agent.read_timeout = config[:read_timeout] if config[:read_timeout]
  @agent.user_agent = config[:user_agent] if config[:user_agent]
  @agent.proxy = config[:proxy] if config[:proxy]
  @agent.basic_auth = config[:basic_auth] if config[:basic_auth]
  @agent.headers = config[:headers] if config[:headers]
  @agent.cookies = config[:cookies] if config[:cookies]
end

.post(url, params = {}, config = {}) ⇒ Object

Singleton > HTTP::GET request

Parameters:

  • url

    Resource link

  • headers

    Request header



73
74
75
76
# File 'lib/tiny_grabber.rb', line 73

def self.post url, params = {}, config = {}
  initialize  config
  @agent.fetch url, :post, {}, params
end

Instance Method Details

#basic_auth(username, password) ⇒ Object

Set BASIC_AUTH agent attribute

Parameters:

  • username

    Authentification username

  • password

    Authentification password



149
150
151
# File 'lib/tiny_grabber.rb', line 149

def basic_auth username, password
  @agent.basic_auth = { username: username, password: password }
end

#cookiesObject

Read COOKIES agent attribute



172
173
174
# File 'lib/tiny_grabber.rb', line 172

def cookies
  @agent.cookies
end

#cookies=(cookies) ⇒ Object

Set COOKIES agent attribute

Parameters:

  • cookies

    Request cookies



181
182
183
# File 'lib/tiny_grabber.rb', line 181

def cookies= cookies
  @agent.cookies = cookies
end

#debug=(debug) ⇒ Object

Set DEBUG flag

Parameters:

  • debug

    Flag to start debug



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/tiny_grabber.rb', line 83

def debug= debug
  if debug.is_a?(TrueClass) || debug.is_a?(FalseClass)
    @agent.debug = debug
    @agent.debug_destination = :print
    @agent.debug_save_html = false
  elsif debug.is_a? Hash
    @agent.debug = debug[:active]
    @agent.debug_destination = debug[:destination]
    @agent.debug_save_html = debug[:save_html]
  end
end

#get(url, headers = {}) ⇒ Object

HTTP::GET request

Parameters:

  • url

    Resource link

  • headers (defaults to: {})

    Request header



42
43
44
# File 'lib/tiny_grabber.rb', line 42

def get url, headers = {}
  @agent.fetch url, :get, headers
end

#headersObject

Read HEADERS agent attribute



156
157
158
# File 'lib/tiny_grabber.rb', line 156

def headers
  @agent.headers
end

#headers=(headers) ⇒ Object

Set HEADERS agent attribute

Parameters:

  • headers

    Request headers



165
166
167
# File 'lib/tiny_grabber.rb', line 165

def headers= headers
  @agent.headers = headers
end

#post(url, params = {}, headers = {}) ⇒ Object

HTTP::POST request

Parameters:

  • url

    Resource link

  • params (defaults to: {})

    Request post data

  • headers (defaults to: {})

    Request header



63
64
65
# File 'lib/tiny_grabber.rb', line 63

def post url, params = {}, headers = {}
  @agent.fetch url, :post, headers, params
end

#proxyObject

Read PROXY agent attribute



130
131
132
# File 'lib/tiny_grabber.rb', line 130

def proxy
  @agent.proxy
end

#proxy=(proxy) ⇒ Object

Set PROXY agent attribute

Parameters:

  • proxy

    Proxy configuration



139
140
141
# File 'lib/tiny_grabber.rb', line 139

def proxy= proxy
  @agent.proxy = proxy
end

#read_timeoutObject

Read READ_TIMEOUT agent attribute



98
99
100
# File 'lib/tiny_grabber.rb', line 98

def read_timeout
  @agent.read_timeout
end

#read_timeout=(read_timeout) ⇒ Object

Set READ_TIMEOUT agent attribute

Parameters:

  • read_timeout

    Waiting time to reading



107
108
109
# File 'lib/tiny_grabber.rb', line 107

def read_timeout= read_timeout
  @agent.read_timeout = read_timeout
end

#resetObject

Call RESET agent method



187
188
189
# File 'lib/tiny_grabber.rb', line 187

def reset
  @agent.reset
end

#user_agentObject

Read USER_AGENT agent attribute



114
115
116
# File 'lib/tiny_grabber.rb', line 114

def user_agent
  @agent.user_agent
end

#user_agent=(user_agent) ⇒ Object

Set USER_AGENT agent attribute

Parameters:

  • user_agent

    Web browser name



123
124
125
# File 'lib/tiny_grabber.rb', line 123

def user_agent= user_agent
  @agent.user_agent = user_agent
end