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.7'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTinyGrabber

Initialize a new TinyGrabber user agent.



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

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



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

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

.initialize(config = {}) ⇒ Object

Singleton > Initialize a new TinyGrabber user agent.



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

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



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

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



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

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

#cookiesObject

Read COOKIES agent attribute



163
164
165
# File 'lib/tiny_grabber.rb', line 163

def cookies
  @agent.cookies
end

#cookies=(cookies) ⇒ Object

Set COOKIES agent attribute

Parameters:

  • cookies

    Request cookies



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

def cookies= cookies
  @agent.cookies = cookies
end

#debug=(debug) ⇒ Object

Set DEBUG flag

Parameters:

  • debug

    Flag to start debug



82
83
84
# File 'lib/tiny_grabber.rb', line 82

def debug= debug
  @agent.debug = debug
end

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

HTTP::GET request

Parameters:

  • url

    Resource link

  • headers (defaults to: {})

    Request header



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

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

#headersObject

Read HEADERS agent attribute



147
148
149
# File 'lib/tiny_grabber.rb', line 147

def headers
  @agent.headers
end

#headers=(headers) ⇒ Object

Set HEADERS agent attribute

Parameters:

  • headers

    Request headers



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

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



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

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

#proxyObject

Read PROXY agent attribute



121
122
123
# File 'lib/tiny_grabber.rb', line 121

def proxy
  @agent.proxy
end

#proxy=(proxy) ⇒ Object

Set PROXY agent attribute

Parameters:

  • proxy

    Proxy configuration



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

def proxy= proxy
  @agent.proxy = proxy
end

#read_timeoutObject

Read READ_TIMEOUT agent attribute



89
90
91
# File 'lib/tiny_grabber.rb', line 89

def read_timeout
  @agent.read_timeout
end

#read_timeout=(read_timeout) ⇒ Object

Set READ_TIMEOUT agent attribute

Parameters:

  • read_timeout

    Waiting time to reading



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

def read_timeout= read_timeout
  @agent.read_timeout = read_timeout
end

#resetObject

Call RESET agent method



178
179
180
# File 'lib/tiny_grabber.rb', line 178

def reset
  @agent.reset
end

#user_agentObject

Read USER_AGENT agent attribute



105
106
107
# File 'lib/tiny_grabber.rb', line 105

def user_agent
  @agent.user_agent
end

#user_agent=(user_agent) ⇒ Object

Set USER_AGENT agent attribute

Parameters:

  • user_agent

    Web browser name



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

def user_agent= user_agent
  @agent.user_agent = user_agent
end

#verify_mode=(verify_mode) ⇒ Object

Set verify_mode

Parameters:

  • verify_mode

    SSL verify mode



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

def verify_mode= verify_mode
  @agent.verify_mode = verify_mode
end