Class: NationSync::Auth
- Inherits:
-
Object
- Object
- NationSync::Auth
- Defined in:
- lib/nationsync/auth.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#domain ⇒ Object
Returns the value of attribute domain.
-
#email ⇒ Object
Returns the value of attribute email.
-
#password ⇒ Object
Returns the value of attribute password.
-
#session_id ⇒ Object
readonly
Returns the value of attribute session_id.
Instance Method Summary collapse
- #authenticate! ⇒ Object
-
#initialize(domain, email, password) ⇒ Auth
constructor
A new instance of Auth.
Constructor Details
#initialize(domain, email, password) ⇒ Auth
Returns a new instance of Auth.
9 10 11 12 13 |
# File 'lib/nationsync/auth.rb', line 9 def initialize(domain, email, password) @domain = domain @email = email @password = password end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
6 7 8 |
# File 'lib/nationsync/auth.rb', line 6 def access_token @access_token end |
#domain ⇒ Object
Returns the value of attribute domain.
7 8 9 |
# File 'lib/nationsync/auth.rb', line 7 def domain @domain end |
#email ⇒ Object
Returns the value of attribute email.
7 8 9 |
# File 'lib/nationsync/auth.rb', line 7 def email @email end |
#password ⇒ Object
Returns the value of attribute password.
7 8 9 |
# File 'lib/nationsync/auth.rb', line 7 def password @password end |
#session_id ⇒ Object (readonly)
Returns the value of attribute session_id.
6 7 8 |
# File 'lib/nationsync/auth.rb', line 6 def session_id @session_id end |
Instance Method Details
#authenticate! ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/nationsync/auth.rb', line 15 def authenticate! agent = Mechanize.new # Disallow redirect agent.redirect_ok = false page = agent.get("https://#{@domain}.nationbuilder.com/admin/theme_tool_api/auth") signin = page.forms.select {|f| f.action == "/admin/theme_tool_api/auth" }.first email = signin.field "user_session[email]" password = signin.field "user_session[password]" email.value = @email password.value = @password # Submit the form resp = signin.submit() # Returns a redirect we have to follow to finish up the authorization. auth = agent.get resp.header["Location"] # Following the auth redirect we get another redirect to: # app://com.nationbuildertheme/index.html?code=[blah]" redirect = auth.header["Location"] # Extract the code from the redirect code = redirect.match(/code=([a-f0-9]+)/)[1] # And then exchange the code for an access_token. resp = HTTParty.get "https://southforward.nationbuilder.com/admin/theme_tool_api/exchange?code=#{code}" body = JSON.parse(resp.body) @access_token = body["access_token"] #puts "access_token: #{access_token}" # Also grab the session ID since we need that. @session_id = agent..select {|c| c.name == "_nbuild_session" }.first.value #puts "session_id: (_nbuild_session) #{session_id}" return true end |