Class: Percy::Client::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/percy/client/resources.rb

Overview

A simple data container object used to pass data to create_snapshot.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_url, options = {}) ⇒ Resource

Returns a new instance of Resource.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/percy/client/resources.rb', line 17

def initialize(resource_url, options = {})
  @resource_url = resource_url

  if !options[:sha] && !options[:content]
    raise ArgumentError, 'Either "sha" or "content" must be given.'
  end
  @sha = options[:sha] || Digest::SHA256.hexdigest(options[:content])
  @content = options[:content]

  @is_root = options[:is_root]
  @root_for_width = options[:root_for_width]
  @mimetype = options[:mimetype]

  # For optional convenience of temporarily storing the local content and path with this
  # object. These are never included when serialized.
  @content = options[:content]
  @path = options[:path]
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



14
15
16
# File 'lib/percy/client/resources.rb', line 14

def content
  @content
end

#is_rootObject

Returns the value of attribute is_root.



11
12
13
# File 'lib/percy/client/resources.rb', line 11

def is_root
  @is_root
end

#mimetypeObject

Returns the value of attribute mimetype.



13
14
15
# File 'lib/percy/client/resources.rb', line 13

def mimetype
  @mimetype
end

#pathObject

Returns the value of attribute path.



15
16
17
# File 'lib/percy/client/resources.rb', line 15

def path
  @path
end

#resource_urlObject

Returns the value of attribute resource_url.



10
11
12
# File 'lib/percy/client/resources.rb', line 10

def resource_url
  @resource_url
end

#root_for_widthObject

Returns the value of attribute root_for_width.



12
13
14
# File 'lib/percy/client/resources.rb', line 12

def root_for_width
  @root_for_width
end

#shaObject

Returns the value of attribute sha.



9
10
11
# File 'lib/percy/client/resources.rb', line 9

def sha
  @sha
end

Instance Method Details

#==(other) ⇒ Object



49
50
51
52
53
54
# File 'lib/percy/client/resources.rb', line 49

def ==(other)
  other.is_a?(self.class) &&
    other.sha == sha &&
    other.resource_url == resource_url &&
    other.mimetype == mimetype
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/percy/client/resources.rb', line 60

def eql?(other)
  self == other && hash == other.hash
end

#hashObject



56
57
58
# File 'lib/percy/client/resources.rb', line 56

def hash
  [sha, resource_url, mimetype].hash
end

#inspectObject Also known as: to_s



64
65
66
67
# File 'lib/percy/client/resources.rb', line 64

def inspect
  content_msg = content.nil? ? '' : "content.length: #{content.length}"
  "<Resource #{sha} #{resource_url} root_for_width:#{root_for_width} is_root:#{!!is_root} #{mimetype} #{content_msg}>"
end

#serializeObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/percy/client/resources.rb', line 36

def serialize
  {
    'type' => 'resources',
    'id' => sha,
    'attributes' => {
      'resource-url' => Addressable::URI.escape(resource_url),
      'mimetype' => mimetype,
      'is-root' => is_root,
      'for-widths' => root_for_width ? [root_for_width] : nil,
    },
  }
end