Class: LinkedinOrbit::Linkedin

Inherits:
Object
  • Object
show all
Defined in:
lib/linkedin_orbit/linkedin.rb

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Linkedin

Returns a new instance of Linkedin.



4
5
6
7
8
9
10
# File 'lib/linkedin_orbit/linkedin.rb', line 4

def initialize(params = {})
  @linkedin_organization = params.fetch(:linkedin_organization)
  @linkedin_token = params.fetch(:linkedin_token)
  @orbit_api_key = params.fetch(:orbit_api_key)
  @orbit_workspace = params.fetch(:orbit_workspace)
  @historical_import = params.fetch(:historical_import, false)
end

Instance Method Details

#get_post_comments(id) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/linkedin_orbit/linkedin.rb', line 123

def get_post_comments(id)
  url = URI("https://api.linkedin.com/v2/socialActions/#{id}/comments?projection=(elements(*(*,actor~(*,profilePicture(displayImage~:playableStreams)))))")
  https = Net::HTTP.new(url.host, url.port)
  https.use_ssl = true

  request = Net::HTTP::Get.new(url)
  request["Accept"] = "application/json"
  request["Content-Type"] = "application/json"
  request["Authorization"] = "Bearer #{@linkedin_token}"

  response = https.request(request)

  response = JSON.parse(response.body)

  response["elements"]
end

#get_postsObject



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
114
115
116
117
118
119
120
121
# File 'lib/linkedin_orbit/linkedin.rb', line 70

def get_posts
  posts = []
  page = 0
  count = 100
  total = 0

  while page * count <= total
    url = URI("https://api.linkedin.com/v2/ugcPosts?q=authors&authors=List(#{CGI.escape(@linkedin_organization)})&sortBy=LAST_MODIFIED&start=#{page*count}&count=#{count}")
    https = Net::HTTP.new(url.host, url.port)
    https.use_ssl = true

    request = Net::HTTP::Get.new(url)
    request["Accept"] = "application/json"
    request["X-Restli-Protocol-Version"] = "2.0.0"
    request["Content-Type"] = "application/json"
    request["Authorization"] = "Bearer #{@linkedin_token}"

    response = https.request(request)

    if response.code == "401"
      puts "⛔️ Your LinkedIn auth token is expired or invalid."
      puts "✨ Great news! LinkedIn is going to be available as a Plug & Play integration in Orbit late February 2022."
      puts "Keep an eye on Canny for updates: https://orbit.canny.io/integrations"
      return []
    end

    parsed_response = JSON.parse(response.body)

    total = parsed_response["paging"]["total"] if page == 0

    return parsed_response["message"] if parsed_response["serviceErrorCode"]

    if parsed_response["elements"].nil? || parsed_response["elements"].empty?
      puts <<~HEREDOC
        No new posts to process from your LinkedIn organization.
        If you suspect this is incorrect, verify your LinkedIn organization schema is correct in your credentials.
      HEREDOC
      return []
    end

    parsed_response["elements"].each do |element|
      next if element["id"].nil?
      posts << {
        "id" => element["id"],
        "message_highlight" => element["specificContent"]["com.linkedin.ugc.ShareContent"]["shareCommentary"]["text"][0, 40]
      }
    end
    page += 1
  end

  posts
end

#last_orbit_activity_timestampObject



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/linkedin_orbit/linkedin.rb', line 140

def last_orbit_activity_timestamp
  @last_orbit_activity_timestamp ||= begin
    OrbitActivities::Request.new(
      api_key: @orbit_api_key,
      workspace_id: @orbit_workspace,
      user_agent: "community-ruby-linkedin-orbit/#{LinkedinOrbit::VERSION}",
      action: "latest_activity_timestamp",
      filters: { activity_type: "custom:linkedin:comment" }
    ).response
  end
end

#process_commentsObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
# File 'lib/linkedin_orbit/linkedin.rb', line 12

def process_comments
  posts = get_posts

  return posts unless posts.is_a?(Array)

  orbit_timestamp = last_orbit_activity_timestamp

  times = 0
  posts.each do |post|

    comments = get_post_comments(post["id"])

    next if comments.nil? || comments.empty?

    # Indicates that the member does not want their information shared
    # Member viewing access if forbidden for profile memberId
    comments.reject! do |comment| 
      if comment.has_key? "actor!"
          true if comment["actor!"]["status"] == 403
      end
    end

    # Indicates that the member does not want their information shared
    comments.reject! do |comment|
      if comment.has_key? "actor~"
        true if comment["actor~"]["id"] == "private"
      end
    end

    comments.each do |comment|
      unless @historical_import && orbit_timestamp
        next if Time.at(comment["created"]["time"] / 1000).utc.to_s < orbit_timestamp unless orbit_timestamp.nil?
      end

      if orbit_timestamp && @historical_import == false
        next if Time.at(comment["created"]["time"] / 1000).utc.to_s < orbit_timestamp
      end

      times += 1

      LinkedinOrbit::Orbit.call(
        type: "comments",
        data: {
          comment: comment,
          title: post["message_highlight"]
        },
        orbit_workspace: @orbit_workspace,
        orbit_api_key: @orbit_api_key
      )
    end
  end
  
  output = "Sent #{times} new comments to your Orbit workspace"

  puts output
  return output
end