Class: TypePadTemplate::Account

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(login_cookies = nil) ⇒ Account

Returns a new instance of Account.



13
14
15
# File 'lib/type_pad_template/account.rb', line 13

def initialize( = nil)
  @login_cookies = 
end

Instance Attribute Details

#login_cookiesObject (readonly)

Returns the value of attribute login_cookies.



11
12
13
# File 'lib/type_pad_template/account.rb', line 11

def 
  @login_cookies
end

Class Method Details

.login(username, password) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/type_pad_template/account.rb', line 3

def self.(username, password)
   = new.tap do |a|
    a.(username, password)
  end

   if .logged_in?
end

Instance Method Details

#blogsObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/type_pad_template/account.rb', line 41

def blogs
  return [] unless logged_in?

  request("/dashboard") do |response|
    response.
    doc.
    search("//ul[@id='blogs-list']//a[@class='blog-name']").
    map do |element|
      if %r{/([0-9a-f]+)/dashboard$} === element["href"]
        id = $1
        Blog.new(self, id, element.text)
      end
    end.
    compact
  end
end

#logged_in?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/type_pad_template/account.rb', line 58

def logged_in?
  !@login_cookies.empty?
end

#login(username, password) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/type_pad_template/account.rb', line 17

def (username, password)
  form_element = Request.new("/secure/services/signin", :ssl => true).
    dispatch.
    response.
    doc.
    at("//form[@id='signin-form-typepad']")

  form = Form.new(form_element).tap do |f|
    f[:username] = username
    f[:password] = password
  end

  @login_cookies = Request.new("/secure/services/signin/save", {
    :ssl => true,
    :method => :post,
    :params => form.to_hash
  }).
    dispatch.
    response.
    cookies

  self
end

#request(path, options = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/type_pad_template/account.rb', line 62

def request(path, options = {})
  # FIXME raise a proper exception instead.
  return nil unless logged_in?

  request = Request.new(path, (options))

  if block_given?
    yield request.
      dispatch.
      response
  else
    request
  end
end