dev_ruby 0.1.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -1
  3. data/Gemfile +12 -0
  4. data/Gemfile.lock +20 -1
  5. data/README.md +254 -3
  6. data/dev_ruby.gemspec +1 -1
  7. data/lib/dev_ruby/client.rb +72 -0
  8. data/lib/dev_ruby/collection.rb +21 -0
  9. data/lib/dev_ruby/error.rb +5 -0
  10. data/lib/dev_ruby/helpers.rb +11 -0
  11. data/lib/dev_ruby/objects/article.rb +8 -0
  12. data/lib/dev_ruby/objects/base_object.rb +24 -0
  13. data/lib/dev_ruby/objects/comment.rb +8 -0
  14. data/lib/dev_ruby/objects/error.rb +8 -0
  15. data/lib/dev_ruby/objects/follower.rb +8 -0
  16. data/lib/dev_ruby/objects/listing.rb +12 -0
  17. data/lib/dev_ruby/objects/organization.rb +8 -0
  18. data/lib/dev_ruby/objects/podcast_episode.rb +8 -0
  19. data/lib/dev_ruby/objects/profile_image.rb +8 -0
  20. data/lib/dev_ruby/objects/tag.rb +8 -0
  21. data/lib/dev_ruby/objects/user.rb +8 -0
  22. data/lib/dev_ruby/objects/video_article.rb +8 -0
  23. data/lib/dev_ruby/resources/articles_resource.rb +169 -0
  24. data/lib/dev_ruby/resources/base_resource.rb +49 -0
  25. data/lib/dev_ruby/resources/comments_resource.rb +35 -0
  26. data/lib/dev_ruby/resources/followers_resource.rb +22 -0
  27. data/lib/dev_ruby/resources/follows_resource.rb +23 -0
  28. data/lib/dev_ruby/resources/listings_resource.rb +75 -0
  29. data/lib/dev_ruby/resources/organizations_resource.rb +67 -0
  30. data/lib/dev_ruby/resources/podcast_episodes_resource.rb +23 -0
  31. data/lib/dev_ruby/resources/profile_images_resource.rb +19 -0
  32. data/lib/dev_ruby/resources/readinglists_resource.rb +23 -0
  33. data/lib/dev_ruby/resources/tags_resource.rb +39 -0
  34. data/lib/dev_ruby/resources/users_resource.rb +41 -0
  35. data/lib/dev_ruby/version.rb +1 -1
  36. data/lib/dev_ruby.rb +37 -2
  37. metadata +31 -3
