Class: FreightKit::Credential

Inherits:
Model
  • Object
show all
Defined in:
lib/freight_kit/models/credential.rb

Overview

Class representing credentials.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#attributes

Constructor Details

#initialize(hash) ⇒ Credential

Returns a new instance of Credential.

Other than the following, instance ‘:attr_reader`s are generated dynamically based on keys.

Parameters:

  • type (Symbol)

    One of ‘:api`, `:website`

  • base_url (String)

    Required when type is ‘:selenoid`

  • username (String)

    Required when type is one of ‘:api`, `:website`

  • password (String)

    Required when type is one of ‘:api`, `:website`

  • access_token (String)

    Required when type is ‘:oauth2`

  • expires_at (DateTime)

    Required when type is ‘:oauth2`

  • scope (String)

    Required when type is ‘:oauth2`

  • proxy_url (String)

    Required when type is ‘:api_proxy`

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/freight_kit/models/credential.rb', line 45

def initialize(hash)
  raise ArgumentError, 'Credential#new: `type` cannot be blank' if hash[:type].blank?

  type = hash[:type]

  requirements = case type
                 when :api_key
                   { api_key: String }
                 when :api, :website
                   { password: String }
                 when :api_proxy
                   { api_key: String, proxy_url: String }
                 when :oauth2
                   { access_token: String, expires_at: ::DateTime, scope: String }
                 when :selenoid
                   { base_url: URI, browser: Symbol }
                 else
                   {}
                 end

  requirements.each_key do |k|
    raise ArgumentError, "Credential#new: `#{k}` cannot be blank" if hash[k].blank?

    unless hash[k].is_a?(requirements[k])
      raise ArgumentError, "Credential#new: `#{k}` must be a #{requirements[k]}, got #{hash[k].class}"
    end
  end

  hash.each do |k, _v|
    next if k == :type

    singleton_class.class_eval { attr_accessor(k.to_s) } unless singleton_class.respond_to?(k)
  end

  super
end

Instance Attribute Details

#access_tokenString

Access token when type is ‘:oauth2`

Returns:

  • (String)

    Access token



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/freight_kit/models/credential.rb', line 30

class Credential < Model
  attr_accessor :type

  # Returns a new instance of Credential.
  #
  # Other than the following, instance `:attr_reader`s are generated dynamically based on keys.
  #
  # @param [Symbol] type One of `:api`, `:website`
  # @param [String] base_url Required when type is `:selenoid`
  # @param [String] username Required when type is one of `:api`, `:website`
  # @param [String] password Required when type is one of `:api`, `:website`
  # @param [String] access_token Required when type is `:oauth2`
  # @param [DateTime] expires_at Required when type is `:oauth2`
  # @param [String] scope Required when type is `:oauth2`
  # @param [String] proxy_url Required when type is `:api_proxy`
  def initialize(hash)
    raise ArgumentError, 'Credential#new: `type` cannot be blank' if hash[:type].blank?

    type = hash[:type]

    requirements = case type
                   when :api_key
                     { api_key: String }
                   when :api, :website
                     { password: String }
                   when :api_proxy
                     { api_key: String, proxy_url: String }
                   when :oauth2
                     { access_token: String, expires_at: ::DateTime, scope: String }
                   when :selenoid
                     { base_url: URI, browser: Symbol }
                   else
                     {}
                   end

    requirements.each_key do |k|
      raise ArgumentError, "Credential#new: `#{k}` cannot be blank" if hash[k].blank?

      unless hash[k].is_a?(requirements[k])
        raise ArgumentError, "Credential#new: `#{k}` must be a #{requirements[k]}, got #{hash[k].class}"
      end
    end

    hash.each do |k, _v|
      next if k == :type

      singleton_class.class_eval { attr_accessor(k.to_s) } unless singleton_class.respond_to?(k)
    end

    super
  end

  def selenoid_options
    return unless type == :selenoid
    return @selenoid_options if @selenoid_options.present?

    download_url = base_url.dup
    download_url.path = '/download'
    download_url = download_url.to_s

    @selenoid_options = { download_url: }
  end

  def watir_args
    return unless type == :selenoid
    return @watir_args if @watir_args.present?

    url = base_url.dup
    url.path = '/wd/hub/'
    url = url.to_s

    @watir_args = [
                    browser,
                    {
                      options: {
                                 prefs: {
                                          download: {
                                                      directory_upgrade: true,
                                                      prompt_for_download: false
                                                    }
                                        }
                               },
                      url:
                    },
                  ]
  end
end

#expires_atDateTime

