Class: Smesser::Provider

Inherits:
Object
  • Object
show all
Defined in:
lib/smesser/provider.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Provider

Returns a new instance of Provider.



25
26
27
28
# File 'lib/smesser/provider.rb', line 25

def initialize(username, password)
  @username = username
  @password = password
end

Class Attribute Details

.descriptionObject

Returns the value of attribute description.



7
8
9
# File 'lib/smesser/provider.rb', line 7

def description
  @description
end

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



10
11
12
# File 'lib/smesser/provider.rb', line 10

def password
  @password
end

#usernameObject

Returns the value of attribute username.



10
11
12
# File 'lib/smesser/provider.rb', line 10

def username
  @username
end

Class Method Details

.load_allObject



16
17
18
19
20
21
22
23
# File 'lib/smesser/provider.rb', line 16

def self.load_all
  Smesser.log.debug("Loading all provider files in #{paths.inspect}")
  paths.each do |path|
    Dir["#{path}/*.rb"].each do |f|
      require f
    end
  end
end

.pathsObject



12
13
14
# File 'lib/smesser/provider.rb', line 12

def self.paths
  @paths ||= [File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'providers'))]
end

Instance Method Details

#agentObject



68
69
70
# File 'lib/smesser/provider.rb', line 68

def agent
  @agent ||= Mechanize.new
end

#click(spec) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/smesser/provider.rb', line 49

def click(spec)
  spec = { :text => spec } if spec.is_a? String
  log.debug("Looking for a form with #{spec.inspect}")
  link = page.link_with(spec)
  raise "Link not found: #{spec.inspect} on #{page.uri} (links: #{page.links.map(&:text).inspect})" unless link
  log.debug("Clicking link #{link.text} => #{link.href}")
  link.click
  log.debug("Clicked link #{link.href} (#{page.code})")
  page.code
end

#get(address) ⇒ Object



30
31
32
33
34
35
# File 'lib/smesser/provider.rb', line 30

def get(address)
  log.debug("Getting #{address}")
  agent.get(address) unless agent.page and agent.page.uri == URI.parse(address)
  log.debug("Got #{address} (#{page.code})")
  page.code
end

#logObject



76
77
78
# File 'lib/smesser/provider.rb', line 76

def log
  Smesser.log
end

#logged_in?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/smesser/provider.rb', line 80

def logged_in?
  false
end

#loginObject



60
61
62
# File 'lib/smesser/provider.rb', line 60

def 
  raise "You need to override #{this}#login"
end

#pageObject



72
73
74
# File 'lib/smesser/provider.rb', line 72

def page
  agent.page
end

#post_form(spec) {|form| ... } ⇒ Object

Yields:

  • (form)


37
38
39
40
41
42
43
44
45
46
47
# File 'lib/smesser/provider.rb', line 37

def post_form(spec, &block)
  spec = { :name => spec } if spec.is_a? String
  log.debug("Looking for form with #{spec.inspect}")
  form = page.form_with(spec)
  raise "Form not found: #{spec.inspect} on #{page.uri} (forms: #{page.forms.map(&:name).inspect})" unless form
  yield form
  log.debug("Submitting form: #{form.action} #{form.request_data}")
  form.submit
  log.debug("Submitted #{form.action} (#{page.code})")
  page.code
end

#send(message, *recipients) ⇒ Object



64
65
66
# File 'lib/smesser/provider.rb', line 64

def send(message, *recipients)
  raise "You need to override #{this}#send"
end