Class: LinkIO::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/linkio/request.rb

Overview

A framework-agnostic view of an incoming HTTP request, containing only what Client#handle_deep_link needs. Build one directly, from a Rack env with Request.from_rack, or let the Rails engine construct it for you.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(full_url:, client_ip:, user_agent: nil, query_params: {}, path_params: {}, device_id: nil) ⇒ Request

Returns a new instance of Request.

Parameters:

  • user_agent (String, nil) (defaults to: nil)
  • full_url (String)

    absolute request URL including query string

  • query_params (Hash) (defaults to: {})

    parsed query parameters

  • path_params (Hash) (defaults to: {})

    route/path parameters to merge into params

  • client_ip (String)

    resolved client IP (proxy-aware)

  • device_id (String, nil) (defaults to: nil)

    explicit device id (query "deviceId" or X-Device-Id)



16
17
18
19
20
21
22
23
# File 'lib/linkio/request.rb', line 16

def initialize(full_url:, client_ip:, user_agent: nil, query_params: {}, path_params: {}, device_id: nil)
  @user_agent = user_agent.to_s
  @full_url = full_url
  @query_params = stringify(query_params)
  @path_params = stringify(path_params)
  @client_ip = client_ip
  @device_id = device_id
end

Instance Attribute Details

#client_ipObject (readonly)

Returns the value of attribute client_ip.



8
9
10
# File 'lib/linkio/request.rb', line 8

def client_ip
  @client_ip
end

#device_idObject (readonly)

Returns the value of attribute device_id.



8
9
10
# File 'lib/linkio/request.rb', line 8

def device_id
  @device_id
end

#full_urlObject (readonly)

Returns the value of attribute full_url.



8
9
10
# File 'lib/linkio/request.rb', line 8

def full_url
  @full_url
end

#path_paramsObject (readonly)

Returns the value of attribute path_params.



8
9
10
# File 'lib/linkio/request.rb', line 8

def path_params
  @path_params
end

#query_paramsObject (readonly)

Returns the value of attribute query_params.



8
9
10
# File 'lib/linkio/request.rb', line 8

def query_params
  @query_params
end

#user_agentObject (readonly)

Returns the value of attribute user_agent.



8
9
10
# File 'lib/linkio/request.rb', line 8

def user_agent
  @user_agent
end

Class Method Details

.from_rack(env, path_params: {}) ⇒ LinkIO::Request

Build a Request from a Rack environment hash.

Parameters:

  • env (Hash)

    a Rack env

  • path_params (Hash) (defaults to: {})

    optional route params (e.g. from a router)

Returns:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/linkio/request.rb', line 40

def self.from_rack(env, path_params: {})
  user_agent = env["HTTP_USER_AGENT"]
  full_url = rack_full_url(env)
  query = Utils.parse_query_params(full_url)
  ip = Utils.client_ip(
    forwarded_for: env["HTTP_X_FORWARDED_FOR"],
    remote_address: env["REMOTE_ADDR"]
  )
  device_id = query["deviceId"] || env["HTTP_X_DEVICE_ID"]

  new(
    user_agent: user_agent,
    full_url: full_url,
    query_params: query,
    path_params: path_params,
    client_ip: ip,
    device_id: device_id
  )
end

Instance Method Details

#paramsHash{String => Object}

Merged query + path params (path params win), matching the Node backend's behaviour of overlaying req.params onto the parsed query params.

Returns:

  • (Hash{String => Object})


29
30
31
32
33
# File 'lib/linkio/request.rb', line 29

def params
  merged = query_params.dup
  path_params.each { |k, v| merged[k] = v unless v.nil? || v.to_s.empty? }
  merged
end