Module: RubyHackernews::MechanizeContext

Included in:
CommentService, EntryService, LoginService, PageFetcher, SignupService, TextService, UserInfoService, VotingService
Defined in:
lib/ruby-hackernews/services/mechanize_context.rb

Constant Summary collapse

@@contexts =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.agent=(key) ⇒ Object



7
8
9
# File 'lib/ruby-hackernews/services/mechanize_context.rb', line 7

def self.agent=(key)
  @@default = key
end

Instance Method Details

#[](key) ⇒ Object



17
18
19
# File 'lib/ruby-hackernews/services/mechanize_context.rb', line 17

def [](key)
  return @@contexts[key]
end

#agentObject



11
12
13
14
15
# File 'lib/ruby-hackernews/services/mechanize_context.rb', line 11

def agent
  @@default ||= :default
  @@contexts[@@default] = Mechanize.new unless @@contexts[@@default]
  return @@contexts[@@default]
end

#authenticated?(key = :default) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/ruby-hackernews/services/mechanize_context.rb', line 25

def authenticated?(key = :default)
  return @@contexts[key] && @@contexts[key].cookie_jar.jar.any?
end

#require_authenticationObject



21
22
23
# File 'lib/ruby-hackernews/services/mechanize_context.rb', line 21

def require_authentication
  raise NotAuthenticatedError unless authenticated?
end