Class: Tendersync::Session
- Inherits:
-
Object
- Object
- Tendersync::Session
- Defined in:
- lib/tendersync/session.rb
Instance Attribute Summary collapse
-
#agent ⇒ Object
readonly
Returns the value of attribute agent.
Instance Method Summary collapse
-
#all_sections ⇒ Object
Return a hash of section id to section name.
- #create_document(section, permalink, body, title) ⇒ Object
-
#documents(section) ⇒ Object
Get the URL’s of documents in the given section.
- #edit_form_for(section, edit_page) ⇒ Object
- #edit_page_for(doc_url) ⇒ Object
- #get(url) ⇒ Object
- #get_section_id(section) ⇒ Object
-
#initialize(site, user, pass) ⇒ Session
constructor
A new instance of Session.
- #login ⇒ Object
-
#ls(*sections) ⇒ Object
Print out a list of all documents.
- #post(document) ⇒ Object
- #pull_from_tender(*sections) ⇒ Object
Constructor Details
#initialize(site, user, pass) ⇒ Session
Returns a new instance of Session.
3 4 5 6 7 8 9 |
# File 'lib/tendersync/session.rb', line 3 def initialize(site, user, pass) @username = user @password = pass @agent = Mechanize.new { |a| a.auth(user, pass) } @site = site @login_site = "#{site}/login" end |
Instance Attribute Details
#agent ⇒ Object (readonly)
Returns the value of attribute agent.
2 3 4 |
# File 'lib/tendersync/session.rb', line 2 def agent @agent end |
Instance Method Details
#all_sections ⇒ Object
Return a hash of section id to section name.
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/tendersync/session.rb', line 53 def all_sections sections = {} get("#{@site}/dashboard/sections").links.each do | link | if link.href =~ %r{/faqs/([^/]*)$} name = $1 sections[name] = link.text end end sections end |
#create_document(section, permalink, body, title) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/tendersync/session.rb', line 108 def create_document(section,permalink,body, title) login form = get("#{@site}/faq/new").form_with(:action => "/faqs") puts "Using default Title..." if title.nil? title = "New Tendersync::Document" if title.nil? document = Tendersync::Document.new( :section => section, :title => title, :permalink => permalink, :body => body ) document.to_form(form) return if $dry_run id = get_section_id(section) form.(:value => id).first.click form.submit Tendersync::Document.from_form(section,edit_page_for("#{@site}/faqs/#{section}/#{permalink}").form_with(:action => /edit/)) end |
#documents(section) ⇒ Object
Get the URL’s of documents in the given section.
42 43 44 45 46 |
# File 'lib/tendersync/session.rb', line 42 def documents(section) login index = get("#{@site}/faqs/#{section}") index.links_like(%r{faqs/#{section}/.+}).collect { |url|"#{@site}#{url}" } end |
#edit_form_for(section, edit_page) ⇒ Object
64 65 66 67 68 69 |
# File 'lib/tendersync/session.rb', line 64 def edit_form_for(section, edit_page) edit_page.forms.each do |form| document = Tendersync::Document.from_form(section,form) return document unless document.document_id.nil? end end |
#edit_page_for(doc_url) ⇒ Object
47 48 49 50 |
# File 'lib/tendersync/session.rb', line 47 def edit_page_for(doc_url) login get "#{@site}#{get(doc_url).links_like(%r{faqs/\d+/edit}).first}" end |
#get(url) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/tendersync/session.rb', line 27 def get(url) login begin page = @agent.get(url) rescue Mechanize::ResponseCodeError => e raise Tendersync::Runner::Error, "Unable to get #{url}" end def page.links_like(r) result = [] links.each { |l| result << l.href if l.href =~ r } result end page end |
#get_section_id(section) ⇒ Object
82 83 84 85 86 87 |
# File 'lib/tendersync/session.rb', line 82 def get_section_id(section) page = get("#{@site}/dashboard/sections/#{section}") form = page.form_with(:action => "/dashboard/sections/#{section}") form.form_node.to_s =~ /id="edit_section_(.*?)"/ $1 end |
#login ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/tendersync/session.rb', line 11 def login return if @logged_in puts "logging in to #{@login_site} as #{@username}..." page = @agent.get(@login_site) f = page.form_with(:action => '/login') do | login_form | login_form['email'] = @username login_form['password'] = @password end result = f.submit if result.links.find { | l | l.href =~ /forgot_password/} raise Tendersync::Runner::Error, "login failed--bad credentials" end # TODO Check the result for a valid login. @logged_in = true end |
#ls(*sections) ⇒ Object
Print out a list of all documents
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/tendersync/session.rb', line 90 def ls(*sections) sections = all_sections.keys if sections.empty? sections.each do | section | puts "Section #{section}" documents(section).map {|url| url =~ %r{/([^/]*)$} && $1 }.each do |link| puts " #{link}" end end end |
#post(document) ⇒ Object
100 101 102 103 104 105 106 107 |
# File 'lib/tendersync/session.rb', line 100 def post(document) login page = "#{@site}/faqs/#{document.section}/#{document.permalink}" form = edit_page_for(page).form_with(:action => /edit/) raise "Unable to load form for page: #{page}" unless form document.to_form(form) form.submit unless $dry_run end |
#pull_from_tender(*sections) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/tendersync/session.rb', line 70 def pull_from_tender(*sections) sections = all_sections.keys if sections.empty? for section in sections do documents(section).collect do |doc_url| edit_page = edit_page_for(doc_url) doc = edit_form_for(section, edit_page) puts " #{doc.permalink}" doc.save unless $dry_run end end end |