dev_orbit 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ce1f0009ec93dd8200f5c31fa63e016780d46aa4214a212990c17192c4da0df6
4
- data.tar.gz: 1c7d8b16887c52840016944fc0a0498187e41ccd39e357967109ba48421473b9
3
+ metadata.gz: 81c671aa577a917ca0147e98e71227375b2f6bdb7d3191d64d4d8eded5c7d34e
4
+ data.tar.gz: 7327d11e7ebe8a546d5cc54b15516337801ba435daad4c8124eb55b9571225a9
5
5
  SHA512:
6
- metadata.gz: 1b2a168a89eea4fad802491d4a686d466f178f62b432c7b2c47ad6f5c20fe7cb17743f4ac6687c94a3a027c214809dde556c64d6bafa3c34fd4d7dfbad2ebcc4
7
- data.tar.gz: d820ef63efd2246cc560227c57a26cf332819d30745db76740d8106b888c6e73cc41e706f7e98a844b39ccf726252f402f44d5113dd53a9de3d35fa477634efb
6
+ metadata.gz: 1efb625e90bbe73b44efcbb6fe0e4fcbe279f7ec2908c30d1b71e9e58e888155e81daffcf8dae5857c4c9ebdc2dae8516c74bfb77db83f56f3867a537e06cac1
7
+ data.tar.gz: a146a6dbb68bbc27ed224d44aab866ac25957d730c00400ba5671ac575982cff637113c125ec331bffaf2efd822d5bc638a70a2817c80431c072d00284204ee8
data/CHANGELOG.md CHANGED
@@ -44,4 +44,8 @@
44
44
  - Update gem specifications
45
45
  ## [0.4.1] - 2021-07-01
46
46
 
47
- - Remove debugging output from processing DEV comments
47
+ - Remove debugging output from processing DEV comments
48
+ ## [0.5.0] - 2021-07-08
49
+
50
+ - Add pagination for all DEV interactions
51
+ - Dynamically filter what to send to Orbit based on last item of that type in the Orbit workspace
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dev_orbit (0.4.0)
4
+ dev_orbit (0.4.1)
5
5
  actionview (~> 6.1)
6
6
  activesupport (~> 6.1)
7
7
  http (~> 4.4)
@@ -25,7 +25,7 @@ GEM
25
25
  minitest (>= 5.1)
26
26
  tzinfo (~> 2.0)
27
27
  zeitwerk (~> 2.3)
28
- addressable (2.7.0)
28
+ addressable (2.8.0)
29
29
  public_suffix (>= 2.0.2, < 5.0)
30
30
  ast (2.4.2)
31
31
  builder (3.2.4)
@@ -78,7 +78,7 @@ GEM
78
78
  rails-html-sanitizer (1.3.0)
79
79
  loofah (~> 2.3)
80
80
  rainbow (3.0.0)
81
- rake (13.0.3)
81
+ rake (13.0.5)
82
82
  regexp_parser (2.1.1)
83
83
  rexml (3.2.5)
84
84
  rspec (3.10.0)
@@ -94,7 +94,7 @@ GEM
94
94
  diff-lcs (>= 1.2.0, < 2.0)
95
95
  rspec-support (~> 3.10.0)
96
96
  rspec-support (3.10.2)
97
- rubocop (1.17.0)
97
+ rubocop (1.18.3)
98
98
  parallel (~> 1.10)
99
99
  parser (>= 3.0.0.0)
100
100
  rainbow (>= 2.2.2, < 4.0)
data/lib/dev_orbit/dev.rb CHANGED
@@ -19,6 +19,8 @@ module DevOrbit
19
19
  def process_comments(type:)
20
20
  articles = get_articles(type: type)
21
21
 
22
+ return if articles.empty? || articles.nil?
23
+
22
24
  articles.each do |article|
23
25
  comments = get_article_comments(article["id"])
24
26
 
@@ -41,53 +43,78 @@ module DevOrbit
41
43
  def process_followers
42
44
  followers = get_followers
43
45
 
