butter_cms_v2 0.0.0.1

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: 395b22b381199739e45aed4ae138c57fc8f920cb8325f34bcbabbf77d17a8dfb
4
+ data.tar.gz: be0c935c0d182d5dc854b5ecd2a1f5c35163e767d28270daad12799e515c7a4d
5
+ SHA512:
6
+ metadata.gz: 4b8f68fb55fc3119ec6b29cbf6584bb0c4070b92b443aa29d130a5518e1f2c85c50a9d9b28530b8b00eb8b5a784cf16a1c7948b14f7050a276314ac367e773ca
7
+ data.tar.gz: 5e04f885ee744e1bc24504063aa005085431fbabec9e4676a3323bf1ea65808680cce11f510355a6b4885320e7f007aca667891690aea0f197c8a5a96917524c
data/lib/butter_cms.rb ADDED
@@ -0,0 +1,34 @@
1
+ require 'butter_cms/configuration'
2
+ require 'butter_cms/url_params_service'
3
+ require 'butter_cms/requests/api'
4
+ require 'butter_cms/requests/get'
5
+ require 'butter_cms/parsers/posts'
6
+ require 'butter_cms/resource'
7
+ require 'butter_cms/author'
8
+ require 'butter_cms/parsers/author_object'
9
+ require 'butter_cms/category'
10
+ require 'butter_cms/parsers/categories_objects'
11
+ require 'butter_cms/tag'
12
+ require 'butter_cms/parsers/tags_objects'
13
+ require 'butter_cms/post'
14
+ require 'butter_cms/parsers/post_object'
15
+ require 'butter_cms/posts_fetch_service'
16
+ require 'butter_cms/parsers/categories'
17
+ require 'butter_cms/parsers/post'
18
+
19
+ module ButterCMS
20
+ class << self
21
+ attr_accessor :configuration
22
+ end
23
+
24
+ # Returns the configuration class instance if block given
25
+ #
26
+ # @return [ButterCMS::Configuration]
27
+ def self.configure
28
+ self.configuration ||= ::ButterCMS::Configuration.new
29
+
30
+ yield(configuration) if block_given?
31
+ end
32
+ end
33
+
34
+ ButterCMS.configure unless ButterCMS.configuration
@@ -0,0 +1,5 @@
1
+ module ButterCMS
2
+ class Author < ButterCMS::Resource
3
+
4
+ end
5
+ end
@@ -0,0 +1,15 @@
1
+ module ButterCMS
2
+ class Category < ButterCMS::Resource
3
+
4
+ # Returns all categories
5
+ #
6
+ # @return [Array<ButterCMS::Category>]
7
+ def self.all
8
+ response = ::ButterCMS::Requests::Get.call("categories")
9
+ attributes = ::ButterCMS::Parsers::Categories.new(response).categories
10
+
11
+ ::ButterCMS::Parsers::CategoriesObjects.call(attributes)
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ module ButterCMS
2
+ class Configuration
3
+ attr_accessor :api_key
4
+
5
+ def initialize
6
+ @api_key = 'PLEASE PROVIDE VALID API KEY'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ module ButterCMS
2
+ module Parsers
3
+ class AuthorObject
4
+ # Returns the new instance of author object from the given attributes
5
+ #
6
+ # @return [ButterCMS::Author]
7
+ def self.call(author)
8
+ ::ButterCMS::Author.new(author)
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,27 @@
1
+ require 'json'
2
+
3
+ module ButterCMS
4
+ module Parsers
5
+ class Categories
6
+
7
+ def initialize(response)
8
+ @response = response
9
+ end
10
+
11
+ # Returns array of category attributes available in the response
12
+ #
13
+ # @return [Array]
14
+ def categories
15
+ parsed_json['data']
16
+ end
17
+
18
+ private
19
+
20
+ attr_reader :response
21
+
22
+ def parsed_json
23
+ @parsed_json ||= ::JSON.parse(response.body)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,15 @@
1
+ module ButterCMS
2
+ module Parsers
3
+ class CategoriesObjects
4
+ # Returns array of category objects created from given array of attributes
5
+ #
6
+ # @return [Array<ButterCMS::Category>]
7
+ def self.call(categories)
8
+ categories.map do |category_attributes|
9
+ ::ButterCMS::Category.new(category_attributes)
10
+ end
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,12 @@
1
+ module ButterCMS
2
+ module Parsers
3
+ class Post < ButterCMS::Parsers::Posts
4
+ # Returns post attributes
5
+ #
6
+ # @return [Hash]
7
+ def post
8
+ parsed_json['data']
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,19 @@
1
+ module ButterCMS
2
+ module Parsers
3
+ class PostObject
4
+ # Returns the new instance of post with the associations included
5
+ #
6
+ # @return [ButterCMS::Post]
7
+ def self.call(post_attributes)
8
+ updated_post_attributes = {
9
+ 'tags' => ::ButterCMS::Parsers::TagsObjects.call(post_attributes.delete('tags')),
10
+ 'categories' => ::ButterCMS::Parsers::CategoriesObjects.call(post_attributes.delete('categories')),
11
+ 'author' => ::ButterCMS::Parsers::AuthorObject.call(post_attributes.delete('author'))
12
+ }
13
+
14
+ ::ButterCMS::Post.new(post_attributes.merge(updated_post_attributes))
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,48 @@
1
+ require 'json'
2
+
3
+ module ButterCMS
4
+ module Parsers
5
+ class Posts
6
+
7
+ def initialize(response)
8
+ @response = response
9
+ end
10
+
11
+ # Returns the number of the next page or nil if not available
12
+ #
13
+ # @return [String]
14
+ def next_page
15
+ parsed_json['meta']['next_page']
16
+ end
17
+
18
+ # Returns the number of the previous page or nil if not available
19
+ #
20
+ # @return [String]
21
+ def previous_page
22
+ parsed_json['meta']['previous_page']
23
+ end
24
+
25
+ # Returns the count of existing posts
26
+ #
27
+ # @return [String]
28
+ def count
29
+ parsed_json['meta']['count']
30
+ end
31
+
32
+ # Returns array of posts attributes available in the response
33
+ #
34
+ # @return [Array]
35
+ def posts
36
+ parsed_json['data']
37
+ end
38
+
39
+ private
40
+
41
+ attr_reader :response
42
+
43
+ def parsed_json
44
+ @parsed_json ||= ::JSON.parse(response.body)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,15 @@
1
+ module ButterCMS
2
+ module Parsers
3
+ class TagsObjects
4
+ # Returns array of tag objects created from given array of attributes
5
+ #
6
+ # @return [Array<ButterCMS::Tag>]
7
+ def self.call(tags)
8
+ tags.map do |tag_attributes|
9
+ ::ButterCMS::Tag.new(tag_attributes)
10
+ end
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,34 @@
1
+ module ButterCMS
2
+ class Post < ButterCMS::Resource
3
+ class RecordNotFound < StandardError; end
4
+ # Returns post for given slug if available otherwise raises error RecordNotFound
5
+ #
6
+ # @return [ButterCMS::Post]
7
+ def self.all
8
+ posts = []
9
+ request_options = { page_size: 10, page: 0 }
10
+ more_posts = true
11
+
12
+ while more_posts
13
+ request_options = request_options.merge(page: request_options[:page] + 1)
14
+ fetch_service = ButterCMS::PostsFetchService.new(request_options)
15
+ posts = posts | fetch_service.posts
16
+ more_posts = fetch_service.more_posts?
17
+ end
18
+
19
+ posts
20
+ end
21
+
22
+ # Returns all available posts from the API
23
+ #
24
+ # @return [ButterCMS::Post]
25
+ def self.find(slug)
26
+ response = ::ButterCMS::Requests::Get.call("posts/#{slug}")
27
+ post_attributes = ::ButterCMS::Parsers::Post.new(response).post
28
+ ::ButterCMS::Parsers::PostObject.call(post_attributes)
29
+ rescue RestClient::NotFound
30
+ raise RecordNotFound
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,35 @@
1
+ module ButterCMS
2
+ class PostsFetchService
3
+ def initialize(request_options)
4
+ @request_options = request_options
5
+ end
6
+
7
+ # Returns array of post objects with the associated records included
8
+ #
9
+ # @return [Array<ButterCMS::Post>]
10
+ def posts
11
+ parser.posts.map do |post_attributes|
12
+ ::ButterCMS::Parsers::PostObject.call(post_attributes)
13
+ end
14
+ end
15
+
16
+ # Returns true if the next page is available, false otherwise
17
+ #
18
+ # @return [Boolean]
19
+ def more_posts?
20
+ !parser.next_page.nil?
21
+ end
22
+
23
+ private
24
+
25
+ attr_reader :request_options
26
+
27
+ def response
28
+ ::ButterCMS::Requests::Get.call("posts", request_options)
29
+ end
30
+
31
+ def parser
32
+ @parser ||= ::ButterCMS::Parsers::Posts.new(response)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,7 @@
1
+ module ButterCMS
2
+ module Requests
3
+ module API
4
+ URL = "https://api.buttercms.com/v2/".freeze
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,22 @@
1
+ require 'rest_client'
2
+
3
+ module ButterCMS
4
+ module Requests
5
+ class Get
6
+
7
+ # Returns response from the request to the given API endpoint
8
+ #
9
+ # @return [RestClient::Response]
10
+ def self.call(path, options = {})
11
+ full_url = [
12
+ ::ButterCMS::Requests::API::URL,
13
+ path,
14
+ ::ButterCMS::UrlParamsService.call(options)
15
+ ].join
16
+
17
+ ::RestClient.get(full_url)
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,9 @@
1
+ module ButterCMS
2
+ class Resource
3
+ def initialize(attributes = {})
4
+ attributes.each do |attr, value|
5
+ define_singleton_method(attr) { attributes[attr] }
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module ButterCMS
2
+ class Tag < ButterCMS::Resource
3
+
4
+ end
5
+ end
@@ -0,0 +1,14 @@
1
+ module ButterCMS
2
+ class UrlParamsService
3
+
4
+ # Returns the query part of the request to the API
5
+ #
6
+ # @return [String]
7
+ def self.call(options = {})
8
+ options[:auth_token] = ButterCMS.configuration.api_key
9
+
10
+ "?" << options.map { |(key, value)| "#{key.to_s}=#{value}" }.join("&")
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,12 @@
1
+ module ButterCMS
2
+ module Version
3
+ module_function
4
+
5
+ # Gem current version
6
+ #
7
+ # @return [String]
8
+ def to_s
9
+ "0.0.0.1"
10
+ end
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: butter_cms_v2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Paweł Dąbrowski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-08-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.7'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 3.7.0
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '3.7'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 3.7.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: rest-client
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '2.0'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '2.0'
47
+ description:
48
+ email: dziamber@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - lib/butter_cms.rb
54
+ - lib/butter_cms/author.rb
55
+ - lib/butter_cms/category.rb
56
+ - lib/butter_cms/configuration.rb
57
+ - lib/butter_cms/parsers/author_object.rb
58
+ - lib/butter_cms/parsers/categories.rb
59
+ - lib/butter_cms/parsers/categories_objects.rb
60
+ - lib/butter_cms/parsers/post.rb
61
+ - lib/butter_cms/parsers/post_object.rb
62
+ - lib/butter_cms/parsers/posts.rb
63
+ - lib/butter_cms/parsers/tags_objects.rb
64
+ - lib/butter_cms/post.rb
65
+ - lib/butter_cms/posts_fetch_service.rb
66
+ - lib/butter_cms/requests/api.rb
67
+ - lib/butter_cms/requests/get.rb
68
+ - lib/butter_cms/resource.rb
69
+ - lib/butter_cms/tag.rb
70
+ - lib/butter_cms/url_params_service.rb
71
+ - lib/butter_cms/version.rb
72
+ homepage: http://github.com/rubyhero/butter_cms
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.7.6
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Ruby wrapper for the ButterCMS API
96
+ test_files: []