Class: GcxApi::Site

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Conversion, ActiveModel::Validations
Defined in:
lib/gcx_api/site.rb

Constant Summary collapse

DEFAULTS =
{privacy: 'public',
theme: 'amped'}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Site

Returns a new instance of Site.



16
17
18
19
20
21
22
23
24
# File 'lib/gcx_api/site.rb', line 16

def initialize(attributes = {})
  @attributes = DEFAULTS.merge(attributes)

  @attributes.each do |name, value|
    send("#{name}=", value)
  end

  @persisted = false
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



11
12
13
# File 'lib/gcx_api/site.rb', line 11

def attributes
  @attributes
end

#domainObject

Returns the value of attribute domain.



11
12
13
# File 'lib/gcx_api/site.rb', line 11

def domain
  @domain
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/gcx_api/site.rb', line 11

def name
  @name
end

#privacyObject

Returns the value of attribute privacy.



11
12
13
# File 'lib/gcx_api/site.rb', line 11

def privacy
  @privacy
end

#sitetypeObject

Returns the value of attribute sitetype.



11
12
13
# File 'lib/gcx_api/site.rb', line 11

def sitetype
  @sitetype
end

#themeObject

Returns the value of attribute theme.



11
12
13
# File 'lib/gcx_api/site.rb', line 11

def theme
  @theme
end

#titleObject

Returns the value of attribute title.



11
12
13
# File 'lib/gcx_api/site.rb', line 11

def title
  @title
end

Instance Method Details

#createObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gcx_api/site.rb', line 30

def create
  parameters = attributes.to_json
  ticket = GcxApi::Cas.new.get_cas_service_ticket(create_endpoint)

  res = RestClient::Request.execute(:method => :post, :url => create_endpoint + '?ticket=' + ticket, :payload => parameters, :timeout => -1) { |response, request, result, &block|
                                                                       Rails.logger.debug request
                                                                       Rails.logger.debug result.inspect
                                                                        # check for error response
                                                                       if [301, 302, 307].include? response.code
                                                                         response.follow_redirection(request, result, &block)
                                                                       elsif response.code.to_i != 200
                                                                         raise response.headers.inspect + response.inspect
                                                                       end
                                                                       response.to_str
  }
  community = JSON.parse(res)
  if community['errors']
    raise community.inspect
  else
    @persisted = true
    return self
  end
end

#create_endpointObject



26
27
28
# File 'lib/gcx_api/site.rb', line 26

def create_endpoint
  GcxApi.gcx_url + "/wp-gcx/create-community.php"
end

#destroy(site_name) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/gcx_api/site.rb', line 69

def destroy(site_name)
  destroy_endpoint = GcxApi.gcx_url + "/#{site_name}/wp-gcx/delete-community.php"

  ticket = GcxApi::Cas.new.get_cas_service_ticket(destroy_endpoint)

  res = RestClient::Request.execute(:method => :post, :url => destroy_endpoint + '?ticket=' + ticket, :payload => {}, :timeout => -1) { |response, request, result, &block|
                                                                         Rails.logger.debug request
                                                                         Rails.logger.debug result.inspect
                                                                          # check for error response
                                                                         if response.code.to_i != 200
                                                                           raise result.inspect
                                                                         end
                                                                         response.to_str
  }
end

#persisted?Boolean

Returns:

  • (Boolean)


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

def persisted?
  @persisted
end

#set_option_values(options) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/gcx_api/site.rb', line 54

def set_option_values(options)
  options_endpoint = (domain || GcxApi.gcx_url) + '/' + name + '/index.php'

  RestClient::Request.execute(:method => :post, :url => options_endpoint, :payload => options, :timeout => -1) { |response, request, result, &block|
    Rails.logger.debug request
    Rails.logger.debug result.inspect
    # check for error response
    if response.code.to_i != 200
      raise response.headers.inspect + response.inspect
    end
    response.to_str
  }
end