Module: Uploadcare::Parser

Defined in:
lib/uploadcare/utils/parser.rb

Defined Under Namespace

Classes: File, Group

Constant Summary collapse

META_URL =
/
  (?<uuid>[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12} # base uuid
  ~?(?<count>\d+)?) # optional count
  (?:\/-\/(?<operations>.*?))?\/?$ # optional operations
/ix

Class Method Summary collapse

Class Method Details

.parse(string) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/uploadcare/utils/parser.rb', line 41

def self.parse string
  matched = META_URL.match(string)

  # just a simple hash - easy to pass next
  captured = Hash[ matched.names.zip( matched.captures ) ]

  # raise an error if no uuid was given in the sting
  raise "Invalid UUID or url was given" if captured["uuid"].nil?

  # operations sring to array of operations
  if captured["operations"]
    captured["operations"] = captured["operations"].split("/-/")
  else
    captured["operations"] = []
  end

  # if count was given - it is a group
  if captured["count"]
    obj = Group.new captured
  else
    obj = File.new captured
  end
end

.parse_file_string(string) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/uploadcare/utils/parser.rb', line 12

def self.parse_file_string string
  result = Uploadcare::Parser.parse(string)

  unless result.is_a?(Uploadcare::Parser::File)
    msg = "invalid CDN URL or UUID was given for file: #{uuid_or_cdn_url}."
    if result.is_a?(Uploadcare::Parser::Group)
      msg = msg + "\n Group UUID was given. Try call @api.group if it is what you intended."
    end
    raise msg
  end

  result
end

.parse_group_string(string) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/uploadcare/utils/parser.rb', line 27

def self.parse_group_string string
  result = Uploadcare::Parser.parse(string)

  unless result.is_a?(Uploadcare::Parser::Group)
    msg = "invalid CDN URL or UUID was given for group: #{uuid_or_cdn_url}."
    if result.is_a?(Uploadcare::Parser::File)
      msg = msg + "\n File UUID was given. Try call @api.file if it is what you intended."
    end
    raise msg
  end

  result
end