Class: NoticeSys::Client

Inherits:
Object
  • Object
show all
Includes:
RXFileIOModule
Defined in:
lib/noticesys.rb

Instance Method Summary collapse

Constructor Details

#initialize(user = '', rmagick = nil, debug: false) ⇒ Client

Returns a new instance of Client.



29
30
31
# File 'lib/noticesys.rb', line 29

def initialize(user='', rmagick=nil, debug: false)
  @user, @rmagick, @debug = user, rmagick, debug
end

Instance Method Details

#send_input(params = {}) ⇒ Object

params expected: msg, file1, file2, file3



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/noticesys.rb', line 35

def send_input(params={})

  # save the file attachments containing the images etc.

  files = (1..3).map do |n|

    f = params['file' + n.to_s]
    next unless f

    original = f[:tempfile].to_path

    scale_img original

  end.compact

  msg = params['msg']

  urls = msg.scan(/https:\/\/[^ ]+/)

  if urls.any? then

    h2 = OgExtractor.new(urls.last).to_h

    puts 'h2: ' + h2.inspect if @debug

    if h2 then
      h = {msg: msg, files: files}

      h[:site]= h2[:url][/https?:\/\/([^\/]+)/,1].sub(/^www\./,'')

      if h2[:img] then
        tmpfile = Down.download h2[:img]
        files2 = scale_img tmpfile.to_path
      end

      h[:files] = files2 || []
      h[:card] = {h2[:card] => { title: h2[:title], desc: h2[:desc],
                                 url: h2[:url]}}
      h[:msg] = msg.sub(urls.last,'')

      return "notice/%s/json: %s" % [@user, h.to_json]

    end

  end

  return "notice/%s: %s" % [@user, msg]

end