Class: Wikidotrb::Module::Site

Inherits:
Object
  • Object
show all
Extended by:
Common::Decorators
Defined in:
lib/wikidotrb/module/site.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, id:, title:, unix_name:, domain:, ssl_supported:) ⇒ Site

Returns a new instance of Site.



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/wikidotrb/module/site.rb', line 72

def initialize(client:, id:, title:, unix_name:, domain:, ssl_supported:)
  @client = client
  @id = id
  @title = title
  @unix_name = unix_name
  @domain = domain
  @ssl_supported = ssl_supported

  @pages = SitePagesMethods.new(self)
  @page = SitePageMethods.new(self)
  @forum = Forum.new(site: self)
end

Instance Attribute Details

#client (readonly)

Returns the value of attribute client.



68
69
70
# File 'lib/wikidotrb/module/site.rb', line 68

def client
  @client
end

#domain (readonly)

Returns the value of attribute domain.



68
69
70
# File 'lib/wikidotrb/module/site.rb', line 68

def domain
  @domain
end

#id (readonly)

Returns the value of attribute id.



68
69
70
# File 'lib/wikidotrb/module/site.rb', line 68

def id
  @id
end

#page (readonly)

Returns the value of attribute page.



68
69
70
# File 'lib/wikidotrb/module/site.rb', line 68

def page
  @page
end

#pages (readonly)

Returns the value of attribute pages.



68
69
70
# File 'lib/wikidotrb/module/site.rb', line 68

def pages
  @pages
end

#ssl_supported (readonly)

Returns the value of attribute ssl_supported.



68
69
70
# File 'lib/wikidotrb/module/site.rb', line 68

def ssl_supported
  @ssl_supported
end

#title (readonly)

Returns the value of attribute title.



68
69
70
# File 'lib/wikidotrb/module/site.rb', line 68

def title
  @title
end

#unix_name (readonly)

Returns the value of attribute unix_name.



68
69
70
# File 'lib/wikidotrb/module/site.rb', line 68

def unix_name
  @unix_name
end

Class Method Details

.from_unix_name(client:, unix_name:) ⇒ Site

Get a site object by its UNIX name

Parameters:

  • client (Client)

    The client

  • unix_name (String)

    Site UNIX name

Returns:

  • (Site)

    Site object

Raises:



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/wikidotrb/module/site.rb', line 93

def self.from_unix_name(client:, unix_name:)
  url = "http://#{unix_name}.wikidot.com"
  timeout = { connect: client.amc_client.config.request_timeout }
  response = HTTPX.with(timeout: timeout).get(url)

  # Handle redirects
  while response.status >= 300 && response.status < 400
    url = response.headers["location"]
    response = HTTPX.with(timeout: timeout).get(url)
  end

  # If the site doesn't exist
  raise Wikidotrb::Common::Exceptions::NotFoundException, "Site is not found: #{unix_name}.wikidot.com" if response.status == 404

  # If the site exists
  source = response.body.to_s

  # id : WIKIREQUEST.info.siteId = xxxx;
  id_match = source.match(/WIKIREQUEST\.info\.siteId = (\d+);/)
  raise Wikidotrb::Common::Exceptions::UnexpectedException, "Cannot find site id: #{unix_name}.wikidot.com" if id_match.nil?

  site_id = id_match[1].to_i

  # title: title tag
  title_match = source.match(%r{<title>(.*?)</title>})
  raise Wikidotrb::Common::Exceptions::UnexpectedException, "Cannot find site title: #{unix_name}.wikidot.com" if title_match.nil?

  title = title_match[1]

  # unix_name : WIKIREQUEST.info.siteUnixName = "xxxx";
  unix_name_match = source.match(/WIKIREQUEST\.info\.siteUnixName = "(.*?)";/)
  if unix_name_match.nil?
    raise Wikidotrb::Common::Exceptions::UnexpectedException,
          "Cannot find site unix_name: #{unix_name}.wikidot.com"
  end

  unix_name = unix_name_match[1]

  # domain : WIKIREQUEST.info.domain = "xxxx";
  domain_match = source.match(/WIKIREQUEST\.info\.domain = "(.*?)";/)
  raise Wikidotrb::Common::Exceptions::UnexpectedException, "Cannot find site domain: #{unix_name}.wikidot.com" if domain_match.nil?

  domain = domain_match[1]

  # Check SSL support
  ssl_supported = response.uri.to_s.start_with?("https")

  new(
    client: client,
    id: site_id,
    title: title,
    unix_name: unix_name,
    domain: domain,
    ssl_supported: ssl_supported
  )
end

.login_required(*methods) Originally defined in module Common::Decorators

Instance Method Details

#amc_request(bodies:, return_exceptions: false)

Execute an AMC request for this site

Parameters:

  • bodies (Array<Hash>)

    List of request bodies

  • return_exceptions (Boolean) (defaults to: false)

    Whether to return exceptions



153
154
155
156
157
158
159
160
# File 'lib/wikidotrb/module/site.rb', line 153

def amc_request(bodies:, return_exceptions: false)
  client.amc_client.request(
    bodies: bodies,
    return_exceptions: return_exceptions,
    site_name: unix_name,
    site_ssl_supported: ssl_supported
  )
end

#get_applicationsArray<SiteApplication>

Get pending membership applications for the site

Returns:



164
165
166
# File 'lib/wikidotrb/module/site.rb', line 164

def get_applications
  SiteApplication.acquire_all(site: self)
end

#get_urlString

Get the site URL

Returns:

  • (String)

    Site URL



198
199
200
# File 'lib/wikidotrb/module/site.rb', line 198

def get_url
  "http#{ssl_supported ? "s" : ""}://#{domain}"
end

#invite_user(user:, text:)

Invite a user to the site

Parameters:

  • user (User)

    User to invite

  • text (String)

    Invitation text



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/wikidotrb/module/site.rb', line 171

def invite_user(user:, text:)
  amc_request(
    bodies: [{
      action: "ManageSiteMembershipAction",
      event: "inviteMember",
      user_id: user.id,
      text: text,
      moduleName: "Empty"
    }]
  )
rescue Wikidotrb::Common::Exceptions::WikidotStatusCodeException => e
  case e.status_code
  when "already_invited"
    raise Wikidotrb::Common::Exceptions::TargetErrorException.new(
      "User is already invited to #{unix_name}: #{user.name}"
    ), e
  when "already_member"
    raise Wikidotrb::Common::Exceptions::TargetErrorException.new(
      "User is already a member of #{unix_name}: #{user.name}"
    ), e
  else
    raise e
  end
end

#to_s



85
86
87
# File 'lib/wikidotrb/module/site.rb', line 85

def to_s
  "Site(id=#{id}, title=#{title}, unix_name=#{unix_name})"
end