haunted 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f19cdc407073f568c9a4858c00df18db45240bd910cdee79b776d477961c36dd
4
+ data.tar.gz: 58b7bf117199d587c41053f4025a835f4d3e511790239ead005c0f4d5067d45b
5
+ SHA512:
6
+ metadata.gz: 33754d40dea7d3ab5f74e9e25963873f6713c5fbe964132902aea888716538c83a1bfa93c358ba0c7ead2efb4d1b3a43c558259d423dd16777ee0075c6113117
7
+ data.tar.gz: 74a68162fd8c787b0cdf3b82b93520a7cc56c4d46a8f7f16603e349b85722673297ad746d94d54cc887ab05bc33505a96c7a49056b3d6f76b41e6f75740f6a93
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ ## [0.1.0] - 2022-09-05
2
+
3
+ - Initial release
4
+
5
+ ## [0.1.1] - 2022-09-06
6
+
7
+ - Fix some wrong urls in the gemspec
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in haunted.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+ gem "rspec", "~> 3.0"
10
+ gem 'rspec-file_fixtures', '~> 0.1.6'
11
+ gem "rubocop", "~> 1.21"
12
+ gem "webmock", "~> 3.18", ">= 3.18.1"
data/Gemfile.lock ADDED
@@ -0,0 +1,79 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ haunted (0.1.1)
5
+ httparty (~> 0.20.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ addressable (2.8.1)
11
+ public_suffix (>= 2.0.2, < 6.0)
12
+ ast (2.4.2)
13
+ crack (0.4.5)
14
+ rexml
15
+ diff-lcs (1.5.0)
16
+ hashdiff (1.0.1)
17
+ httparty (0.20.0)
18
+ mime-types (~> 3.0)
19
+ multi_xml (>= 0.5.2)
20
+ json (2.6.2)
21
+ mime-types (3.4.1)
22
+ mime-types-data (~> 3.2015)
23
+ mime-types-data (3.2022.0105)
24
+ multi_xml (0.6.0)
25
+ parallel (1.22.1)
26
+ parser (3.1.2.1)
27
+ ast (~> 2.4.1)
28
+ public_suffix (5.0.0)
29
+ rainbow (3.1.1)
30
+ rake (13.0.6)
31
+ regexp_parser (2.5.0)
32
+ rexml (3.2.5)
33
+ rspec (3.11.0)
34
+ rspec-core (~> 3.11.0)
35
+ rspec-expectations (~> 3.11.0)
36
+ rspec-mocks (~> 3.11.0)
37
+ rspec-core (3.11.0)
38
+ rspec-support (~> 3.11.0)
39
+ rspec-expectations (3.11.0)
40
+ diff-lcs (>= 1.2.0, < 2.0)
41
+ rspec-support (~> 3.11.0)
42
+ rspec-file_fixtures (0.1.6)
43
+ rspec (~> 3.0)
44
+ rspec-mocks (3.11.1)
45
+ diff-lcs (>= 1.2.0, < 2.0)
46
+ rspec-support (~> 3.11.0)
47
+ rspec-support (3.11.0)
48
+ rubocop (1.36.0)
49
+ json (~> 2.3)
50
+ parallel (~> 1.10)
51
+ parser (>= 3.1.2.1)
52
+ rainbow (>= 2.2.2, < 4.0)
53
+ regexp_parser (>= 1.8, < 3.0)
54
+ rexml (>= 3.2.5, < 4.0)
55
+ rubocop-ast (>= 1.20.1, < 2.0)
56
+ ruby-progressbar (~> 1.7)
57
+ unicode-display_width (>= 1.4.0, < 3.0)
58
+ rubocop-ast (1.21.0)
59
+ parser (>= 3.1.1.0)
60
+ ruby-progressbar (1.11.0)
61
+ unicode-display_width (2.2.0)
62
+ webmock (3.18.1)
63
+ addressable (>= 2.8.0)
64
+ crack (>= 0.3.2)
65
+ hashdiff (>= 0.4.0, < 2.0.0)
66
+
67
+ PLATFORMS
68
+ x86_64-linux
69
+
70
+ DEPENDENCIES
71
+ haunted!
72
+ rake (~> 13.0)
73
+ rspec (~> 3.0)
74
+ rspec-file_fixtures (~> 0.1.6)
75
+ rubocop (~> 1.21)
76
+ webmock (~> 3.18, >= 3.18.1)
77
+
78
+ BUNDLED WITH
79
+ 2.3.7
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Dana Holt
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,108 @@
1
+ # Haunted
2
+
3
+ This gem will allow you to pull data from a Ghost CMS installation through its content API.
4
+
5
+ You can read more about the Ghost CMS here:
6
+ https://ghost.org/
7
+
8
+ ...and more about its content API here:
9
+ https://ghost.org/docs/content-api/
10
+
11
+ ## Installation
12
+
13
+ Install the gem and add to the application's Gemfile by executing:
14
+
15
+ $ bundle add haunted
16
+
17
+ If bundler is not being used to manage dependencies, install the gem by executing:
18
+
19
+ $ gem install haunted
20
+
21
+ ## Usage
22
+
23
+ ### Creating a Ghost Content API Key
24
+ Using the gem is pretty simple.
25
+
26
+ All you need is the url of your Ghost CMS site and a content API key.
27
+
28
+ You can generate a content API key by going to your Ghost admin area and then Settings > Integrations.
29
+
30
+ Once there you click "Add custom integration" to create a new key.
31
+
32
+ ## Haunted Gem Quick Start
33
+
34
+ ### Initializing the API client
35
+
36
+ api = Haunted::GhostContentApi.new('https://my-ghost-cms-site.com', '{CONTENT_API_KEY}')
37
+
38
+ ### Getting All Resources
39
+ There are methods to get all items for a specified type (posts, authors, tags, pages).
40
+
41
+ For example, if you want to get all posts you would.
42
+
43
+ posts = api.posts
44
+
45
+ By default results are returned in pages of 15 items, but you use the paging parameter to specify the number of items
46
+ per page and also the page number.
47
+
48
+ For example, if you wanted to get all posts in a single call this would do it:
49
+
50
+ all_posts_for_real = api.posts(paging: { limit: :all })
51
+
52
+ You can also use limit with page to specify the page of results to return, this would return the second page of
53
+ results in pages of 5:
54
+
55
+ page_of_posts = api.posts(paging: { limit: '5', page: '2' })
56
+
57
+ ### Getting Resource by ID
58
+ If you need to get a single resource by its ID then you can use the find by id methods (posts_by_id, authors_by_id,
59
+ tags_by_id, pages_by_id).
60
+
61
+ For example, if you want to get a post by its id:
62
+
63
+ post = api.find_by_id("6260c376eb848005f39bf06e")
64
+
65
+ ### Getting Resource by Slug
66
+ Getting resources by their slug is the same as by getting them by ID. There are a set of methods (posts_by_slug,
67
+ authors_by_slug, tags_by_slug, pages_by_slug) for that.
68
+
69
+ For example, if you want to get a post by its slug:
70
+
71
+ post = apt.find_by_slug("my-cool-post")
72
+
73
+ ## Including Related Resources
74
+ All methods have an include parameter to specify related resources to return. An example of this would be a post's
75
+ author.
76
+
77
+ You can include the author resource for a post along with it like this:
78
+
79
+ posts_with_author = api.posts(include: [:author])
80
+
81
+ ## Limiting Returned Fields
82
+ If you don't need all of a resource's field you can specify when ones you need using the fields parameter. All methods
83
+ accept this parameter.
84
+
85
+ Say you wanted to only return a post's id, title, and slug.
86
+
87
+ You would do it like this:
88
+
89
+ posts_with_only_certain_fields = api.posts(fields: [:id, :title, :slug])
90
+
91
+ ## Further Reading
92
+ For more details on the Ghost CMS content API please take a look at the official documentation:
93
+
94
+ https://ghost.org/docs/content-api/
95
+
96
+ ## Development
97
+
98
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
99
+
100
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
101
+
102
+ ## Contributing
103
+
104
+ Bug reports and pull requests are welcome on GitHub at https://github.com/DanaHolt/haunted.
105
+
106
+ ## License
107
+
108
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Haunted
4
+ VERSION = "0.1.1"
5
+ end
data/lib/haunted.rb ADDED
@@ -0,0 +1,167 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "haunted/version"
4
+ require "httparty"
5
+
6
+ module Haunted
7
+ ##
8
+ # Client that communicates with the Ghost CMS content API
9
+ ##
10
+ class GhostContentApi
11
+ include HTTParty
12
+
13
+ ##
14
+ # Initializes a new Ghost Content API client
15
+ #
16
+ # site_url: URL to the Ghost site
17
+ # key: Content API key
18
+ # options: api_path - override the default content API path, api_version - override the default API version
19
+ ##
20
+ def initialize(site_url, key, **options)
21
+ @site_url = site_url
22
+ @key = key
23
+ @api_path = options[:api_path] || "ghost/api/content"
24
+ @api_version = options[:api_version] || "v5.0"
25
+ end
26
+
27
+ %w[posts authors tags pages].each do |resource_type|
28
+ ##
29
+ # Methods that get all posts, authors, tags, or pages. By default all items are returned with all
30
+ # properties in pages of 15 results each.
31
+ #
32
+ # include: Array of strings or symbols that specify additional related resource types to include, for example
33
+ # include[:tags] would include a post's tags.
34
+ #
35
+ # fields: Array of strings or symbols that specify which resource fields are returned. If you only need some
36
+ # fields then use this option.
37
+ #
38
+ # Available options for the include and fields options vary depending on the resource, see the Ghost Content API
39
+ # for more information.
40
+ #
41
+ # paging: Hash that specifies paging and limit options. Use { limit: all } to return all items. Specify a number
42
+ # to returned that number of items per page. Use { page: x } to specify what page of results to return.
43
+ ##
44
+ define_method(resource_type) do |include: nil, fields: nil, paging: nil|
45
+ self.class.get(
46
+ build_browse_api_endpoint(resource_type),
47
+ query: build_query_string_params(include: include, fields: fields, paging: paging),
48
+ headers: build_headers
49
+ )
50
+ end
51
+ end
52
+
53
+ %w[posts_by_id authors_by_id tags_by_id pages_by_id].each do |method|
54
+ resource_type = method.split("_").first
55
+
56
+ ##
57
+ # These methods (posts_by_id authors_by_id tags_by_id pages_by_id) get posts, authors, tags, or pages by
58
+ # the resource's id.
59
+ #
60
+ # id: id of the item you want to return
61
+ #
62
+ # include: Array of strings or symbols that specify additional related resource types to include, for example
63
+ # include[:tags] would include a post's tags.
64
+ #
65
+ # fields: Array of strings or symbols that specify which resource fields are returned. If you only need some
66
+ # fields then use this option.
67
+ #
68
+ # Available options for the include and fields options vary depending on the resource, see the Ghost Content API
69
+ # for more information.
70
+ ##
71
+ define_method(method) do |id, include: nil, fields: nil|
72
+ self.class.get(
73
+ build_id_api_endpoint(resource_type, id),
74
+ query: build_query_string_params(include: include, fields: fields),
75
+ headers: build_headers
76
+ )
77
+ end
78
+ end
79
+
80
+ %w[posts_by_slug authors_by_slug tags_by_slug pages_by_slug].each do |method|
81
+ resource_type = method.split("_").first
82
+
83
+ ##
84
+ # These methods (posts_by_slug authors_by_slug tags_by_slug pages_by_slug )get posts, authors, tags, or pages by
85
+ # the resource's slug.
86
+ #
87
+ # slug: slug of the item you want to return
88
+ #
89
+ # include: Array of strings or symbols that specify additional related resource types to include, for example
90
+ # include[:tags] would include a post's tags.
91
+ #
92
+ # fields: Array of strings or symbols that specify which resource fields are returned. If you only need some
93
+ # fields then use this option.
94
+ #
95
+ # Available options for the include and fields options vary depending on the resource, see the Ghost Content API
96
+ # for more information.
97
+ ##
98
+ define_method(method) do |slug, include: nil, fields: nil|
99
+ self.class.get(
100
+ build_slug_api_endpoint(resource_type, slug),
101
+ query: build_query_string_params(include: include, fields: fields),
102
+ headers: build_headers
103
+ )
104
+ end
105
+ end
106
+
107
+ ##
108
+ # Gets the available tiers
109
+ #
110
+ # fields: Array of strings or symbols that specify which resource fields are returned. If you only need some
111
+ # fields then use this option.
112
+ #
113
+ # Available options for the include and fields options vary depending on the resource, see the Ghost Content API
114
+ # for more information.
115
+ #
116
+ # paging: Hash that specifies paging and limit options. Use { limit: all } to return all items. Specify a number
117
+ # to returned that number of items per page. Use { page: x } to specify what page of results to return.
118
+ ##
119
+ def tiers(fields: nil, paging: nil)
120
+ self.class.get(
121
+ build_browse_api_endpoint("tiers"),
122
+ query: build_query_string_params(fields: fields, paging: paging),
123
+ headers: build_headers
124
+ )
125
+ end
126
+
127
+ ##
128
+ # Gets the settings
129
+ ##
130
+ def settings
131
+ self.class.get(
132
+ build_browse_api_endpoint("settings"),
133
+ query: build_query_string_params,
134
+ headers: build_headers
135
+ )
136
+ end
137
+
138
+ private
139
+
140
+ def build_browse_api_endpoint(type)
141
+ "#{@site_url}/#{@api_path}/#{type}"
142
+ end
143
+
144
+ def build_id_api_endpoint(type, id)
145
+ "#{build_browse_api_endpoint(type)}/#{id}"
146
+ end
147
+
148
+ def build_slug_api_endpoint(type, slug)
149
+ "#{build_browse_api_endpoint(type)}/slug/#{slug}"
150
+ end
151
+
152
+ def build_query_string_params(include: nil, fields: nil, paging: nil)
153
+ params = { key: @key }
154
+
155
+ params[:include] = include.join(",") unless include.nil? || include.empty?
156
+ params[:fields] = fields.join(",") unless fields.nil? || fields.empty?
157
+ params[:limit] = paging[:limit] unless paging.nil? || paging[:limit].nil? || paging[:limit].to_s.empty?
158
+ params[:page] = paging[:page] unless paging.nil? || paging[:page].nil? || paging[:page].to_s.empty?
159
+
160
+ params
161
+ end
162
+
163
+ def build_headers
164
+ { 'Accept-Version': @api_version }
165
+ end
166
+ end
167
+ end
data/sig/haunted.rbs ADDED
@@ -0,0 +1,3 @@
1
+ module Haunt
2
+ VERSION: String
3
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: haunted
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Dana Holt
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-09-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.20.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.20.0
27
+ description: This gem retrieves data from a Ghost CMS through the content API.
28
+ email:
29
+ - ''
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".rspec"
35
+ - ".rubocop.yml"
36
+ - CHANGELOG.md
37
+ - Gemfile
38
+ - Gemfile.lock
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - lib/haunted.rb
43
+ - lib/haunted/version.rb
44
+ - sig/haunted.rbs
45
+ homepage: https://github.com/DanaHolt/haunted
46
+ licenses:
47
+ - MIT
48
+ metadata:
49
+ homepage_uri: https://github.com/DanaHolt/haunted
50
+ source_code_uri: https://github.com/DanaHolt/haunted
51
+ changelog_uri: https://github.com/DanaHolt/haunted/blob/master/CHANGELOG.md
52
+ post_install_message: BOO!
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 2.6.0
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubygems_version: 3.3.21
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: Ghost CMS Content API client
71
+ test_files: []