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(country:, visitor:, experience: nil) ⇒ Session

Returns a new instance of Session.



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

def initialize(country:, visitor:, experience: nil)
  @country = country
  @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(country:, visitor:, experience: nil) ⇒ Object



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

def self.create(country:, visitor:, experience: nil)
  instance = new(country: country, 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)



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

def create
  data = { country: @country,
           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



40
41
42
# File 'lib/flowcommerce_spree/session.rb', line 40

def experience
  @session.local&.experience
end

#expires_atObject



44
45
46
# File 'lib/flowcommerce_spree/session.rb', line 44

def expires_at
  @session.visit.expires_at
end

#idObject



52
53
54
# File 'lib/flowcommerce_spree/session.rb', line 52

def id
  @session.id
end

#localObject



48
49
50
# File 'lib/flowcommerce_spree/session.rb', line 48

def local
  @session.local
end

#update(data) ⇒ Object

if we want to manually switch to specific country or experience



34
35
36
37
# File 'lib/flowcommerce_spree/session.rb', line 34

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