44
- followers.each do |follower|
45
- next if follower.nil? || follower.empty?
46
-
47
- DevOrbit::Orbit.new(
48
- type: "followers",
49
- data: {
50
- follower: follower
51
- },
52
- workspace_id: @workspace_id,
53
- api_key: @orbit_api_key,
54
- historical_import: @historical_import
55
- ).call
56
- end
46
+ return if followers.empty? || followers.nil?
47
+
48
+ DevOrbit::Orbit.new(
49
+ type: "followers",
50
+ data: {
51
+ followers: followers
52
+ },
53
+ workspace_id: @workspace_id,
54
+ api_key: @orbit_api_key,
55
+ historical_import: @historical_import,
56
+ last_orbit_member_timestamp: last_orbit_member_timestamp
57
+ ).call
57
58
  end
58
59
 
59
60
  private
60
61
 
61
62
  def get_articles(type:)
62
- if type == "user"
63
- url = URI("https://dev.to/api/articles?username=#{@username}&top=1")
64
- end
63
+ page = 1
64
+ articles = []
65
+ looped_at_least_once = false
65
66
 
66
- if type == "organization"
67
- url = URI("https://dev.to/api/organizations/#{@organization}/articles")
68
- end
67
+ while page >= 1
68
+ page += 1 if looped_at_least_once
69
+ url = URI("https://dev.to/api/articles?username=#{@username}&page=#{page}&per_page=1000") if type == "user"
69
70
 
70
- https = Net::HTTP.new(url.host, url.port)
71
- https.use_ssl = true
71
+ if type == "organization"
72
+ url = URI("https://dev.to/api/organizations/#{@organization}/articles&page=#{page}&per_page=1000")
73
+ end
72
74
 
73
- request = Net::HTTP::Get.new(url)
75
+ https = Net::HTTP.new(url.host, url.port)
76
+ https.use_ssl = true
74
77
 
75
- response = https.request(request)
78
+ request = Net::HTTP::Get.new(url)
76
79
 
77
- JSON.parse(response.body) if DevOrbit::Utils.valid_json?(response.body)
80
+ response = https.request(request)
81
+
82
+ response = JSON.parse(response.body) if DevOrbit::Utils.valid_json?(response.body)
83
+
84
+ articles << response unless response.empty? || response.nil?
85
+ looped_at_least_once = true
86
+
87
+ break if response.empty? || response.nil?
88
+ end
89
+
90
+ articles.flatten!
78
91
  end
79
92
 
80
93
  def get_followers
81
- url = URI("https://dev.to/api/followers/users")
82
- https = Net::HTTP.new(url.host, url.port)
83
- https.use_ssl = true
94
+ page = 1
95
+ followers = []
96
+ looped_at_least_once = false
84
97
 
85
- request = Net::HTTP::Get.new(url)
86
- request["api_key"] = @api_key
98
+ while page >= 1
99
+ page += 1 if looped_at_least_once
100
+ url = URI("https://dev.to/api/followers/users?page=#{page}&per_page=1000")
101
+ https = Net::HTTP.new(url.host, url.port)
102
+ https.use_ssl = true
87
103
 
88
- response = https.request(request)
104
+ request = Net::HTTP::Get.new(url)
105
+ request["api_key"] = @api_key
106
+
107
+ response = https.request(request)
108
+
109
+ response = JSON.parse(response.body) if DevOrbit::Utils.valid_json?(response.body)
110
+
111
+ followers << response unless response.empty? || response.nil?
112
+ looped_at_least_once = true
113
+
114
+ break if response.empty? || response.nil?
115
+ end
89
116
 
90
- JSON.parse(response.body) if DevOrbit::Utils.valid_json?(response.body)
117
+ followers.flatten!
91
118
  end
92
119
 
93
120
  def get_article_comments(id)
@@ -105,5 +132,26 @@ module DevOrbit
105
132
 
106
133
  comments
107
134
  end
