buttercms-ruby 1.0.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
+ SHA1:
3
+ metadata.gz: f72c57972718778708f150fa6a31a9576c1c299a
4
+ data.tar.gz: 74afaa24b20838aee3b6fe474e68a63dbcd9b771
5
+ SHA512:
6
+ metadata.gz: c651afb1629f09e01435d04b48be8d67c1eeb02c5da3d06c63bcb827d0763434b110f98b9abdc73c60445bfed7f5366177d7f17692bd235913d650766bbca411
7
+ data.tar.gz: ef78a1f79753a75fd5e0500c045d9da04861c98057040cacb1f5ea4988bc9a2597e80d8b0ec71d2abf1822116026053e8216ffd0f34e5dceb0001257e0bf948b
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .DS_Store
19
+ .ruby-version
20
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --format documentation --color
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rake'
6
+
7
+ group :development, :test do
8
+ gem 'rspec-rails'
9
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Butter
4
+ https://buttercms.com/
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in
14
+ all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # ButterCMS API Ruby Client
2
+
3
+ ## Setup
4
+
5
+ To setup your project, follow these steps:
6
+
7
+ 1. Install using `gem install buttercms-ruby` or by adding to your `Gemfile`:
8
+
9
+ ```ruby
10
+ gem 'buttercms-ruby', '~>1.0.0'
11
+ ```
12
+
13
+ 2. Set your API token.
14
+
15
+ ```ruby
16
+ require 'buttercms-ruby'
17
+
18
+ ButterCMS::api_token = "YourToken"
19
+ ```
20
+
21
+ ## Quick Start
22
+
23
+ ```ruby
24
+ posts = ButterCMS::Post.all(page: 1, page_size: 10)
25
+ puts posts.first.title
26
+ puts posts.meta.next_page
27
+
28
+ post = ButterCMS::Post.find("post-slug")
29
+ puts post.title
30
+
31
+ author = ButterCMS::Author.find("author-slug")
32
+ puts author.first_name
33
+
34
+ category = ButterCMS::Category.find("author-slug")
35
+ puts category.name
36
+
37
+ rss_feed = ButterCMS::Feed.find(:rss)
38
+ puts rss_feed.data
39
+ ```
40
+
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new('spec')
4
+
5
+ task :default => :spec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.unshift(File.join(File.dirname(__FILE__), 'lib'))
3
+
4
+ require 'buttercms/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "buttercms-ruby"
8
+ s.version = ButterCMS::VERSION
9
+ s.required_ruby_version = '>= 1.9.3'
10
+ s.require_paths = ["lib"]
11
+ s.summary = 'A simple Ruby client for the buttercms.com REST API'
12
+ s.description = 'Butter is a blogging platform loved by engineers. See https://buttercms.com for details.'
13
+ s.authors = ["ButterCMS"]
14
+ s.email= ["support@buttercms.com"]
15
+ s.homepage = "https://buttercms.com/docs"
16
+ s.license = 'MIT'
17
+
18
+ s.add_dependency 'rest-client'
19
+
20
+ s.add_development_dependency 'rspec', '~> 2.7'
21
+ s.add_development_dependency 'webmock'
22
+
23
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
24
+ s.files = `git ls-files`.split("\n")
25
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
26
+ end
@@ -0,0 +1,39 @@
1
+ require 'json'
2
+ require 'rest_client'
3
+ require 'ostruct'
4
+
5
+ require 'buttercms/hash_to_object'
6
+ require 'buttercms/butter_collection'
7
+ require 'buttercms/butter_resource'
8
+ require 'buttercms/author'
9
+ require 'buttercms/category'
10
+ require 'buttercms/post'
11
+ require 'buttercms/feed'
12
+
13
+ module ButterCMS
14
+ @api_url = 'https://api.buttercms.com/v2'
15
+ @token = nil
16
+
17
+ def self.api_token=(token)
18
+ @token = token
19
+ end
20
+
21
+ def self.token
22
+ @token
23
+ end
24
+
25
+ def self.endpoint
26
+ @api_url
27
+ end
28
+
29
+ def self.request(path, options = {})
30
+ raise ArgumentError.new "Please set your API token" unless token
31
+
32
+ response = RestClient.get(endpoint + path,
33
+ {accept: :json, authorization: "Token #{@token}", params: options})
34
+
35
+
36
+ JSON.parse(response.body)
37
+ end
38
+ end
39
+
@@ -0,0 +1,7 @@
1
+ module ButterCMS
2
+ class Author < ButterResource
3
+ def self.resource_path
4
+ "/authors"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,21 @@
1
+ module ButterCMS
2
+ class ButterCollection
3
+ include Enumerable
4
+
5
+ attr_reader :meta
6
+
7
+ def initialize(klass, json)
8
+ data = json["data"]
9
+ meta = json["meta"]
10
+
11
+ @meta = HashToObject.convert(meta) if meta
12
+ @items = data.map {|o| klass.new("data" => o) }
13
+ end
14
+
15
+ def each(&block)
16
+ @items.each do |member|
17
+ block.call(member)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,54 @@
1
+ module ButterCMS
2
+ class ButterResource
3
+ attr_reader :meta, :data
4
+
5
+ def initialize(json)
6
+ data = json["data"]
7
+ meta = json["meta"]
8
+
9
+ @json = json
10
+ @data = HashToObject.convert(data)
11
+ @meta = HashToObject.convert(meta) if meta
12
+
13
+ data.each do |key, value|
14
+ instance_variable_set("@#{key}", @data.send(key))
15
+ self.class.send(:attr_reader, key)
16
+ end
17
+ end
18
+
19
+ def inspect
20
+ id_string = (self.respond_to?(:id) && !self.id.nil?) ? " id=#{self.id}" : ""
21
+ "#<#{self.class}:0x#{self.object_id.to_s(16)}#{id_string}> JSON: " + JSON.pretty_generate(@json)
22
+ end
23
+
24
+ def self.endpoint(id = '')
25
+ resource_path + '/' + id.to_s
26
+ end
27
+
28
+ def self.resource_path
29
+ raise "resource_path must be set"
30
+ end
31
+
32
+ def self.all(options = {})
33
+ response = ButterCMS.request(self.endpoint, options)
34
+
35
+ self.create_collection(response)
36
+ end
37
+
38
+ def self.find(id, options = {})
39
+ response = ButterCMS.request(self.endpoint(id), options)
40
+
41
+ self.create_object(response)
42
+ end
43
+
44
+ private
45
+
46
+ def self.create_collection(response)
47
+ ButterCollection.new(self, response)
48
+ end
49
+
50
+ def self.create_object(response)
51
+ self.new(response)
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,7 @@
1
+ module ButterCMS
2
+ class Category < ButterResource
3
+ def self.resource_path
4
+ "/categories"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module ButterCMS
2
+ class Feed < ButterResource
3
+ def self.resource_path
4
+ "/feeds"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module ButterCMS
2
+ class HashToObject
3
+ def self.convert(hash)
4
+ JSON.parse(hash.to_json, object_class: OpenStruct)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module ButterCMS
2
+ class Post < ButterResource
3
+ def self.resource_path
4
+ "/posts"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module ButterCMS
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe ButterCMS do
4
+ describe '.request' do
5
+ context 'with an api token' do
6
+ before do
7
+ ButterCMS.stub(:token).and_return('test123')
8
+ end
9
+
10
+ it 'should make an api request' do
11
+ stub_request(:get, ButterCMS.endpoint).to_return(body: JSON.generate({data: {test: 'test'}}))
12
+ expect{ ButterCMS.request('') }.to_not raise_error
13
+ end
14
+ end
15
+
16
+ context 'without an api token' do
17
+ it 'should throw an argument error' do
18
+ expect{ ButterCMS.request() }.to raise_error(ArgumentError)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe ButterCMS::ButterCollection do
4
+ let(:json) { {"data" => ["foo"], "meta" => {}} }
5
+ let(:klass) { double('klass', :new => 'bar') }
6
+
7
+ it 'implements #meta' do
8
+ collection = ButterCMS::ButterCollection.new(klass, json)
9
+
10
+ expect(collection.meta).to be_a(OpenStruct)
11
+ end
12
+
13
+ it 'implements #count' do
14
+ collection = ButterCMS::ButterCollection.new(klass, json)
15
+
16
+ expect(collection.count).to eq 1
17
+ end
18
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe ButterCMS::ButterResource do
4
+ describe '.all' do
5
+ before do
6
+ ButterCMS.stub(:token).and_return('test123')
7
+ ButterCMS.stub(:request).and_return({"data" => [{"attribute" => 'test'}]})
8
+
9
+ ButterCMS::ButterResource.stub(:resource_path).
10
+ and_return('')
11
+ end
12
+
13
+ it 'should make a request with the correct endpoint' do
14
+ expect(ButterCMS).to receive(:request).with('/', {})
15
+ ButterCMS::ButterResource.all()
16
+ end
17
+
18
+ it 'should return a collection' do
19
+ objects = ButterCMS::ButterResource.all
20
+ expect(objects).to be_a(ButterCMS::ButterCollection)
21
+ expect(objects.first).to be_a(ButterCMS::ButterResource)
22
+ end
23
+ end
24
+
25
+ describe '.find' do
26
+ before do
27
+ ButterCMS.stub(:token).and_return('test123')
28
+ ButterCMS.stub(:request).and_return({"data" => {"attribute" => 'test'}})
29
+
30
+ ButterCMS::ButterResource.stub(:resource_path).
31
+ and_return('')
32
+ end
33
+
34
+ it 'should make a request with the correct endpoint' do
35
+ expect(ButterCMS).to receive(:request).with('/1', {})
36
+ ButterCMS::ButterResource.find(1)
37
+ end
38
+
39
+ it 'should return one object' do
40
+ expect(ButterCMS::ButterResource.find(1)).to be_a(ButterCMS::ButterResource)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe ButterCMS::HashToObject do
4
+ describe '.convert' do
5
+ it 'converts hash to object' do
6
+ hash = {
7
+ "key1" => "value",
8
+ "key2" => {
9
+ "nested_key" => "nested value"
10
+ }
11
+ }
12
+
13
+ obj = ButterCMS::HashToObject.convert(hash)
14
+
15
+ expect(obj.key1).to eq 'value'
16
+ expect(obj.key2.nested_key).to eq "nested value"
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,8 @@
1
+ $:.unshift File.expand_path('..', __FILE__)
2
+ $:.unshift File.expand_path('../../lib', __FILE__)
3
+
4
+ require 'rubygems'
5
+ require 'bundler/setup'
6
+ require 'rspec'
7
+ require 'webmock/rspec'
8
+ require 'buttercms-ruby'
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: buttercms-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - ButterCMS
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-02-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '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'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: webmock
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Butter is a blogging platform loved by engineers. See https://buttercms.com
56
+ for details.
57
+ email:
58
+ - support@buttercms.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - Gemfile
66
+ - LICENSE
67
+ - README.md
68
+ - Rakefile
69
+ - VERSION
70
+ - buttercms-ruby.gemspec
71
+ - lib/buttercms-ruby.rb
72
+ - lib/buttercms/author.rb
73
+ - lib/buttercms/butter_collection.rb
74
+ - lib/buttercms/butter_resource.rb
75
+ - lib/buttercms/category.rb
76
+ - lib/buttercms/feed.rb
77
+ - lib/buttercms/hash_to_object.rb
78
+ - lib/buttercms/post.rb
79
+ - lib/buttercms/version.rb
80
+ - spec/lib/butter-ruby_spec.rb
81
+ - spec/lib/buttercms/butter_collection_spec.rb
82
+ - spec/lib/buttercms/butter_resource_spec.rb
83
+ - spec/lib/buttercms/hash_to_object_spec.rb
84
+ - spec/spec_helper.rb
85
+ homepage: https://buttercms.com/docs
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: 1.9.3
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.4.5.1
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: A simple Ruby client for the buttercms.com REST API
109
+ test_files:
110
+ - spec/lib/butter-ruby_spec.rb
111
+ - spec/lib/buttercms/butter_collection_spec.rb
112
+ - spec/lib/buttercms/butter_resource_spec.rb
113
+ - spec/lib/buttercms/hash_to_object_spec.rb
114
+ - spec/spec_helper.rb