Class: GoogleSpreadsheetFetcher::SheetUrl

Inherits:
Object
  • Object
show all
Defined in:
lib/google_spreadsheet_fetcher/sheet_url.rb

Constant Summary collapse

HOST =
'docs.google.com'.freeze
PATH_PATTERN =
%r{spreadsheets/d/(?<spreadsheet_id>[^/]+)/edit}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spreadsheet_id, sheet_id) ⇒ SheetUrl

Returns a new instance of SheetUrl.



12
13
14
15
16
# File 'lib/google_spreadsheet_fetcher/sheet_url.rb', line 12

def initialize(spreadsheet_id, sheet_id)
  @spreadsheet_id = spreadsheet_id
  @sheet_id = sheet_id
  freeze
end

Instance Attribute Details

#sheet_idObject (readonly)

Returns the value of attribute sheet_id.



8
9
10
# File 'lib/google_spreadsheet_fetcher/sheet_url.rb', line 8

def sheet_id
  @sheet_id
end

#spreadsheet_idObject (readonly)

Returns the value of attribute spreadsheet_id.



8
9
10
# File 'lib/google_spreadsheet_fetcher/sheet_url.rb', line 8

def spreadsheet_id
  @spreadsheet_id
end

Class Method Details

.parse!(url) ⇒ Object

Raises:



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/google_spreadsheet_fetcher/sheet_url.rb', line 35

def parse!(url)
  uri = URI.parse(url)
  raise InvalidSheetUrl unless uri.host == HOST

  path_matched_result = uri.path.match(PATH_PATTERN)
  raise InvalidSheetUrl unless path_matched_result

  spreadsheet_id = path_matched_result[:spreadsheet_id]
  sheet_id = extract_sheet_id(uri.fragment)

  new(spreadsheet_id, sheet_id)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



22
23
24
25
26
# File 'lib/google_spreadsheet_fetcher/sheet_url.rb', line 22

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

  other.hash == hash
end

#hashObject



30
31
32
# File 'lib/google_spreadsheet_fetcher/sheet_url.rb', line 30

def hash
  [spreadsheet_id, sheet_id].join.hash
end

#urlObject



18
19
20
# File 'lib/google_spreadsheet_fetcher/sheet_url.rb', line 18

def url
  "https://#{self.class::HOST}/spreadsheets/d/#{spreadsheet_id}/edit#gid=#{sheet_id}"
end