Token expiration date/time when type is ‘:oauth2`

Returns:

  • (DateTime)

    Token expiration date/time



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/freight_kit/models/credential.rb', line 30

class Credential < Model
  attr_accessor :type

  # Returns a new instance of Credential.
  #
  # Other than the following, instance `:attr_reader`s are generated dynamically based on keys.
  #
  # @param [Symbol] type One of `:api`, `:website`
  # @param [String] base_url Required when type is `:selenoid`
  # @param [String] username Required when type is one of `:api`, `:website`
  # @param [String] password Required when type is one of `:api`, `:website`
  # @param [String] access_token Required when type is `:oauth2`
  # @param [DateTime] expires_at Required when type is `:oauth2`
  # @param [String] scope Required when type is `:oauth2`
  # @param [String] proxy_url Required when type is `:api_proxy`
  def initialize(hash)
    raise ArgumentError, 'Credential#new: `type` cannot be blank' if hash[:type].blank?

    type = hash[:type]

    requirements = case type
                   when :api_key
                     { api_key: String }
                   when :api, :website
                     { password: String }
                   when :api_proxy
                     { api_key: String, proxy_url: String }
                   when :oauth2
                     { access_token: String, expires_at: ::DateTime, scope: String }
                   when :selenoid
                     { base_url: URI, browser: Symbol }
                   else
                     {}
                   end

    requirements.each_key do |k|
      raise ArgumentError, "Credential#new: `#{k}` cannot be blank" if hash[k].blank?

      unless hash[k].is_a?(requirements[k])
        raise ArgumentError, "Credential#new: `#{k}` must be a #{requirements[k]}, got #{hash[k].class}"
      end
    end

    hash.each do |k, _v|
      next if k == :type

      singleton_class.class_eval { attr_accessor(k.to_s) } unless singleton_class.respond_to?(k)
    end

    super
  end

  def selenoid_options
    return unless type == :selenoid
    return @selenoid_options if @selenoid_options.present?

    download_url = base_url.dup
    download_url.path = '/download'
    download_url = download_url.to_s

    @selenoid_options = { download_url: }
  end

  def watir_args
    return unless type == :selenoid
    return @watir_args if @watir_args.present?

    url = base_url.dup
    url.path = '/wd/hub/'
    url = url.to_s

    @watir_args = [
                    browser,
                    {
                      options: {
                                 prefs: {
                                          download: {
                                                      directory_upgrade: true,
                                                      prompt_for_download: false
                                                    }
                                        }
                               },
                      url:
                    },
                  ]
  end
end

#kindSymbol

What kind?

Returns:

  • (Symbol)

    One of ‘:api`, `:api_key`, `:oauth2`, `:website`



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/freight_kit/models/credential.rb', line 30

class Credential < Model
  attr_accessor :type

  # Returns a new instance of Credential.
  #
  # Other than the following, instance `:attr_reader`s are generated dynamically based on keys.
  #
  # @param [Symbol] type One of `:api`, `:website`
  # @param [String] base_url Required when type is `:selenoid`
  # @param [String] username Required when type is one of `:api`, `:website`
  # @param [String] password Required when type is one of `:api`, `:website`
  # @param [String] access_token Required when type is `:oauth2`
  # @param [DateTime] expires_at Required when type is `:oauth2`
  # @param [String] scope Required when type is `:oauth2`
  # @param [String] proxy_url Required when type is `:api_proxy`
  def initialize(hash)
    raise ArgumentError, 'Credential#new: `type` cannot be blank' if hash[:type].blank?

    type = hash[:type]

    requirements = case type
                   when :api_key
                     { api_key: String }
                   when :api, :website
                     { password: String }
                   when :api_proxy
                     { api_key: String, proxy_url: String }
                   when :oauth2
                     { access_token: String, expires_at: ::DateTime, scope: String }
                   when :selenoid
                     { base_url: URI, browser: Symbol }
                   else
                     {}
                   end

    requirements.each_key do |k|
      raise ArgumentError, "Credential#new: `#{k}` cannot be blank" if hash[k].blank?

      unless hash[k].is_a?(requirements[k])
        raise ArgumentError, "Credential#new: `#{k}` must be a #{requirements[k]}, got #{hash[k].class}"
      end
    end

    hash.each do |k, _v|
      next if k == :type

      singleton_class.class_eval { attr_accessor(k.to_s) } unless singleton_class.respond_to?(k)
    end

    super
  end

  def selenoid_options
    return unless type == :selenoid
    return @selenoid_options if @selenoid_options.present?

    download_url = base_url.dup
    download_url.path = '/download'
    download_url = download_url.to_s

    @selenoid_options = { download_url: }
  end

  def watir_args
    return unless type == :selenoid
    return @watir_args if @watir_args.present?

    url = base_url.dup
    url.path = '/wd/hub/'
    url = url.to_s

    @watir_args = [
                    browser,
                    {
                      options: {
                                 prefs: {
                                          download: {
                                                      directory_upgrade: true,
                                                      prompt_for_download: false
                                                    }
                                        }
                               },
                      url:
                    },
                  ]
  end
