Class: Wco::Site

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Paranoia, Mongoid::Timestamps
Defined in:
app/models/wco/site.rb

Constant Summary collapse

KIND_DRUPAL =
'drupal'
KIND_IG =
'instagram'
KIND_WP =
'wordpress'
KINDS =
%w| drupal instagram wordpress |

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.kinds_listObject



13
14
15
# File 'app/models/wco/site.rb', line 13

def self.kinds_list
  [nil] + KINDS
end

.listObject



41
42
43
# File 'app/models/wco/site.rb', line 41

def self.list
  [[nil,nil]] + all.map { |s| [ s.origin, s.id ] }
end

Instance Method Details

#bodyObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/models/wco/site.rb', line 45

def body
  {
    "_links": {
      "type":{"href":"http://pi.local/rest/type/node/article"}
    },
    "title":[{"value":"Node +++ 123 bac +++" }],
    "body":[{"value": "<b>hello, wor</b>ld!", "format": "full_html" }],
    "type":[{"target_id":"article"}],
    "status": [{"value": 1}],
    "_embedded": {
      "http://pi.local/rest/relation/node/article/field_issue": [
        { "uuid": [{ "value": "56229a95-d675-43e1-99b1-f9e11b5579c5" }] }
      ],
      "http://pi.local/rest/relation/node/article/field_tags": [
        { "uuid": [{ "value": "45646a7d-1a16-42e8-b758-f6e1c8d976f7" }] },
        { "uuid": [{ "value": "834e34e2-05ae-498d-b876-453798872ce1" }] }
      ]
    }

  }
end

#check_sitemapObject



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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'app/models/wco/site.rb', line 110

def check_sitemap
  @results = []
  @total_count = 0
  @error_count = 0

  sitemap_paths.each do |check|
    @total_count += 1

    puts "Checking #{check[:path]}:"
    if check[:selector].present?
      begin
        body = HTTParty.get( "#{origin}#{check[:path]}" ).body
      rescue OpenSSL::SSL::SSLError => err
        @results.push "NOT OK [ssl-exception] #{check[:path]}".red
        logg "NOT OK [ssl-exception] #{check[:path]}"
        check.update status: 'NOT OK'

        next
      end
      doc = Nokogiri::HTML( body )
      out = doc.search check[:selector]
      if out.present?
        @results.push "OK #{check[:path]}"
        logg "OK #{check[:path]}"
        check.update status: 'OK'
      else
        @results.push "NOT OK [selector-missing] #{check[:path]}".red
        logg "NOT OK [selector-missing] #{check[:path]}"
        check.update status: 'NOT OK'
        @error_count += 1
      end

      if check[:meta_description]
        out = doc.search( 'head meta[name="description"]' )[0]['content']
        if check[:meta_description] == out
          @results.push "OK #{check[:path]} meta_description"
          logg "OK #{check[:path]} meta_description"
          check.update status: 'OK'
        else
          @results.push "NOT OK [meta-description-missing] #{check[:path]}".red
          logg "NOT OK [meta-description-missing] #{check[:path]}"
          check.update status: 'NOT OK'
          @error_count += 1
        end
      end

    elsif check[:redirect_to].present?
      out = HTTParty.get( "#{origin}#{check[:path]}", follow_redirects: false )
      if( out.headers[:location] == check[:redirect_to] ||
          out.headers[:location] == "#{origin}#{check[:redirect_to]}" )
        @results.push "OK #{check[:path]}"
        logg "OK #{check[:path]}"
        check.update status: 'OK'
      else
        @results.push "NOT OK [redirect-missing] #{check[:path]}".red
        logg "NOT OK [redirect-missing] #{check[:path]}"
        check.update status: 'NOT OK'

        puts!( out.response, 'response' ) if DEBUG
        # puts!( out.body, 'body' ) if DEBUG

        @error_count += 1

        puts "NOT OK #{check[:path]}".red
        puts out.headers[:location]
        puts check[:redirect_to]
      end
    else
      @results.push "SKIP #{check[:path]}"
      logg "SKIP #{check[:path]}"
      check.update status: 'SKIP'
    end

    if check[:selectors]&[0]
      check[:selectors].each do |selector|
        body = HTTParty.get( "#{origin}#{check[:path]}" ).body
        doc = Nokogiri::HTML( body )
        out = doc.search selector
        if out.present?
          @results.push "OK #{check[:path]} selectors:#{selector}"
          logg "OK #{check[:path]} selectors:#{selector}"
          check.update status: 'OK'
        else
          @results.push "NOT OK [selectors-missing:#{selector}] #{check[:path]}".red
          logg "NOT OK [selectors-missing:#{selector}] #{check[:path]}"
          check.update status: 'NOT OK'
          @error_count += 1
        end
      end
    end

  end

  puts "Results:".green
  @results.each do |r|
    puts r
  end
  puts "Total count: #{@total_count}"
  puts "Error count: #{@error_count}"
  return { total_count: @total_count, error_count: @error_count, results: @results }
end

#do_postObject



67
68
69
70
71
72
73
# File 'app/models/wco/site.rb', line 67

def do_post
  HTTParty.post( post_url,
    body: JSON.generate( body ),
    headers: { 'Content-Type' => 'application/hal+json' },
    basic_auth: { username: username, password: password },
  )
end

#logg(msg) ⇒ Object



212
213
214
215
216
217
# File 'app/models/wco/site.rb', line 212

def logg msg
  Wco::Log.create!({
    message: msg,
    obj: self,
  })
end

#to_sObject



36
37
38
39
# File 'app/models/wco/site.rb', line 36

def to_s
  slug
  # origin
end

#wp_importObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/models/wco/site.rb', line 75

def wp_import
  site = self

  root_tag = Wco::Tag.find_or_create_by slug: "#{site.slug}_wp-import", site_id: site.id
  url      = "#{site.origin}/wp-json/wp/v2/posts"
  pi_admin = Wco::Profile.find_or_create_by email: '[email protected]'
  n_pages  = 12
  per_page = 100

  (1..n_pages).each do |page|
    print "Page #{page}"

    posts = HTTParty.get url, query: { per_page: per_page, page: page }
    posts.each do |post|
      report = Wco::Report.new({
        legacy_id: post['id'],
        created_at: post['date'],
        slug: post['link'].sub(site.origin, ''),
        title: post['title']['rendered'],
        subtitle: post['excerpt']['rendered'],
        body: post['content']['rendered'],
        author: pi_admin,
        tag_ids: ( [ root_tag ] + site.tags.where( :legacy_id.in => post['categories'] ) ).map(&:id),
      })

      if report.save
        print '^'
      else
        puts report.errors.messages
      end
    end
  end
  puts "ok"
end