@@ -0,0 +1,169 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rubocop:disable Metrics/ClassLength
4
+ module DevRuby
5
+ module Resources
6
+ class ArticlesResource < BaseResource
7
+ def published(**params)
8
+ params = to_default_pagination_params(params)
9
+
10
+ response = get_request('articles', params: params)
11
+
12
+ if Helpers.expected_response?(response, 200)
13
+ collection = Collection.from_response(response: response,
14
+ type: DevRuby::Objects::Article,
15
+ params: params)
16
+
17
+ Success(collection)
18
+ else
19
+ Failure(error_parser(response))
20
+ end
21
+ end
22
+
23
+ def create(**body)
24
+ response = post_request('articles', body: { article: body })
25
+
26
+ if Helpers.expected_response?(response, 201)
27
+ article = DevRuby::Objects::Article.new(response.body)
28
+
29
+ Success(article)
30
+ else
31
+ Failure(error_parser(response))
32
+ end
33
+ end
34
+
35
+ def latest_published(**params)
36
+ params = to_default_pagination_params(params)
37
+
38
+ response = get_request('articles/latest', params: params)
39
+
40
+ if Helpers.expected_response?(response, 200)
41
+ collection = Collection.from_response(response: response,
42
+ type: DevRuby::Objects::Article,
43
+ params: params)
44
+
45
+ Success(collection)
46
+ else
47
+ Failure(error_parser(response))
48
+ end
49
+ end
50
+
51
+ def find(id)
52
+ response = get_request("articles/#{id}")
53
+
54
+ if Helpers.expected_response?(response, 200)
55
+ article = DevRuby::Objects::Article.new(response.body)
56
+
57
+ Success(article)
58
+ else
59
+ Failure(error_parser(response))
60
+ end
61
+ end
62
+
63
+ def update(id:, **body)
64
+ response = put_request("articles/#{id}", body: { article: body })
65
+
66
+ if Helpers.expected_response?(response, 200)
67
+ article = DevRuby::Objects::Article.new(response.body)
68
+
69
+ Success(article)
70
+ else
71
+ Failure(error_parser(response))
72
+ end
73
+ end
74
+
75
+ def find_by_path(username:, slug:)
76
+ response = get_request("articles/#{username}/#{slug}")
77
+
78
+ if Helpers.expected_response?(response, 200)
79
+ article = DevRuby::Objects::Article.new(response.body)
80
+
81
+ Success(article)
82
+ else
83
+ Failure(error_parser(response))
84
+ end
85
+ end
86
+
87
+ def me(**params)
88
+ params = to_default_pagination_params(params)
89
+
90
+ response = get_request('articles/me')
91
+
92
+ if Helpers.expected_response?(response, 200)
93
+ collection = Collection.from_response(response: response,
94
+ type: DevRuby::Objects::Article,
95
+ params: params)
96
+
97
+ Success(collection)
98
+ else
99
+ Failure(error_parser(response))
100
+ end
101
+ end
102
+
103
+ def me_published(**params)
104
+ params = to_default_pagination_params(params)
105
+
106
+ response = get_request('articles/me/published', params: params)
107
+
108
+ if Helpers.expected_response?(response, 200)
109
+ collection = Collection.from_response(response: response,
110
+ type: DevRuby::Objects::Article,
111
+ params: params)
112
+
113
+ Success(collection)
114
+ else
115
+ Failure(error_parser(response))
116
+ end
117
+ end
118
+
119
+ def me_unpublished(**params)
120
+ params = to_default_pagination_params(params)
121
+
122
+ response = get_request('articles/me/unpublished', params: params)
123
+
124
+ if Helpers.expected_response?(response, 200)
125
+ collection = Collection.from_response(response: response,
126
+ type: DevRuby::Objects::Article,
127
+ params: params)
128
+
129
+ Success(collection)
130
+ else
131
+ Failure(error_parser(response))
132
+ end
133
+ end
134
+
135
+ def me_all(**params)
136
+ params = to_default_pagination_params(params)
137
+
138
+ response = get_request('articles/me/all', params: params)
139
+
140
+ if Helpers.expected_response?(response, 200)
141
+ collection = Collection.from_response(response: response,
142
+ type: DevRuby::Objects::Article,
143
+ params: params)
144
+
145
+ Success(collection)
146
+ else
147
+ Failure(error_parser(response))
148
+ end
149
+ end
150
+
151
+ def videos(**params)
152
+ params = to_default_pagination_params(params)
153
+
154
+ response = get_request('videos', params: params)
155
+
156
+ if Helpers.expected_response?(response, 200)
157
+ collection = Collection.from_response(response: response,
158
+ type: DevRuby::Objects::VideoArticle,
159
+ params: params)
160
+
161
+ Success(collection)
162
+ else
163
+ Failure(error_parser(response))
164
+ end
165
+ end
166
+ end
167
+ end
168
+ end
169
+ # rubocop:enable Metrics/ClassLength
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry/monads'
4
+
5
+ module DevRuby
6
+ module Resources
7
+ class BaseResource
8
+ include Dry::Monads[:result]
9
+
10
+ attr_reader :client
11
+
12
+ def initialize(client)
13
+ @client = client
14
+ end
15
+
16
+ private
17
+
18
+ def get_request(url, params: {}, headers: {})
19
+ client.connection.get(url, params, headers)
20
+ end
21
+
22
+ def post_request(url, body:, headers: {})
23
+ client.connection.post(url, body, headers)
24
+ end
25
+
26
+ def patch_request(url, body:, headers: {})
27
+ client.connection.patch(url, body, headers)
28
+ end
29
+
30
+ def put_request(url, body:, headers: {})
31
+ client.connection.put(url, body, headers)
32
+ end
33
+
34
+ def delete_request(url, params: {}, headers: {})
35
+ client.connection.delete(url, params, headers)
36
+ end
37
+
38
+ def error_parser(response)
39
+ DevRuby::Objects::Error.new(response.body)
40
+ end
41
+
42
+ def to_default_pagination_params(params)
43
+ params[:page] ||= 1
44
+ params[:per_page] ||= DevRuby.per_page
45
+ params
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DevRuby
4
+ module Resources
5
+ class CommentsResource < BaseResource
6
+ def all(**params)
7
+ params = to_default_pagination_params(params)
8
+
9
+ response = get_request('comments', params: params)
10
+
11
+ if Helpers.expected_response?(response, 200)
12
+ collection = Collection.from_response(response: response,
13
+ type: DevRuby::Objects::Comment,
14
+ params: params)
15
+
16
+ Success(collection)
17
+ else
18
+ Failure(error_parser(response))
19
+ end
20
+ end
21
+
22
+ def find(id)
23
+ response = get_request("comments/#{id}")
24
+
25
+ if Helpers.expected_response?(response, 200)
26
+ comment = DevRuby::Objects::Comment.new(response.body)
27
+
28
+ Success(comment)
29
+ else
30
+ Failure(error_parser(response))
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DevRuby
4
+ module Resources
5
+ class FollowersResource < BaseResource
6
+ def all(**params)
7
+ params = to_default_pagination_params(params)
8
+
9
+ response = get_request('followers/users')
10
+
11
+ if Helpers.expected_response?(response, 200)
12
+ collection = Collection.from_response(response: response,
13
+ type: DevRuby::Objects::Follower,
14
+ params: params)
15
+ Success(collection)
16
+ else
17
+ Failure(error_parser(response))
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DevRuby
4
+ module Resources
5
+ class FollowsResource < BaseResource
6
+ def followed_tags(**params)
7
+ params = to_default_pagination_params(params)
8
+
9
+ response = get_request('follows/tags', params: params)
10
+
11
+ if Helpers.expected_response?(response, 200)
12
+ collection = Collection.from_response(response: response,
13
+ type: DevRuby::Objects::Tag,
14
+ params: params)
15
+
16
+ Success(collection)
17
+ else
18
+ Failure(error_parser(response))
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DevRuby
4
+ module Resources
5
+ class ListingsResource < BaseResource
6
+ def published(**params)
7
+ params = to_default_pagination_params(params)
8
+
9
+ response = get_request('listings', params: params)
10
+
11
+ if Helpers.expected_response?(response, 200)
12
+ collection = Collection.from_response(response: response,
13
+ type: DevRuby::Objects::Listing,
14
+ params: params)
15
+
16
+ Success(collection)
17
+ else
18
+ Failure(error_parser(response))
19
+ end
20
+ end
21
+
22
+ def create(**body)
23
+ response = post_request('listings', body: { listing: body })
24
+
25
+ if Helpers.expected_response?(response, 201)
26
+ listing = DevRuby::Objects::Listing.new(response.body)
27
+
28
+ Success(listing)
29
+ else
30
+ Failure(error_parser(response))
31
+ end
32
+ end
33
+
34
+ def published_by_category(category:, **params)
35
+ params = to_default_pagination_params(params)
36
+
37
+ response = get_request("listings/category/#{category}", params: params)
38
+
39
+ if Helpers.expected_response?(response, 200)
40
+ collection = Collection.from_response(response: response,
41
+ type: DevRuby::Objects::Listing,
42
+ params: params)
43
+
44
+ Success(collection)
45
+ else
46
+ Failure(error_parser(response))
47
+ end
48
+ end
49
+
50
+ def find(id)
51
+ response = get_request("listings/#{id}")
52
+
53
+ if Helpers.expected_response?(response, 200)
54
+ listing = DevRuby::Objects::Listing.new(response.body)
55
+
56
+ Success(listing)
57
+ else
58
+ Failure(error_parser(response))
59
+ end
60
+ end
61
+
62
+ def update(id:, **body)
63
+ response = put_request("listings/#{id}", body: { listing: body })
64
+
65
+ if Helpers.expected_response?(response, 200)
66
+ listing = DevRuby::Objects::Listing.new(response.body)
67
+
68
+ Success(listing)
69
+ else
70
+ Failure(error_parser(response))
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DevRuby
4
+ module Resources
5
+ class OrganizationsResource < BaseResource
6
+ def find_by_username(username)
7
+ response = get_request("organizations/#{username}")
8
+
9
+ if Helpers.expected_response?(response, 200)
10
+ organization = DevRuby::Objects::Organization.new(response.body)
11
+
12
+ Success(organization)
13
+ else
14
+ Failure(error_parser(response))
15
+ end
16
+ end
17
+
18
+ def all_users_by_username(username:, **params)
19
+ params = to_default_pagination_params(params)
20
+
21
+ response = get_request("organizations/#{username}/users", params: params)
22
+
23
+ if Helpers.expected_response?(response, 200)
24
+ collection = Collection.from_response(response: response,
25
+ type: DevRuby::Objects::User,
26
+ params: params)
27
+
28
+ Success(collection)
29
+ else
30
+ Failure(error_parser(response))
31
+ end
32
+ end
33
+
34
+ def all_listings_by_username(username:, **params)
35
+ params = to_default_pagination_params(params)
36
+
37
+ response = get_request("organizations/#{username}/listings", params: params)
38
+
39
+ if Helpers.expected_response?(response, 200)
40
+ collection = Collection.from_response(response: response,
41
+ type: DevRuby::Objects::Listing,
42
+ params: params)
43
+
44
+ Success(collection)
45
+ else
46
+ Failure(error_parser(response))
47
+ end
48
+ end
49
+
50
+ def all_articles_by_username(username:, **params)
51
+ params = to_default_pagination_params(params)
52
+
53
+ response = get_request("organizations/#{username}/articles", params: params)
54
+
55
+ if Helpers.expected_response?(response, 200)
56
+ collection = Collection.from_response(response: response,
57
+ type: DevRuby::Objects::Article,
58
+ params: params)
59
+
60
+ Success(collection)
61
+ else
62
+ Failure(error_parser(response))
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DevRuby
4
+ module Resources
5
+ class PodcastEpisodesResource < BaseResource
6
+ def published(**params)
7
+ params = to_default_pagination_params(params)
8
+
9
+ response = get_request('podcast_episodes', params: params)
10
+
11
+ if Helpers.expected_response?(response, 200)
12
+ collection = Collection.from_response(response: response,
13
+ type: DevRuby::Objects::PodcastEpisode,
14
+ params: params)
15
+
16
+ Success(collection)
17
+ else
18
+ Failure(error_parser(response))
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DevRuby
4
+ module Resources
5
+ class ProfileImagesResource < BaseResource
6
+ def find_by_username(username)
7
+ response = get_request("profile_images/#{username}")
8
+
9
+ if Helpers.expected_response?(response, 200)
10
+ profile_image = DevRuby::Objects::ProfileImage.new(response.body)
11
+
12
+ Success(profile_image)
13
+ else
14
+ Failure(error_parser(response))
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DevRuby
4
+ module Resources
5
+ class ReadinglistsResource < BaseResource
6
+ def all(**params)
7
+ params = to_default_pagination_params(params)
8
+
9
+ response = get_request('readinglist', params: params)
10
+
11
+ if Helpers.expected_response?(response, 200)
12
+ collection = Collection.from_response(response: response,
13
+ type: DevRuby::Objects::Article,
14
+ params: params)
15
+
16
+ Success(collection)
17
+ else
18
+ Failure(error_parser(response))
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DevRuby
4
+ module Resources
5
+ class TagsResource < BaseResource
6
+ def all(**params)
7
+ params = to_default_pagination_params(params)
8
+
9
+ response = get_request('tags', params: params)
10
+
11
+ if Helpers.expected_response?(response, 200)
12
+ collection = Collection.from_response(response: response,
13
+ type: DevRuby::Objects::Tag,
14
+ params: params)
15
+
16
+ Success(collection)
17
+ else
18
+ Failure(error_parser(response))
19
+ end
20
+ end
21
+
22
+ def followed_tags(**params)
23
+ params = to_default_pagination_params(params)
24
+
25
+ response = get_request('tags', params: params)
26
+
27
+ if Helpers.expected_response?(response, 200)
28
+ collection = Collection.from_response(response: response,
29
+ type: DevRuby::Objects::Tag,
30
+ params: params)
31
+
32
+ Success(collection)
33
+ else
34
+ Failure(error_parser(response))
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DevRuby
4
+ module Resources
5
+ class UsersResource < BaseResource
6
+ def me
7
+ response = get_request('users/me')
8
+
9
+ if Helpers.expected_response?(response, 200)
10
+ user = DevRuby::Objects::User.new(response.body)
11
+
12
+ Success(user)
13
+ else
14
+ Failure(error_parser(response))
15
+ end
16
+ end
17
+
18
+ def find(id)
19
+ response = get_request("users/#{id}")
20
+
21
+ if Helpers.expected_response?(response, 200)
22
+ user = DevRuby::Objects::User.new(response.body)
23
+
24
+ Success(user)
25
+ else
26
+ Failure(error_parser(response))
27
+ end
28
+ end
29
+
30
+ def invite_user(**body)
31
+ response = post_request('admin/users', body: body)
32
+
33
+ if Helpers.expected_response?(response, 200)
34
+ Success(response.body)
35
+ else
36
+ Failure(error_parser(response))
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DevRuby
4
- VERSION = '0.1.0'
4
+ VERSION = '1.0.0'
5
5
  end