end

#passwordString

Password when type is one of ‘:api`, `:website`

Returns:

  • (String)

    Username



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/freight_kit/models/credential.rb', line 30

class Credential < Model
  attr_accessor :type

  # Returns a new instance of Credential.
  #
  # Other than the following, instance `:attr_reader`s are generated dynamically based on keys.
  #
  # @param [Symbol] type One of `:api`, `:website`
  # @param [String] base_url Required when type is `:selenoid`
  # @param [String] username Required when type is one of `:api`, `:website`
  # @param [String] password Required when type is one of `:api`, `:website`
  # @param [String] access_token Required when type is `:oauth2`
  # @param [DateTime] expires_at Required when type is `:oauth2`
  # @param [String] scope Required when type is `:oauth2`
  # @param [String] proxy_url Required when type is `:api_proxy`
  def initialize(hash)
    raise ArgumentError, 'Credential#new: `type` cannot be blank' if hash[:type].blank?

    type = hash[:type]

    requirements = case type
                   when :api_key
                     { api_key: String }
                   when :api, :website
                     { password: String }
                   when :api_proxy
                     { api_key: String, proxy_url: String }
                   when :oauth2
                     { access_token: String, expires_at: ::DateTime, scope: String }
                   when :selenoid
                     { base_url: URI, browser: Symbol }
                   else
                     {}
                   end

    requirements.each_key do |k|
      raise ArgumentError, "Credential#new: `#{k}` cannot be blank" if hash[k].blank?

      unless hash[k].is_a?(requirements[k])
        raise ArgumentError, "Credential#new: `#{k}` must be a #{requirements[k]}, got #{hash[k].class}"
      end
    end

    hash.each do |k, _v|
      next if k == :type

      singleton_class.class_eval { attr_accessor(k.to_s) } unless singleton_class.respond_to?(k)
    end

    super
  end

  def selenoid_options
    return unless type == :selenoid
    return @selenoid_options if @selenoid_options.present?

    download_url = base_url.dup
    download_url.path = '/download'
    download_url = download_url.to_s

    @selenoid_options = { download_url: }
  end

  def watir_args
    return unless type == :selenoid
    return @watir_args if @watir_args.present?

    url = base_url.dup
    url.path = '/wd/hub/'
    url = url.to_s

    @watir_args = [
                    browser,
                    {
                      options: {
                                 prefs: {
                                          download: {
                                                      directory_upgrade: true,
                                                      prompt_for_download: false
                                                    }
                                        }
                               },
                      url:
                    },
                  ]
  end
end

#scopeString

Scope when type is ‘:oauth2`

Returns:

  • (String)

    Scope



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/freight_kit/models/credential.rb', line 30

class Credential < Model
  attr_accessor :type

  # Returns a new instance of Credential.
  #
  # Other than the following, instance `:attr_reader`s are generated dynamically based on keys.
  #
  # @param [Symbol] type One of `:api`, `:website`
  # @param [String] base_url Required when type is `:selenoid`
  # @param [String] username Required when type is one of `:api`, `:website`
  # @param [String] password Required when type is one of `:api`, `:website`
  # @param [String] access_token Required when type is `:oauth2`
  # @param [DateTime] expires_at Required when type is `:oauth2`
  # @param [String] scope Required when type is `:oauth2`
  # @param [String] proxy_url Required when type is `:api_proxy`
  def initialize(hash)
    raise ArgumentError, 'Credential#new: `type` cannot be blank' if hash[:type].blank?

    type = hash[:type]

    requirements = case type
                   when :api_key
                     { api_key: String }
                   when :api, :website
                     { password: String }
                   when :api_proxy
                     { api_key: String, proxy_url: String }
                   when :oauth2
                     { access_token: String, expires_at: ::DateTime, scope: String }
                   when :selenoid
                     { base_url: URI, browser: Symbol }
                   else
                     {}
                   end

    requirements.each_key do |k|
      raise ArgumentError, "Credential#new: `#{k}` cannot be blank" if hash[k].blank?

      unless hash[k].is_a?(requirements[k])
        raise ArgumentError, "Credential#new: `#{k}` must be a #{requirements[k]}, got #{hash[k].class}"
      end
    end

    hash.each do |k, _v|
      next if k == :type

      singleton_class.class_eval { attr_accessor(k.to_s) } unless singleton_class.respond_to?(k)
    end

    super
  end

  def selenoid_options
    return unless type == :selenoid
    return @selenoid_options if @selenoid_options.present?

    download_url = base_url.dup
    download_url.path = '/download'
    download_url = download_url.to_s

    @selenoid_options = { download_url: }
  end

  def watir_args
    return unless type == :selenoid
    return @watir_args if @watir_args.present?

    url = base_url.dup
    url.path = '/wd/hub/'
    url = url.to_s

    @watir_args = [
                    browser,
                    {
                      options: {
                                 prefs: {
                                          download: {
                                                      directory_upgrade: true,
                                                      prompt_for_download: false
                                                    }
                                        }
                               },
                      url:
                    },
                  ]
  end
