Module: Telegraph::Page

Defined in:
lib/telegraph/api/page.rb,
lib/telegraph/validations/page.rb

Defined Under Namespace

Classes: CommonPageSchema

Constant Summary collapse

CreatePageSchema =
Dry::Validation.Schema(CommonPageSchema)
EditPageSchema =
Dry::Validation.Schema(CommonPageSchema) do
  required(:path) { str? & filled? }
end
GetPageSchema =
Dry::Validation.Schema do
  required(:path) { str? & filled? }
  optional(:return_content) { bool? }
end
GetPageListSchema =
Dry::Validation.Schema do
  required(:access_token) { str? & filled? }
  optional(:offset) { int? }
  optional(:limit) { int? & size?(0..200) }
end
GetViewsSchema =
Dry::Validation.Schema do
  required(:path) { str? & filled? }
  required(:year) { int? & included_in?(2000..2100) }
  required(:month) { int? & included_in?(1..12) }
  optional(:day) { int? & included_in?(1..31) }
  optional(:hour) { int? & included_in?(0..24) }
end

Class Method Summary collapse

Class Method Details

.create(params) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/telegraph/api/page.rb', line 3

def self.create(params)
  return nil unless CreatePageSchema.(params).success?
  response = Telegraph::Core.request('createPage', params)
  if response.dig('result', 'content')
    response['result']['content'] = Types::Page.new(Hashie.symbolize_keys response['result'])
  end
  response
end

.edit(params) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/telegraph/api/page.rb', line 12

def self.edit(params)
  return nil unless EditPageSchema.(params).success?
  path = params.delete(:path)
  response = Telegraph::Core.request('editPage/' + path, params)
  if response.dig('result', 'content')
    response['result']['content'] = Types::Page.new(Hashie.symbolize_keys response['result'])
  end
  response
end

.get(params) ⇒ Object



22
23
24
25
26
27
# File 'lib/telegraph/api/page.rb', line 22

def self.get(params)
  return nil unless GetPageSchema.(params).success?
  path = params.delete(:path)
  response = Telegraph::Core.request('getPage/' + path, params)
  Types::Page.new(Hashie.symbolize_keys response['result'])
end

.list(params) ⇒ Object



29
30
31
32
33
# File 'lib/telegraph/api/page.rb', line 29

def self.list(params)
  return nil unless GetPageListSchema.(params).success?
  response = Telegraph::Core.request('getPageList', params)
  Types::PageList.new(Hashie.symbolize_keys response['result'])
end

.views(params) ⇒ Object



35
36
37
38
39
40
# File 'lib/telegraph/api/page.rb', line 35

def self.views(params)
  return nil unless GetViewsSchema.(params).success?
  path = params.delete(:path)
  response = Telegraph::Core.request('getViews/' + path, params)
  Types::PageViews.new(Hashie.symbolize_keys response['result'])
end