data/lib/dev_ruby.rb CHANGED
@@ -1,8 +1,43 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'faraday'
4
+ require 'dry-configurable'
5
+ require 'awesome_print'
3
6
  require_relative 'dev_ruby/version'
4
7
 
5
8
  module DevRuby
6
- class Error < StandardError; end
7
- # Your code goes here...
9
+ extend Dry::Configurable
10
+
11
+ setting :logger, default: ::Logger.new($stdout), reader: true
12
+ setting :log_api_bodies, default: false, reader: true
13
+ setting :per_page, default: 20, reader: true
14
+
15
+ require_relative 'dev_ruby/helpers'
16
+ require_relative 'dev_ruby/error'
17
+ require_relative 'dev_ruby/client'
18
+ require_relative 'dev_ruby/collection'
19
+ require_relative 'dev_ruby/resources/base_resource'
20
+ require_relative 'dev_ruby/resources/articles_resource'
21
+ require_relative 'dev_ruby/resources/comments_resource'
22
+ require_relative 'dev_ruby/resources/follows_resource'
23
+ require_relative 'dev_ruby/resources/followers_resource'
24
+ require_relative 'dev_ruby/resources/listings_resource'
25
+ require_relative 'dev_ruby/resources/organizations_resource'
26
+ require_relative 'dev_ruby/resources/podcast_episodes_resource'
27
+ require_relative 'dev_ruby/resources/readinglists_resource'
28
+ require_relative 'dev_ruby/resources/users_resource'
29
+ require_relative 'dev_ruby/resources/tags_resource'
30
+ require_relative 'dev_ruby/resources/profile_images_resource'
31
+ require_relative 'dev_ruby/objects/base_object'
32
+ require_relative 'dev_ruby/objects/article'
33
+ require_relative 'dev_ruby/objects/video_article'
34
+ require_relative 'dev_ruby/objects/comment'
35
+ require_relative 'dev_ruby/objects/error'
36
+ require_relative 'dev_ruby/objects/tag'
37
+ require_relative 'dev_ruby/objects/follower'
38
+ require_relative 'dev_ruby/objects/listing'
39
+ require_relative 'dev_ruby/objects/organization'
40
+ require_relative 'dev_ruby/objects/user'
41
+ require_relative 'dev_ruby/objects/podcast_episode'
42
+ require_relative 'dev_ruby/objects/profile_image'
8
43
  end