Class: Purevolume::Client

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

Constant Summary collapse

SITE =
'http://www.purevolume.com'
LOGIN =
'/login'
POSTS =
'/dashboard?s=posts&tab='
ADD =
'add_post'
LIST =
'posts'
GENERAL =
'General'
SUCCESS =
'div.success_message'
DASHBOARD =
'/dashboard'

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Client

Returns a new instance of Client.



17
18
19
20
21
22
# File 'lib/purevolume.rb', line 17

def initialize username, password
  @agent      = WWW::Mechanize.new
  @username   = username
  @password   = password
  @login_pg = @add_post_pg = @dashboard  = @profile_url = @blog_url = @type = @valid = nil
end

Instance Method Details

#account_infoObject



55
56
57
58
59
60
61
62
63
# File 'lib/purevolume.rb', line 55

def 
  { "rsp" => { "site" => {
    "name"     => profile_name,
    "profile"  => profile_url,
    "type"     => profile_type,
    "blog"     => profile_blog_url },
    "stat"     => "ok" }
  } if @valid
end

#authenticateObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/purevolume.rb', line 28

def authenticate
  @login_pg || 
   = @login_pg.forms_with( :action => LOGIN ).first
  
  if 
    .username = @username
    .password = @password
    response_page       = @agent.submit(  )
    @dashboard          = response_page if response_page.uri.to_s == "#{SITE}#{DASHBOARD}"
  end
  @valid = can_post?
end

#post(title, body) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/purevolume.rb', line 41

def post title, body
  @add_post_pg || add_post_page
  post_form = @add_post_pg.forms_with( :action =>  "#{POSTS}#{ADD}" ).first
  
  if post_form
    category              = post_form.field_with( :name => 'category' )
    category.options.each { |opt| opt.select if opt.value == GENERAL } if category
    post_form.blog_title  = title
    post_form.blog_post   = body
    response              = !@agent.submit( post_form, post_form.buttons.first ).search( SUCCESS ).empty?
  end
  response ? success_response(title) : error_response(title)
end

#valid_user?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/purevolume.rb', line 24

def valid_user?
  @valid || authenticate
end