Class: SolidusGraphqlApi::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/solidus_graphql_api/context.rb

Constant Summary collapse

AUTHORIZATION_HEADER =
"Authorization"
TOKEN_PATTERN =
/^Bearer (?<token>.*)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request:) ⇒ Context

Returns a new instance of Context.



10
11
12
13
# File 'lib/solidus_graphql_api/context.rb', line 10

def initialize(request:)
  @request = request
  @headers = request.headers
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



8
9
10
# File 'lib/solidus_graphql_api/context.rb', line 8

def headers
  @headers
end

#requestObject (readonly)

Returns the value of attribute request.



8
9
10
# File 'lib/solidus_graphql_api/context.rb', line 8

def request
  @request
end

Instance Method Details

#current_abilityObject



32
33
34
# File 'lib/solidus_graphql_api/context.rb', line 32

def current_ability
  @current_ability ||= Spree::Ability.new(current_user)
end

#current_orderObject



44
45
46
47
48
# File 'lib/solidus_graphql_api/context.rb', line 44

def current_order
  return @current_order if instance_variable_defined?(:@current_order)

  @current_order = current_order_by_current_user || current_order_by_guest_token
end

#current_pricing_optionsObject



50
51
52
# File 'lib/solidus_graphql_api/context.rb', line 50

def current_pricing_options
  @current_pricing_options ||= pricing_options_from_context
end

#current_storeObject



40
41
42
# File 'lib/solidus_graphql_api/context.rb', line 40

def current_store
  @current_store ||= Spree::Config.current_store_selector_class.new(request).store
end

#current_userObject



24
25
26
27
28
29
30
# File 'lib/solidus_graphql_api/context.rb', line 24

def current_user
  @current_user ||= begin
    return nil if bearer_token.blank?

    Spree.user_class.find_by(spree_api_key: bearer_token)
  end
end

#order_tokenObject



36
37
38
# File 'lib/solidus_graphql_api/context.rb', line 36

def order_token
  @order_token ||= headers["X-Spree-Order-Token"]
end

#to_hObject



15
16
17
18
19
20
21
22
# File 'lib/solidus_graphql_api/context.rb', line 15

def to_h
  { current_user: current_user,
    current_ability: current_ability,
    current_store: current_store,
    current_order: current_order,
    current_pricing_options: current_pricing_options,
    order_token: order_token }
end