end

#typeObject

Returns the value of attribute type.



31
32
33
# File 'lib/freight_kit/models/credential.rb', line 31

def type
  @type
end

#usernameString

Username when type is one of ‘:api`, `:website`

Returns:

  • (String)

    Username



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/freight_kit/models/credential.rb', line 30

class Credential < Model
  attr_accessor :type

  # Returns a new instance of Credential.
  #
  # Other than the following, instance `:attr_reader`s are generated dynamically based on keys.
  #
  # @param [Symbol] type One of `:api`, `:website`
  # @param [String] base_url Required when type is `:selenoid`
  # @param [String] username Required when type is one of `:api`, `:website`
  # @param [String] password Required when type is one of `:api`, `:website`
  # @param [String] access_token Required when type is `:oauth2`
  # @param [DateTime] expires_at Required when type is `:oauth2`
  # @param [String] scope Required when type is `:oauth2`
  # @param [String] proxy_url Required when type is `:api_proxy`
  def initialize(hash)
    raise ArgumentError, 'Credential#new: `type` cannot be blank' if hash[:type].blank?

    type = hash[:type]

    requirements = case type
                   when :api_key
                     { api_key: String }
                   when :api, :website
                     { password: String }
                   when :api_proxy
                     { api_key: String, proxy_url: String }
                   when :oauth2
                     { access_token: String, expires_at: ::DateTime, scope: String }
                   when :selenoid
                     { base_url: URI, browser: Symbol }
                   else
                     {}
                   end

    requirements.each_key do |k|
      raise ArgumentError, "Credential#new: `#{k}` cannot be blank" if hash[k].blank?

      unless hash[k].is_a?(requirements[k])
        raise ArgumentError, "Credential#new: `#{k}` must be a #{requirements[k]}, got #{hash[k].class}"
      end
    end

    hash.each do |k, _v|
      next if k == :type

      singleton_class.class_eval { attr_accessor(k.to_s) } unless singleton_class.respond_to?(k)
    end

    super
  end

  def selenoid_options
    return unless type == :selenoid
    return @selenoid_options if @selenoid_options.present?

    download_url = base_url.dup
    download_url.path = '/download'
    download_url = download_url.to_s

    @selenoid_options = { download_url: }
  end

  def watir_args
    return unless type == :selenoid
    return @watir_args if @watir_args.present?

    url = base_url.dup
    url.path = '/wd/hub/'
    url = url.to_s

    @watir_args = [
                    browser,
                    {
                      options: {
                                 prefs: {
                                          download: {
                                                      directory_upgrade: true,
                                                      prompt_for_download: false
                                                    }
                                        }
                               },
                      url:
                    },
                  ]
  end
end

Instance Method Details

#selenoid_optionsObject



82
83
84
85
86
87
88
89
90
91
# File 'lib/freight_kit/models/credential.rb', line 82

def selenoid_options
  return unless type == :selenoid
  return @selenoid_options if @selenoid_options.present?

  download_url = base_url.dup
  download_url.path = '/download'
  download_url = download_url.to_s

  @selenoid_options = { download_url: }
end

#watir_argsObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/freight_kit/models/credential.rb', line 93

def watir_args
  return unless type == :selenoid
  return @watir_args if @watir_args.present?

  url = base_url.dup
  url.path = '/wd/hub/'
  url = url.to_s

  @watir_args = [
                  browser,
                  {
                    options: {
                               prefs: {
                                        download: {
                                                    directory_upgrade: true,
                                                    prompt_for_download: false
                                                  }
                                      }
                             },
                    url:
                  },
                ]
end