135
+
136
+ def last_orbit_member_timestamp
137
+ @last_orbit_member_timestamp ||= begin
138
+ url = URI("https://app.orbit.love/api/v1/#{@workspace_id}/members?direction=DESC&items=10&identity=devto&sort=created_at")
139
+
140
+ http = Net::HTTP.new(url.host, url.port)
141
+ http.use_ssl = true
142
+
143
+ request = Net::HTTP::Get.new(url)
144
+ request["Accept"] = "application/json"
145
+ request["Authorization"] = "Bearer #{@orbit_api_key}"
146
+ request["User-Agent"] = "community-ruby-dev-orbit/#{DevOrbit::VERSION}"
147
+
148
+ response = http.request(request)
149
+ response = JSON.parse(response.body)
150
+
151
+ return nil if response["data"].nil? || response["data"].empty?
152
+
153
+ response["data"][0]["attributes"]["created_at"]
154
+ end
155
+ end
108
156
  end
109
157
  end
@@ -6,13 +6,14 @@ require "active_support/time"
6
6
 
7
7
  module DevOrbit
8
8
  class Orbit
9
- def initialize(type:, data:, workspace_id:, api_key:, historical_import: false)
9
+ def initialize(type:, data:, workspace_id:, api_key:, historical_import: false, last_orbit_member_timestamp: nil)
10
10
  @type = type
11
11
  @data = data
12
12
  @workspace_id = workspace_id
13
13
  @api_key = api_key
14
14
  @historical_import = historical_import
15
15
  @last_orbit_activity_timestamp = last_orbit_activity_timestamp(type)
16
+ @last_orbit_member_timestamp = last_orbit_member_timestamp
16
17
  end
17
18
 
18
19
  def call
@@ -21,11 +22,11 @@ module DevOrbit
21
22
 
22
23
  @data[:comments].each do |comment|
23
24
  unless @historical_import && @last_orbit_activity_timestamp
24
- next if comment["created_at"] || comment[:created_at] < @last_orbit_activity_timestamp unless @last_orbit_activity_timestamp.nil?
25
+ next if (comment["created_at"] || comment[:created_at]) < @last_orbit_activity_timestamp unless @last_orbit_activity_timestamp.nil?
25
26
  end
26
27
 
27
28
  if @last_orbit_activity_timestamp && @historical_import == false
28
- next if comment["created_at"] || comment[:created_at] < @last_orbit_activity_timestamp
29
+ next if (comment["created_at"] || comment[:created_at]) < @last_orbit_activity_timestamp
29
30
  end
30
31
 
31
32
  times += 1
@@ -46,17 +47,39 @@ module DevOrbit
46
47
  end
47
48
 
48
49
  if @type == "followers"
49
- DevOrbit::Interactions::Follower.new(
50
- id: @data[:follower]["id"],
51
- name: @data[:follower]["name"],
52
- username: @data[:follower]["username"],
53
- url: @data[:follower]["path"],
54
- workspace_id: @workspace_id,
55
- api_key: @api_key
56
- )
50
+ counter = 0
51
+ @data[:followers].each do |follower|
52
+ next if follower.nil? || follower.empty?
53
+
54
+ unless @historical_import && @last_orbit_member_timestamp
55
+ next if follower["created_at"] < @last_orbit_member_timestamp unless @last_orbit_member_timestamp.nil?
56
+ end
57
+
58
+ if @last_orbit_member_timestamp && @historical_import == false
59
+ next if follower["created_at"] < @last_orbit_member_timestamp
60
+ end
61
+
62
+ sleep(20) if counter % 100 == 0 && counter != 0 # API rate limiting
63
+
64
+ DevOrbit::Interactions::Follower.new(
65
+ id: follower["id"],
66
+ name: follower["name"],
67
+ username: follower["username"],
68
+ url: follower["path"],
69
+ workspace_id: @workspace_id,
70
+ api_key: @api_key
71
+ )
72
+ counter += 1
73
+ end
74
+ output = "Sent #{counter} new followers to Orbit"
75
+
76
+ puts output
77
+ return output
57
78
  end
58
79
  end
59
80
 
81
+ private
82
+
60
83
  def last_orbit_activity_timestamp(type)
61
84
  @last_orbit_activity_timestamp ||= begin
62
85
  if type == "comments"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DevOrbit
4
- VERSION = "0.4.1"
4
+ VERSION = "0.5.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dev_orbit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Orbit DevRel
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-07-01 00:00:00.000000000 Z
12
+ date: 2021-07-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport