Class: FlowcommerceSpree::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/flowcommerce_spree/session.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ip:, visitor:, experience: nil) ⇒ Session

Returns a new instance of Session.



14
15
16
17
18
19
20
# File 'lib/flowcommerce_spree/session.rb', line 14

def initialize(ip:, visitor:, experience: nil)
  ip = '127.0.0.1' if ip == '::1'

  @ip      = ip
  @visitor = visitor
  @experience = experience
end

Instance Attribute Details

#localizedObject

Returns the value of attribute localized.



6
7
8
# File 'lib/flowcommerce_spree/session.rb', line 6

def localized
  @localized
end

#sessionObject

Returns the value of attribute session.



6
7
8
# File 'lib/flowcommerce_spree/session.rb', line 6

def session
  @session
end

#visitorObject

Returns the value of attribute visitor.



6
7
8
# File 'lib/flowcommerce_spree/session.rb', line 6

def visitor
  @visitor
end

Class Method Details

.create(ip:, visitor:, experience: nil) ⇒ Object



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

def self.create(ip:, visitor:, experience: nil)
  instance = new(ip: ip, visitor: visitor, experience: experience)
  instance.create
  instance
end

Instance Method Details

#createObject

create session without or with experience (the latter is useful for creating a new session with the order’s experience on refreshing the checkout_token)



24
25
26
27
28
29
30
31
32
33
# File 'lib/flowcommerce_spree/session.rb', line 24

def create
  data = { ip: @ip,
           visit: { id: @visitor,
                    expires_at: (Time.now + 30.minutes).iso8601 } }
  data[:experience] = @experience if @experience

  session_model = ::Io::Flow::V0::Models::SessionForm.new data
  @session = FlowCommerce.instance(http_handler: LoggingHttpHandler.new)
                         .sessions.post_organizations_by_organization(ORGANIZATION, session_model)
end

#experienceObject

get local experience or return nil



42
43
44
# File 'lib/flowcommerce_spree/session.rb', line 42

def experience
  @session.local&.experience
end

#expires_atObject



46
47
48
# File 'lib/flowcommerce_spree/session.rb', line 46

def expires_at
  @session.visit.expires_at
end

#idObject



54
55
56
# File 'lib/flowcommerce_spree/session.rb', line 54

def id
  @session.id
end

#localObject



50
51
52
# File 'lib/flowcommerce_spree/session.rb', line 50

def local
  @session.local
end

#update(data) ⇒ Object

if we want to manually switch to specific country or experience



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

def update(data)
  @session = FlowCommerce.instance.sessions.put_by_session(@session.id,
                                                           ::Io::Flow::V0::Models::SessionPutForm.new(data))
end