pure_sapphire 1.0 → 1.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d11bc1bacb47b7241552a43fe91e2f89f65ff008b4d93fb6ca85aaec5721b270
4
- data.tar.gz: 3e5fcbaa02f2c1acf870462851e63d74026b6363b215e7f32b1f941781e65679
3
+ metadata.gz: f8bb8fc42b9979267830babb5405df6d9c623f1aa86cb60005e195f0b96c7f9e
4
+ data.tar.gz: b7723ff0bc84c1ec24c24bc642f756926ad290fc0a5fa4ff0ad5904cc3d9e726
5
5
  SHA512:
6
- metadata.gz: 2387f8ce5d395981640aa70994e3fb368b5ffff562f602450e92a71dc7b409be53607471d07647b378c6ebf00fe4c882877c8a57101b81ab618be3c37ff497bf
7
- data.tar.gz: fc270ba50381cd2f59d9e781223c164e6e313947f4ecca998b85b95993729ac20bf55f008deb9a719c2e3bd80ee13c719c5c95d8213d39fcd8e3a2633e013243
6
+ metadata.gz: d86c4b3fe6d5ec03225cc64c0768f97c9c739cb290721c8921f6c646ce0c6da86ebc2151fc31b967fe0ef52758c0d01fed2438076f35858451dc7c008d49c926
7
+ data.tar.gz: 74c88eab10a51499e3daf955dca8abed1b43fb9da72c711939623772d27a4d1cbfc71454c80ce318cdb32a1ca2849998037a62fbb560548aa649c623020fb8b5
@@ -0,0 +1,29 @@
1
+ require 'httparty'
2
+
3
+ class Sapphire::HTTPartyReddit
4
+ include HTTParty
5
+ base_uri 'https://www.reddit.com'
6
+
7
+ def initialize
8
+ @options = { query: { limit: 10, sort: 'hot' } }
9
+ end
10
+
11
+ def get_posts(subreddit)
12
+ response = self.class.get("/r/#{subreddit}/hot.json", @options)
13
+ parse_response(response)
14
+ end
15
+
16
+ private
17
+
18
+ def parse_response(response)
19
+ p response
20
+ response['data']['children'].map do |post|
21
+ {
22
+ title: post['data']['title'],
23
+ url: post['data']['url']
24
+ }
25
+ end
26
+ end
27
+ end
28
+
29
+
@@ -0,0 +1,27 @@
1
+ require 'rest-client'
2
+ require 'json'
3
+
4
+ class Sapphire::RestClientReddit
5
+ BASE_URL = 'https://www.reddit.com'
6
+
7
+ def initialize
8
+ @headers = { params: { limit: 10, sort: 'hot' } }
9
+ end
10
+
11
+ def get_posts(subreddit)
12
+ url = "#{BASE_URL}/r/#{subreddit}/hot.json"
13
+ response = RestClient.get(url, @headers)
14
+ parse_response(response)
15
+ end
16
+
17
+ private
18
+
19
+ def parse_response(response)
20
+ JSON.parse(response.body)['data']['children'].map do |post|
21
+ {
22
+ title: post['data']['title'],
23
+ url: post['data']['url']
24
+ }
25
+ end
26
+ end
27
+ end
data/lib/pure_sapphire.rb CHANGED
@@ -1,8 +1,44 @@
1
+
2
+ # Get the hottest posts from a subreddit
3
+
1
4
  class Sapphire
2
- def self.gem_version
3
- Gem::Version.new('1.0.0')
5
+ @BASE_URL = 'https://www.reddit.com'
6
+
7
+ def self.gem_version
8
+ Gem::Version.new('1.0.1')
9
+ end
10
+
11
+ def self.summary
12
+ puts "I am a casual Reddit consumer"
13
+ end
14
+
15
+ def self.consumer_pick_client(client_name = "HTTParty", subreddit = "BeInspired")
16
+ p "You choose " + client_name
17
+ case client_name
18
+ when "HTTParty"
19
+ @client = HTTPartyReddit
20
+ when "RestClient"
21
+ @client = RestClientReddit
22
+ else
23
+ raise ArgumentError, "Invalid client: #{client_name} choose either HTTParty or RestClient"
4
24
  end
5
- def self.consumer
6
- puts "I am a consumer of Sapphire"
25
+ @subreddit = subreddit
26
+
27
+ end
28
+
29
+ def self.consumer_fetch
30
+ if @client.nil?
31
+ raise ArgumentError, "Client is not set, please call #pick_client first"
32
+ end
33
+ reddit_api = @client.new
34
+ posts = reddit_api.get_posts(@subreddit)
35
+ posts.each do |post|
36
+ puts "Title: #{post[:title]}"
37
+ puts "URL: #{post[:url]}"
38
+ puts "-" * 50
39
+ end
7
40
  end
8
- end
41
+ end
42
+
43
+ require 'apiclients/restclient'
44
+ require 'apiclients/httparty'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pure_sapphire
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.0'
4
+ version: '1.1'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefanos Ioannou
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-02-16 00:00:00.000000000 Z
11
+ date: 2024-02-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Consume multiple APIs from mutli sources like Reddit, Twitter and more
14
14
  using pure functions
@@ -17,6 +17,8 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
+ - lib/apiclients/httparty.rb
21
+ - lib/apiclients/restclient.rb
20
22
  - lib/pure_sapphire.rb
21
23
  homepage: https://codebyte.cy
22
24
  licenses: