Class: Resizing::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/resizing/configuration.rb

Overview

Configuration class for Resizing client

– usage.

options = {
  host: 'https://www.resizing.net',
  project_id: '098a2a0d-0000-0000-0000-000000000000',
  secret_token: '4g1cshg......rbs6'
}
configuration = Resizing::Configuration.new(options)
Resizing::Client.new(configuration)

++

Constant Summary collapse

DEFAULT_HOST =
'https://img.resizing.net'
DEFAULT_IMAGE_HOST =
'https://img.resizing.net'
DEFAULT_VIDEO_HOST =
'https://video.resizing.net'
DEFAULT_OPEN_TIMEOUT =
2
DEFAULT_RESPONSE_TIMEOUT =
10
TRANSFORM_OPTIONS =
%i[w width h height f format c crop q quality].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*attrs) ⇒ Configuration

Returns a new instance of Configuration.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/resizing/configuration.rb', line 27

def initialize(*attrs)
  case attr = attrs.first
  when Hash
    if attr[:project_id].nil? || attr[:secret_token].nil?
      raise_configiration_error
    end
    if attr[:host].present?
      raise_configiration_error
    end

    initialize_by_hash attr
    return
  end

  raise_configiration_error
end

Instance Attribute Details

#enable_mockObject (readonly)

Returns the value of attribute enable_mock.



18
19
20
# File 'lib/resizing/configuration.rb', line 18

def enable_mock
  @enable_mock
end

#image_hostObject (readonly)

Returns the value of attribute image_host.



18
19
20
# File 'lib/resizing/configuration.rb', line 18

def image_host
  @image_host
end

#open_timeoutObject (readonly)

Returns the value of attribute open_timeout.



18
19
20
# File 'lib/resizing/configuration.rb', line 18

def open_timeout
  @open_timeout
end

#project_idObject (readonly)

Returns the value of attribute project_id.



18
19
20
# File 'lib/resizing/configuration.rb', line 18

def project_id
  @project_id
end

#response_timeoutObject (readonly)

Returns the value of attribute response_timeout.



18
19
20
# File 'lib/resizing/configuration.rb', line 18

def response_timeout
  @response_timeout
end

#secret_tokenObject (readonly)

Returns the value of attribute secret_token.



18
19
20
# File 'lib/resizing/configuration.rb', line 18

def secret_token
  @secret_token
end

#video_hostObject (readonly)

Returns the value of attribute video_host.



18
19
20
# File 'lib/resizing/configuration.rb', line 18

def video_host
  @video_host
end

Instance Method Details

#==(other) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/resizing/configuration.rb', line 91

def ==(other)
  return false unless self.class == other.class

  %i[image_host video_host project_id secret_token open_timeout response_timeout].all? do |name|
    send(name) == other.send(name)
  end
end

#generate_auth_headerObject



49
50
51
52
53
54
55
# File 'lib/resizing/configuration.rb', line 49

def generate_auth_header
  current_timestamp = Time.now.to_i
  data = [current_timestamp, secret_token].join('|')
  token = Digest::SHA2.hexdigest(data)
  version = 'v1'
  [version, current_timestamp, token].join(',')
end

#generate_identifierObject

たぶんここにおくものではない もしくはキャッシュしない



83
84
85
# File 'lib/resizing/configuration.rb', line 83

def generate_identifier
  "/projects/#{project_id}/upload/images/#{generate_image_id}"
end

#generate_image_idObject



87
88
89
# File 'lib/resizing/configuration.rb', line 87

def generate_image_id
  ::SecureRandom.uuid
end

#generate_image_url(image_id, version_id = nil, transforms = []) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/resizing/configuration.rb', line 57

def generate_image_url(image_id, version_id = nil, transforms = [])
  path = transformation_path(transforms)
  version = if version_id.nil?
              nil
            else
              "v#{version_id}"
            end

  parts = []
  parts << image_id
  parts << version if version
  parts << path unless path.empty?
  "#{image_host}/projects/#{project_id}/upload/images/#{parts.join('/')}"
end

#hostObject



44
45
46
47
# File 'lib/resizing/configuration.rb', line 44

def host
  Kernel.warn "[DEPRECATED] The Configuration#host is deprecated. Use Configuration#image_host."
  self.image_host
end

#transformation_path(transformations) ⇒ Object

this method should be divided other class



73
74
75
76
77
78
79
# File 'lib/resizing/configuration.rb', line 73

def transformation_path(transformations)
  transformations = [transformations] if transformations.is_a? Hash

  transformations.map do |transform|
    transform.slice(*TRANSFORM_OPTIONS).map { |key, value| [key, value].join('_') }.join(',')
  end.join('/')
end