• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Unleash / unleash-client-ruby / 11031869423

25 Sep 2024 11:14AM UTC coverage: 95.57% (-1.7%) from 97.25%
11031869423

Pull #204

github

web-flow
Merge 4cf325450 into d890ae8d3
Pull Request #204: docs: migration guide for v6

73 of 74 new or added lines in 8 files covered. (98.65%)

6 existing lines in 3 files now uncovered.

453 of 474 relevant lines covered (95.57%)

1377.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.87
/lib/unleash/context.rb
1
module Unleash
16✔
2
  class Context
16✔
3
    ATTRS = [:app_name, :environment, :user_id, :session_id, :remote_address, :current_time].freeze
16✔
4

5
    attr_accessor(*[ATTRS, :properties].flatten)
16✔
6

7
    def initialize(params = {})
16✔
8
      raise ArgumentError, "Unleash::Context must be initialized with a hash." unless params.is_a?(Hash)
3,840✔
9

10
      self.app_name = value_for("appName", params, Unleash&.configuration&.app_name)
3,840✔
11
      self.environment = value_for("environment", params, Unleash&.configuration&.environment || "default")
3,840✔
12
      self.user_id = value_for("userId", params)&.to_s
3,840✔
13
      self.session_id = value_for("sessionId", params)
3,840✔
14
      self.remote_address = value_for("remoteAddress", params)
3,840✔
15
      self.current_time = value_for("currentTime", params, Time.now.utc.iso8601.to_s)
3,840✔
16

17
      properties = value_for("properties", params)
3,840✔
18
      self.properties = properties.is_a?(Hash) ? properties.transform_keys(&:to_sym) : {}
3,840✔
19
    end
20

21
    def to_s
16✔
22
      "<Context: user_id=#{@user_id},session_id=#{@session_id},remote_address=#{@remote_address},properties=#{@properties}" \
2,800✔
23
      ",app_name=#{@app_name},environment=#{@environment},current_time=#{@current_time}>"
24
    end
25

26
    def as_json
16✔
27
      {
28
        appName: to_safe_value(self.app_name),
3,710✔
29
        environment: to_safe_value(self.environment),
462✔
30
        userId: to_safe_value(self.user_id),
462✔
31
        sessionId: to_safe_value(self.session_id),
462✔
32
        remoteAddress: to_safe_value(self.remote_address),
462✔
33
        currentTime: to_safe_value(self.current_time),
462✔
34
        properties: self.properties.transform_values{ |value| to_safe_value(value) }
1,840✔
35
      }
36
    end
37

38
    def to_json(*options)
16✔
39
      as_json(*options).to_json(*options)
3,712✔
40
    end
41

42
    def to_h
16✔
43
      ATTRS.map{ |attr| [attr, self.send(attr)] }.to_h.merge(properties: @properties)
112✔
44
    end
45

46
    # returns the value found for the key in the context, or raises a KeyError exception if not found.
47
    def get_by_name(name)
16✔
48
      normalized_name = underscore(name).to_sym
576✔
49

50
      if ATTRS.include? normalized_name
576✔
51
        self.send(normalized_name)
288✔
52
      else
53
        self.properties.fetch(normalized_name, nil) || self.properties.fetch(name.to_sym)
288✔
54
      end
55
    end
56

57
    def include?(name)
16✔
58
      normalized_name = underscore(name)
112✔
59
      return self.instance_variable_defined? "@#{normalized_name}" if ATTRS.include? normalized_name.to_sym
112✔
60

61
      self.properties.include?(normalized_name.to_sym) || self.properties.include?(name.to_sym)
80✔
62
    end
63

64
    private
16✔
65

66
    # Method to fetch values from hash for two types of keys: string in camelCase and symbol in snake_case
67
    def value_for(key, params, default_value = nil)
16✔
68
      params.values_at(key, key.to_sym, underscore(key), underscore(key).to_sym).compact.first || default_value
26,880✔
69
    end
70

71
    def to_safe_value(value)
16✔
72
      return nil if value.nil?
23,648✔
73

74
      if value.is_a?(Time)
10,192✔
NEW
75
        value.utc.iso8601
×
76
      else
77
        value.to_s
10,192✔
78
      end
79
    end
80

81
    # converts CamelCase to snake_case
82
    def underscore(camel_cased_word)
16✔
83
      camel_cased_word.to_s.gsub(/(.)([A-Z])/, '\1_\2').downcase
54,448✔
84
    end
85
  end
86
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc