Class: Wco::ReportsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/wco/reports_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/wco/reports_controller.rb', line 6

def create
  params[:report][:tag_ids]&.delete ''

  @report = Wco::Report.new params[:report].permit!
  authorize! :create, @report

  @report.author = current_profile

  if params[:report][:image_thumb]&.[](:image).present?
    thumb = @report.image_thumb || @report.build_image_thumb
    thumb.image = params[:report][:image_thumb_attributes][:image]
    thumb.save
  end

  if @report.save
    flash_notice "created report"
  else
    flash_alert "Cannot create report: #{@report.errors.messages}"
  end
  redirect_to action: 'index'
end

#destroyObject



28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/wco/reports_controller.rb', line 28

def destroy
  @report = Wco::Report.find params[:id]
  authorize! :destroy, @report
  if @report.destroy
    flash_notice 'ok'
  else
    flash_alert 'No luck.'
  end
  redirect_to action: 'index'
end

#editObject



39
40
41
42
# File 'app/controllers/wco/reports_controller.rb', line 39

def edit
  @report = Wco::Report.unscoped.find params[:id]
  authorize! :edit, @report
end

#indexObject



44
45
46
47
48
49
50
51
# File 'app/controllers/wco/reports_controller.rb', line 44

def index
  authorize! :index, Wco::Report
  @reports = Wco::Report.all
  if params[:deleted]
    @reports = Wco::Report.unscoped.where( :deleted_at.ne => nil )
  end
  @reports = @reports.page( params[:reports_page] ).per( current_profile.per_page )
end

#newObject



53
54
55
56
# File 'app/controllers/wco/reports_controller.rb', line 53

def new
  authorize! :new, Wco::Report
  @new_report = Wco::Report.new
end

#showObject



58
59
60
61
62
63
64
65
66
# File 'app/controllers/wco/reports_controller.rb', line 58

def show
  @report = Wco::Report.unscoped.find params[:id]
  authorize! :show, @report

  @publishers_list = Wco::Publisher.list

  # @config = JSON.parse( @report.config_json )
  # @duration_ms = @config['vtimes'].last.to_i + @config['vdurations'].last.to_i
end

#to_company_linkedinObject

not working, no access



69
70
71
72
73
74
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
109
110
111
112
113
# File 'app/controllers/wco/reports_controller.rb', line 69

def to_company_linkedin
  @report = Wco::Report.unscoped.find params[:id]
  authorize! :edit, @report
  pi = Wco::Profile.pi

  response = HTTParty.get(
    "https://api.linkedin.com/v2/organizationAcls?q=roleAssignee",
    headers: {
      "Authorization" => "Bearer #{pi.linkedin_access_token}"
    }
  )
  org = JSON.parse(response.body)
  puts! org, 'org'

  uri = URI("https://api.linkedin.com/v2/ugcPosts")

  req = Net::HTTP::Post.new(uri)
  req["Authorization"] = "Bearer #{pi.linkedin_access_token}"
  req["Content-Type"] = "application/json"
  req["X-Restli-Protocol-Version"] = "2.0.0"

  body = {
    author: "urn:li:organization:#{org['id']}",
    lifecycleState: "PUBLISHED",
    specificContent: {
      "com.linkedin.ugc.ShareContent": {
        shareCommentary: {
          text: "#{@report.title}   #{@report.body}",
        },
        shareMediaCategory: "NONE"
      }
    },
    visibility: {
      "com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC"
    }
  }

  req.body = body.to_json

  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
    http.request(req)
  end

  puts res.body
end

#to_facebookObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/controllers/wco/reports_controller.rb', line 115

def to_facebook
  @report = Wco::Report.unscoped.find params[:id]
  authorize! :edit, @report
  pi = Wco::Profile.pi

  text = @report.body
  text.gsub!(%r{</p\s*>}i, "\n")
  text.gsub!(%r{<p\s*/?>}i, "")
  text.gsub!(%r{<[^>]*>}, "")
  text.strip.gsub(/\n{3,}/, "\n\n")

  Wco::FacebookPoster.new.post("#{@report.title}\n\n#{text}")
  flash_notice 'Probably ok.'
  redirect_to request.referrer
end

#to_linkedinObject



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
# File 'app/controllers/wco/reports_controller.rb', line 131

def to_linkedin
  @report = Wco::Report.unscoped.find params[:id]
  authorize! :edit, @report
  pi = Wco::Profile.pi

  uri = URI("https://api.linkedin.com/v2/userinfo")
  req = Net::HTTP::Get.new(uri)
  req['Authorization'] = "Bearer #{pi.linkedin_access_token}"

  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http| http.request(req) }
  profile = JSON.parse(res.body)
  puts! profile, 'profile'
  user_id = profile['sub']

  # Create post
  post_uri = URI("https://api.linkedin.com/v2/ugcPosts")
  post_req = Net::HTTP::Post.new(post_uri)

  post_req['Authorization'] = "Bearer #{pi.linkedin_access_token}"
  post_req['Content-Type'] = "application/json"
  post_req['X-Restli-Protocol-Version'] = "2.0.0"


  text = @report.body
  text.gsub!(%r{</p\s*>}i, "\n")
  text.gsub!(%r{<p\s*/?>}i, "")
  text.gsub!(%r{<[^>]*>}, "")
  text.strip.gsub(/\n{3,}/, "\n\n")

  body = {
    author: "urn:li:person:#{user_id}",
    lifecycleState: "PUBLISHED",
    specificContent: {
      "com.linkedin.ugc.ShareContent": {
        shareCommentary: {
          text: "#{@report.title}\n\n#{text}",
        },
        shareMediaCategory: "NONE"
      }
    },
    visibility: {
      "com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC"
    }
  }

  post_req.body = body.to_json

  post_res = Net::HTTP.start(post_uri.hostname, post_uri.port, use_ssl: true) do |http|
    http.request(post_req)
  end

  render json: JSON.parse(post_res.body)
end

#updateObject



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'app/controllers/wco/reports_controller.rb', line 186

def update
  params[:report][:tag_ids]&.delete ''
  img_thumb_params = params[:report][:image_thumb]
  params[:report].delete :image_thumb
  # puts! img_thumb_params, 'img_thumb_params'

  @report = Wco::Report.unscoped.find params[:id]
  authorize! :update, @report

  if @report.update params[:report].permit!

    if img_thumb_params.present?
      @photo = Wco::Photo.new photo: img_thumb_params
      @photo.save!
      @report.image_thumb = @photo
    end


    flash_notice "updated report"
  else
    flash_alert "Cannot update report: #{@report.errors.messages}"
  end
  redirect_to action: 'index'
end