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.



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

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]
  @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.



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

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.



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

def mimetype
  @mimetype
end

#pathObject

Returns the value of attribute path.



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

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

#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



46
47
48
49
50
51
# File 'lib/percy/client/resources.rb', line 46

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#hashObject



53
54
55
# File 'lib/percy/client/resources.rb', line 53

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

#inspectObject Also known as: to_s



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

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

#serializeObject



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

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