trio-blog-api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9e6cb60631072856ce21f721aee1df3c8aa362dcca8aea76bf4bb8a9b709637d
4
+ data.tar.gz: 1c1da6fbb1be0603e98de941d1408a4e2db5786f166577812fb39c66177c621c
5
+ SHA512:
6
+ metadata.gz: 6559bec31d05544f804e1475ba4330907fee63faa4b8ff5900708b1fc441ea0b856706c2439547d6d1693f4903885d5c82d34eb7b3e5085ea0dc6cd9b16ad326
7
+ data.tar.gz: 06f82edae7798cda73283a2c550e952cc1db4eb87e7c3e254f8b988f6704267b02c897aba6e1df232ef699b6ef69d438d69c84de1b15950511405aa62215d97a
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in trio-blog-api.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,30 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ trio-blog-api (0.1.0)
5
+ nokogiri (~> 1.10)
6
+ typhoeus (~> 1.3)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ethon (0.12.0)
12
+ ffi (>= 1.3.0)
13
+ ffi (1.11.1)
14
+ mini_portile2 (2.4.0)
15
+ nokogiri (1.10.3)
16
+ mini_portile2 (~> 2.4.0)
17
+ rake (10.4.0)
18
+ typhoeus (1.3.1)
19
+ ethon (>= 0.9.0)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ bundler (~> 2.0)
26
+ rake (~> 10.0)
27
+ trio-blog-api!
28
+
29
+ BUNDLED WITH
30
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # Trio Blog API
2
+
3
+ This gem was created as a object of study in [my blog post](http://trio.dev). It's a simple API to make it easier to fetch the last Trio blog post.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'trio-blog-api'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install trio-blog-api
20
+
21
+ ## Usage
22
+
23
+ You can fetch the last blog post with:
24
+
25
+ ```ruby
26
+ require "trio-blog-api"
27
+
28
+ Trio::Blog::API.fetch_last_post.to_h
29
+
30
+ {:title=>"7 Healthy Habits Of A Remote Software Developer",
31
+ :thumbnail_image_url=>
32
+ "https://res.cloudinary.com/usetrio/image/upload/h_270,c_scale/v1564583137/nolomx0dhkdjdd9zq8fo.jpg",
33
+ :post_url=>
34
+ "https://trio.dev/blog/7-healthy-habits-of-a-remote-software-developer-67133",
35
+ :author=>
36
+ {:name=>"Dhyego Calota",
37
+ :avatar_image_url=>
38
+ "https://res.cloudinary.com/usetrio/image/upload/h_90,c_scale/v1553087570/uwaslkbvubggrh8rul30.jpg"}}
39
+ ```
40
+
41
+ ## Settings
42
+
43
+ | Option | Type | Default | Description |
44
+ | ---------------------------------- | -------- | ---------------------------- | ------------------------------------------------ |
45
+ | `base_url` | *String* | `http://trio.dev` | Base URL of trio's website |
46
+ | `post_page_path` | *String* | `/` | Post page path URL that contains the last post |
47
+ | `post_selector` | *String* | `.card-article` | CSS selector of the post wrapper element |
48
+ | `title_selector` | *String* | `.card-article-title` | CSS selector of the post title element |
49
+ | `thumbnail_image_url_selector` | *String* | `.card-article-thumb` | CSS selector of the post thumbnail image element |
50
+ | `post_url_selector` | *String* | `.card-article-link` | CSS selector of the post link element |
51
+ | `author_name_selector` | *String* | `.card-article-author-name` | CSS selector of the post author name element |
52
+ | `author_avatar_image_url_selector` | *String* | `.card-article-author-image` | CSS selector of the post author avatar element |
53
+
54
+ ### Usage
55
+ ```ruby
56
+ Trio::Blog::API.configure do |config|
57
+ config.post_selector = '.card-article:first-child'
58
+ end
59
+ ```
60
+
61
+ ## Contributing
62
+
63
+ Bug reports and pull requests are welcome on GitHub at https://github.com/usetrio/trio-blog-api.
64
+
65
+ ## License
66
+
67
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "trio/blog/api"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,48 @@
1
+ module Trio
2
+ module Blog
3
+ module API
4
+ class Config
5
+ attr_writer :base_url,
6
+ :post_page_path,
7
+ :post_selector,
8
+ :title_selector,
9
+ :thumbnail_image_url_selector,
10
+ :post_url_selector,
11
+ :author_name_selector,
12
+ :author_avatar_image_url_selector
13
+
14
+ def base_url
15
+ @base_url ||= 'https://trio.dev'
16
+ end
17
+
18
+ def post_page_path
19
+ @post_page_path ||= '/'
20
+ end
21
+
22
+ def post_selector
23
+ @post_selector ||= '.card-article'
24
+ end
25
+
26
+ def title_selector
27
+ @title_selector ||= '.card-article-title'
28
+ end
29
+
30
+ def thumbnail_image_url_selector
31
+ @thumbnail_image_url_selector ||= '.card-article-thumb'
32
+ end
33
+
34
+ def post_url_selector
35
+ @post_url_selector ||= '.card-article-link'
36
+ end
37
+
38
+ def author_name_selector
39
+ @author_name_selector ||= '.card-article-author-name'
40
+ end
41
+
42
+ def author_avatar_image_url_selector
43
+ @author_avatar_image_url_selector ||= '.card-article-author-image'
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,14 @@
1
+ module Trio
2
+ module Blog
3
+ module API
4
+ class DocumentReader
5
+ attr_reader :document, :config
6
+
7
+ def initialize(document, config)
8
+ @document = document
9
+ @config = config
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module Trio
2
+ module Blog
3
+ module API
4
+ class EmptyAttributeError < Error
5
+ attr_reader :attribute_name
6
+
7
+ def initialize(attribute_name)
8
+ super("Empty attribute value for '#{attribute_name}'")
9
+ @attribute_name = attribute_name
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,7 @@
1
+ module Trio
2
+ module Blog
3
+ module API
4
+ class Error < StandardError; end
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,14 @@
1
+ module Trio
2
+ module Blog
3
+ module API
4
+ class ResponseError < Error
5
+ attr_reader :response
6
+
7
+ def initialize(response)
8
+ super(response.return_message)
9
+ @response = response
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module Trio
2
+ module Blog
3
+ module API
4
+ class SelectorNotFoundError < Error
5
+ attr_reader :selector
6
+
7
+ def initialize(selector)
8
+ super("Element not found with '#{selector}' selector")
9
+ @selector = selector
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,41 @@
1
+ require "uri"
2
+
3
+ module Trio
4
+ module Blog
5
+ module API
6
+ class PostAttributes < DocumentReader
7
+ attr_reader :selectors
8
+
9
+ def initialize(selectors, document, config)
10
+ super(document, config)
11
+ @selectors = selectors
12
+ end
13
+
14
+ def title
15
+ selectors.title_element.text || raise(EmptyAttributeError.new(__method__))
16
+ end
17
+
18
+ def thumbnail_image_url
19
+ selectors.thumbnail_image_element.attr("data-src") || raise(EmptyAttributeError.new(__method__))
20
+ end
21
+
22
+ def post_url
23
+ post_url = selectors.post_link_element.attr("href") || raise(EmptyAttributeError.new(__method__))
24
+ parsed_post_url = URI.parse(post_url)
25
+ base_url = URI.parse(config.base_url)
26
+ parsed_post_url.host = base_url.host
27
+ parsed_post_url.scheme = base_url.scheme
28
+ parsed_post_url.to_s
29
+ end
30
+
31
+ def author_name
32
+ selectors.author_name_element.text || raise(EmptyAttributeError.new(__method__))
33
+ end
34
+
35
+ def author_avatar_image_url
36
+ selectors.author_avatar_image_element.attr("data-src") || raise(EmptyAttributeError.new(__method__))
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,27 @@
1
+ module Trio
2
+ module Blog
3
+ module API
4
+ class PostReader < DocumentReader
5
+ def selectors
6
+ @selectors ||= PostSelectors.new(document, config)
7
+ end
8
+
9
+ def attributes
10
+ @attributes ||= PostAttributes.new(selectors, document, config)
11
+ end
12
+
13
+ def to_h
14
+ {
15
+ title: attributes.title,
16
+ thumbnail_image_url: attributes.thumbnail_image_url,
17
+ post_url: attributes.post_url,
18
+ author: {
19
+ name: attributes.author_name,
20
+ avatar_image_url: attributes.author_avatar_image_url
21
+ }
22
+ }
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,31 @@
1
+ module Trio
2
+ module Blog
3
+ module API
4
+ class PostSelectors < DocumentReader
5
+ def post_element
6
+ document.css(config.post_selector).first || raise(SelectorNotFoundError.new(config.post_selector))
7
+ end
8
+
9
+ def title_element
10
+ post_element.css(config.title_selector).first || raise(SelectorNotFoundError.new(config.title_selector))
11
+ end
12
+
13
+ def thumbnail_image_element
14
+ post_element.css(config.thumbnail_image_url_selector).first || raise(SelectorNotFoundError.new(config.thumbnail_image_url_selector))
15
+ end
16
+
17
+ def post_link_element
18
+ post_element.css(config.post_url_selector).first || raise(SelectorNotFoundError.new(config.post_url_selector))
19
+ end
20
+
21
+ def author_name_element
22
+ post_element.css(config.author_name_selector).first || raise(SelectorNotFoundError.new(config.author_name_selector))
23
+ end
24
+
25
+ def author_avatar_image_element
26
+ post_element.css(config.author_avatar_image_url_selector).first || raise(SelectorNotFoundError.new(config.author_avatar_image_url_selector))
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,7 @@
1
+ module Trio
2
+ module Blog
3
+ module API
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,34 @@
1
+ require "typhoeus"
2
+ require "nokogiri"
3
+ require "trio/blog/api/version"
4
+ require "trio/blog/api/config"
5
+ require "trio/blog/api/document_reader"
6
+ require "trio/blog/api/post_reader"
7
+ require "trio/blog/api/post_selectors"
8
+ require "trio/blog/api/post_attributes"
9
+ require "trio/blog/api/error/error"
10
+ require "trio/blog/api/error/response_error"
11
+ require "trio/blog/api/error/selector_not_found_error"
12
+ require "trio/blog/api/error/empty_attribute_error"
13
+
14
+ module Trio
15
+ module Blog
16
+ module API
17
+ extend self
18
+
19
+ def config
20
+ @config ||= Config.new
21
+ end
22
+
23
+ def configure
24
+ yield config
25
+ end
26
+
27
+ def fetch_last_post
28
+ response = Typhoeus.get(config.base_url)
29
+ raise ResponseError.new(response) unless response.success?
30
+ PostReader.new(Nokogiri::HTML(response.body), config)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,33 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "trio/blog/api/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "trio-blog-api"
7
+ spec.version = Trio::Blog::API::VERSION
8
+ spec.authors = ["Dhyego Calota"]
9
+ spec.email = ["dhyego@usetrio.com"]
10
+
11
+ spec.summary = %q{This gem is an API to make it easier to fetch the last Trio blog post.}
12
+ spec.homepage = "https://github.com/usetrio/trio-blog-api"
13
+ spec.license = "MIT"
14
+
15
+ if spec.respond_to?(:metadata)
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = spec.homepage
18
+ else
19
+ raise "RubyGems 2.0 or newer is required to protect against " \
20
+ "public gem pushes."
21
+ end
22
+
23
+ spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_development_dependency "bundler", "~> 2.0"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_dependency "typhoeus", "~> 1.3"
32
+ spec.add_dependency "nokogiri", "~> 1.10"
33
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trio-blog-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dhyego Calota
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-08-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: typhoeus
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: nokogiri
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.10'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.10'
69
+ description:
70
+ email:
71
+ - dhyego@usetrio.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - Gemfile.lock
79
+ - LICENSE.txt
80
+ - README.md
81
+ - bin/console
82
+ - bin/setup
83
+ - lib/trio/blog/api.rb
84
+ - lib/trio/blog/api/config.rb
85
+ - lib/trio/blog/api/document_reader.rb
86
+ - lib/trio/blog/api/error/empty_attribute_error.rb
87
+ - lib/trio/blog/api/error/error.rb
88
+ - lib/trio/blog/api/error/response_error.rb
89
+ - lib/trio/blog/api/error/selector_not_found_error.rb
90
+ - lib/trio/blog/api/post_attributes.rb
91
+ - lib/trio/blog/api/post_reader.rb
92
+ - lib/trio/blog/api/post_selectors.rb
93
+ - lib/trio/blog/api/version.rb
94
+ - trio-blog-api.gemspec
95
+ homepage: https://github.com/usetrio/trio-blog-api
96
+ licenses:
97
+ - MIT
98
+ metadata:
99
+ homepage_uri: https://github.com/usetrio/trio-blog-api
100
+ source_code_uri: https://github.com/usetrio/trio-blog-api
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.7.9
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: This gem is an API to make it easier to fetch the last Trio blog post.
121
+ test_files: []