Class: KCommercial::KCPipeline::Kim

Inherits:
Object
  • Object
show all
Defined in:
lib/KCommercialPipeline/core/kim_notify.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(robot_key, kim_model) ⇒ Kim

Returns a new instance of Kim.



36
37
38
39
# File 'lib/KCommercialPipeline/core/kim_notify.rb', line 36

def initialize(robot_key,kim_model)
    @robot_key = robot_key
    @kim_model = kim_model
end

Instance Attribute Details

#kim_modelObject

Returns the value of attribute kim_model.



33
34
35
# File 'lib/KCommercialPipeline/core/kim_notify.rb', line 33

def kim_model
  @kim_model
end

#robot_keyObject

Returns the value of attribute robot_key.



34
35
36
# File 'lib/KCommercialPipeline/core/kim_notify.rb', line 34

def robot_key
  @robot_key
end

Instance Method Details

#notifi_kimObject



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/KCommercialPipeline/core/kim_notify.rb', line 41

def notifi_kim
    url = "https://kim-robot.kwaitalk.com/api/robot/send"
    payload = {}
    msgtype = ""
    #payload[:mentioned_list] = @kim_model.mentioned_list
    case @kim_model.msg_type
    when MessageType::Txt
        msgtype = "text"
        payload[:text] = {:content => @kim_model.content,:mentioned_list => @kim_model.mentioned_list || []}
    when MessageType::Image
        msgtype  = "image"
        payload[:markdown] = {:media_id => @kim_model.image_url}
    when MessageType::Markdown
        msgtype = "markdown"
        payload[:markdown] = {:content => @kim_model.content}
    else

    end
    payload[:msgtype] = msgtype
    unless robot_key.nil?
        url = url + "?key=#{robot_key}"
    end
    unless  payload[:markdown].nil?
        payload[:markdown][:mentioned_list] = @kim_model.mentioned_list || []
    end

    RestClient::Request.new({
                              method: :post,
                              url: url,
                              headers:{"Content-Type" => "application/json"},
                              payload: payload.to_json
                            }).execute do |response|
        puts "response==#{response}"
        case response.code
        when 400
            [ :error, JSON.parse(response.to_str) ]
        when 200
            [ :success, JSON.parse(response.to_str) ]
        else
            fail "Invalid response #{response.to_str} received."
        end
    end
end