Class: CycleHire::Session

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/cycle_hire/session.rb

Defined Under Namespace

Classes: AuthenticationError

Instance Method Summary collapse

Constructor Details

#initialize(email, password) ⇒ Session

Returns a new instance of Session.



9
10
11
12
# File 'lib/cycle_hire/session.rb', line 9

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

Instance Method Details

#authenticate!Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cycle_hire/session.rb', line 14

def authenticate!
  # initial request to get csrf
  csrf_response = make_request(:get, '/')
  doc = Nokogiri::HTML(csrf_response.body)
  csrf = doc.xpath("//input[@id='login__csrf_token']").first.attribute('value').value

  # authentication request
  options = {
    :query => {
      'login[Email]' => @email,
      'login[Password]' => @password,
      'login[_csrf_token]' => csrf
    },
    :headers => {
      'Content-Type' => 'application/x-www-form-urlencoded',
    }
  }
  authentication_response = make_request(:post, '/', options)
  raise AuthenticationError unless authentication_response.body =~ /Account Summary/
  self
end

#journeysObject



36
37
38
39
40
# File 'lib/cycle_hire/session.rb', line 36

def journeys
  response = make_request(:get, '/account/activity')
  parser = CycleHire::JourneyParser.new(response.body)
  parser.parse
end