gitbook-api 0.0.0.pre.alpha.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 95bed6cf4f2409bd3d05464e99ccfc67746888b3
4
+ data.tar.gz: 91e4f1a0a26d603f73ddb742cf725f7021e497e0
5
+ SHA512:
6
+ metadata.gz: 77ec0e00ebe1acccb86a505104188ff6673678efb94e8c8481e693eae877cdbbff502e052eb626d83223aa5dcf059fe81ca97613fc8f2c74b80291224a5bdc31
7
+ data.tar.gz: 8093c14f4489bdff50800a122b9ecad8f2ce48f1a364d07c5c52bc750a561d8474726f1f0abc194be9f80eaaf2d07573e108c0c9bb3f363bc3ca5680e02d0436
@@ -0,0 +1,18 @@
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
+ .idea
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --order random
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'http://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gitbook_api-gitbook.gemspec
4
+ gemspec
5
+
6
+ group :development, :test do
7
+ gem 'guard'
8
+ gem 'guard-rspec'
9
+ gem 'guard-bundler'
10
+ gem 'rb-fsevent'
11
+ gem 'growl'
12
+ end
@@ -0,0 +1,10 @@
1
+ guard 'rspec' do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
6
+
7
+ guard 'bundler' do
8
+ watch('Gemfile')
9
+ watch('gitbook_api.gemspec')
10
+ end
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016 Calvin Huang
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,20 @@
1
+ # gitbook-api
2
+ Ruby unofficial API for GitBook.
3
+
4
+ - [ ] Add / Remove star.
5
+ - [ ] Check is starred.
6
+ - [x] Fetch starred books by specify username.
7
+ - [ ] Fetch authenticated user's own books.
8
+ - [ ] Oauth2 exchange to authenticate user (Maybe use omniauth-gitbook?).
9
+ - [ ] Use username and password to authenticate user.
10
+ - [ ] Create book.
11
+ - [ ] Update book.
12
+ - [ ] Manage book setting.
13
+ - [ ] Search book in gitbook.com.
14
+ - [ ] Fetch book's detail information.
15
+
16
+ ## Contribution
17
+ I'm appreciate at any improvement, please feel free to open PR / Issue to this repo or you can contact [me](https://github.com/Calvin-Huang).
18
+
19
+ ## License
20
+ Copyright (c) [Calvin Huang](https://github.com/Calvin-Huang). This software is licensed under the [MIT License](https://github.com/Calvin-Huang/CHRealHideUIView/blob/master/LICENSE).
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new
6
+
7
+ desc 'Run specs'
8
+ task :default => :spec
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/gitbook_api/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Calvin Huang"]
6
+ gem.email = ["calvin.peak@capslock.tw"]
7
+ gem.description = %q{Ruby unofficial API for gitbook.com.}
8
+ gem.summary = %q{Ruby unofficial API for gitbook.com.}
9
+ gem.homepage = "https://github.com/Calvin-Huang/gitbook-api"
10
+ gem.license = "MIT"
11
+
12
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
13
+ gem.files = `git ls-files`.split("\n")
14
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ gem.name = "gitbook-api"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = GitBook::VERSION
18
+
19
+ gem.add_dependency 'omniauth', '~> 1.0'
20
+ gem.add_dependency 'omniauth-oauth2', '>= 1.1.1', '< 2.0'
21
+ gem.add_dependency 'omniauth-gitbook', '>= 1.0.0', '< 2.0'
22
+ gem.add_dependency 'nokogiri', '~> 1.0'
23
+ gem.add_development_dependency 'rspec', '~> 3.0'
24
+ gem.add_development_dependency 'rack-test'
25
+ gem.add_development_dependency 'simplecov'
26
+ gem.add_development_dependency 'webmock'
27
+ end
@@ -0,0 +1,35 @@
1
+ require 'rake'
2
+
3
+ require 'gitbook_api/version'
4
+ require 'gitbook_api/configurable'
5
+ require 'gitbook_api/api'
6
+
7
+ module GitBook
8
+ class << self
9
+ include GitBook::Configurable
10
+
11
+ def api
12
+ # Return a new instance if option in GitBook different to GItBook::API
13
+ return @api if defined?(@api) && @api.same_options?(options)
14
+ @api = GitBook::API.new(options)
15
+ end
16
+
17
+ def logger
18
+ # config.logger
19
+ end
20
+
21
+ private
22
+
23
+ def respond_to_missing?(method_name, include_private=false)
24
+ api.respond_to?(method_name, include_private)
25
+ end
26
+
27
+ def method_missing(method_name, *args, &block)
28
+ if api.respond_to?(method_name)
29
+ return api.send(method_name, *args, &block)
30
+ end
31
+
32
+ super
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,15 @@
1
+ require 'gitbook_api/configurable'
2
+ require 'gitbook_api/api/stars'
3
+
4
+ module GitBook
5
+ class API
6
+ include GitBook::Configurable
7
+ include GitBook::API::Stars
8
+
9
+ def initialize(options = {})
10
+ GitBook::Configurable.keys.each do |key|
11
+ instance_variable_set(:"@#{key}", options[key] || GitBook.options.instance_variable_get(:"@#{key}"))
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,27 @@
1
+ require 'faraday'
2
+ require 'nokogiri'
3
+ require 'json'
4
+
5
+ module GitBook
6
+ class API
7
+ module Stars
8
+ def stars(username = nil)
9
+ response = Faraday.get "#{base_url}/@#{username}/starred"
10
+
11
+ html_doc = Nokogiri::HTML(response.body)
12
+ books = html_doc.css('#AuthorProfile .Books:first-child .Book')
13
+ books.map do |book|
14
+ anchor = book.css('h4.title > a')
15
+ book_infos = anchor.attr('href').text.split('/')
16
+
17
+ {
18
+ id: book_infos.last,
19
+ author: book_infos[book_infos.length - 2],
20
+ title: anchor.text,
21
+ description: book.css('p.description').text
22
+ }
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,40 @@
1
+ module GitBook
2
+ module Configurable
3
+ attr_accessor :base_url, :api_url, :username, :password, :access_token
4
+
5
+ def self.keys
6
+ @keys ||= [
7
+ :base_url,
8
+ :api_url,
9
+ :username,
10
+ :password,
11
+ :access_token,
12
+ ]
13
+ end
14
+
15
+ def configure
16
+ yield self
17
+ end
18
+
19
+ def options
20
+ Hash[GitBook::Configurable.keys.map{ |key| [key, send(key)] }]
21
+ end
22
+
23
+ def same_options?(options_to_compare)
24
+ options_to_compare.hash == options.hash
25
+ end
26
+
27
+ def reset_configuration
28
+ GitBook::Configurable.keys.each { |key| send("#{key}=", nil) }
29
+ end
30
+
31
+ # Getter will assign default value
32
+ def base_url
33
+ @base_url ||= 'https://www.gitbook.com'
34
+ end
35
+
36
+ def api_url
37
+ @api_url ||= 'https://api.gitbook.com'
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module GitBook
2
+ VERSION = '0.0.0-alpha.1'
3
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe GitBook::API do
4
+ let(:tester_username) { 'gitbookreader-tester' }
5
+
6
+ describe GitBook::API::Stars do
7
+ describe '#list' do
8
+ it "should request user's stars and convert to json format" do
9
+ stars = GitBook.api.stars tester_username
10
+ expected_data = [
11
+ {
12
+ id: 'test',
13
+ author: 'calvin-huang',
14
+ title: 'Test book for GitBookReader',
15
+ description: "iOS repo - https://github.com/Calvin-Huang/GitbookReader-iOS\r\nRails repo - https://github.com/Calvin-Huang/GitbookReader\r\nThis repo will not remove for unit test,",
16
+ }
17
+ ]
18
+
19
+ expect(stars).to eq(expected_data)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,95 @@
1
+ require 'spec_helper'
2
+
3
+ describe GitBook::Configurable do
4
+ describe '#options' do
5
+ api = GitBook::API.new
6
+
7
+ it 'should have options hash map and it have same value with variable' do
8
+ expect(api.options[:base_url]).to eq(api.base_url)
9
+ expect(api.options[:api_url]).to eq(api.api_url)
10
+ expect(api.options[:username]).to eq(api.username)
11
+ expect(api.options[:password]).to eq(api.password)
12
+ expect(api.options[:access_token]).to eq(api.access_token)
13
+ end
14
+ end
15
+
16
+ context 'Initialize GitBook::API with default settings' do
17
+ subject do
18
+ GitBook::API.new
19
+ end
20
+
21
+ it 'should have default base url' do
22
+ expect(subject.base_url).to eq('https://www.gitbook.com')
23
+ expect(subject.options[:base_url]).to eq('https://www.gitbook.com')
24
+ end
25
+
26
+ it 'should have default api url' do
27
+ expect(subject.api_url).to eq('https://api.gitbook.com')
28
+ expect(subject.options[:api_url]).to eq('https://api.gitbook.com')
29
+ end
30
+
31
+ it 'should not have usename default' do
32
+ expect(subject.username).to be_nil
33
+ expect(subject.options[:username]).to be_nil
34
+ end
35
+
36
+ it 'should not have password default' do
37
+ expect(subject.password).to be_nil
38
+ expect(subject.options[:password]).to be_nil
39
+ end
40
+
41
+ it 'should not have access token default' do
42
+ expect(subject.access_token).to be_nil
43
+ expect(subject.options[:access_token]).to be_nil
44
+ end
45
+ end
46
+
47
+ context 'Configure with block' do
48
+ let(:base_url) { 'https://enterprise.gitbook.com' }
49
+ let(:api_url) { 'https://api.enterprise.gitbook.com'}
50
+ let(:username) { 'foo' }
51
+ let(:password) { 'boo' }
52
+ let(:access_token) { 'ewjdo1zjqweo123@$6933' }
53
+
54
+ subject do
55
+ GitBook.configure do |config|
56
+ config.base_url = base_url
57
+ config.api_url = api_url
58
+ config.username = username
59
+ config.password = password
60
+ config.access_token = access_token
61
+ end
62
+
63
+ GitBook.api
64
+ end
65
+
66
+ it 'should match base url' do
67
+ expect(subject.base_url).to eq(base_url)
68
+ expect(subject.options[:base_url]).to eq(base_url)
69
+ end
70
+
71
+ it 'should match api url' do
72
+ expect(subject.api_url).to eq(api_url)
73
+ expect(subject.options[:api_url]).to eq(api_url)
74
+ end
75
+
76
+ it 'should match username' do
77
+ expect(subject.username).to eq(username)
78
+ expect(subject.options[:username]).to eq(username)
79
+ end
80
+
81
+ it 'should match password' do
82
+ expect(subject.password).to eq(password)
83
+ expect(subject.options[:password]).to eq(password)
84
+ end
85
+
86
+ it 'should match auth_token' do
87
+ expect(subject.access_token).to eq(access_token)
88
+ expect(subject.options[:access_token]).to eq(access_token)
89
+ end
90
+ end
91
+
92
+ after(:all) do
93
+ GitBook.reset_configuration
94
+ end
95
+ end
@@ -0,0 +1,9 @@
1
+ require 'rspec'
2
+ require 'rack/test'
3
+ # require 'webmock/rspec'
4
+ require 'gitbook_api'
5
+
6
+ RSpec.configure do |config|
7
+ # config.include WebMock::API
8
+ config.include Rack::Test::Methods
9
+ end
metadata ADDED
@@ -0,0 +1,187 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gitbook-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0.pre.alpha.1
5
+ platform: ruby
6
+ authors:
7
+ - Calvin Huang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: omniauth
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: omniauth-oauth2
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.1.1
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: '2.0'
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 1.1.1
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: '2.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: omniauth-gitbook
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 1.0.0
54
+ - - "<"
55
+ - !ruby/object:Gem::Version
56
+ version: '2.0'
57
+ type: :runtime
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 1.0.0
64
+ - - "<"
65
+ - !ruby/object:Gem::Version
66
+ version: '2.0'
67
+ - !ruby/object:Gem::Dependency
68
+ name: nokogiri
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: '1.0'
74
+ type: :runtime
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - "~>"
79
+ - !ruby/object:Gem::Version
80
+ version: '1.0'
81
+ - !ruby/object:Gem::Dependency
82
+ name: rspec
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '3.0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - "~>"
93
+ - !ruby/object:Gem::Version
94
+ version: '3.0'
95
+ - !ruby/object:Gem::Dependency
96
+ name: rack-test
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ - !ruby/object:Gem::Dependency
110
+ name: simplecov
111
+ requirement: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ type: :development
117
+ prerelease: false
118
+ version_requirements: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ - !ruby/object:Gem::Dependency
124
+ name: webmock
125
+ requirement: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ type: :development
131
+ prerelease: false
132
+ version_requirements: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ description: Ruby unofficial API for gitbook.com.
138
+ email:
139
+ - calvin.peak@capslock.tw
140
+ executables: []
141
+ extensions: []
142
+ extra_rdoc_files: []
143
+ files:
144
+ - ".gitignore"
145
+ - ".rspec"
146
+ - Gemfile
147
+ - Guardfile
148
+ - LICENSE
149
+ - README.md
150
+ - Rakefile
151
+ - gitbook_api.gemspec
152
+ - lib/gitbook_api.rb
153
+ - lib/gitbook_api/api.rb
154
+ - lib/gitbook_api/api/stars.rb
155
+ - lib/gitbook_api/configurable.rb
156
+ - lib/gitbook_api/version.rb
157
+ - spec/gitbook_api/api_spec.rb
158
+ - spec/gitbook_api/configure_spec.rb
159
+ - spec/spec_helper.rb
160
+ homepage: https://github.com/Calvin-Huang/gitbook-api
161
+ licenses:
162
+ - MIT
163
+ metadata: {}
164
+ post_install_message:
165
+ rdoc_options: []
166
+ require_paths:
167
+ - lib
168
+ required_ruby_version: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ required_rubygems_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - ">"
176
+ - !ruby/object:Gem::Version
177
+ version: 1.3.1
178
+ requirements: []
179
+ rubyforge_project:
180
+ rubygems_version: 2.6.6
181
+ signing_key:
182
+ specification_version: 4
183
+ summary: Ruby unofficial API for gitbook.com.
184
+ test_files:
185
+ - spec/gitbook_api/api_spec.rb
186
+ - spec/gitbook_api/configure_spec.rb
187
+ - spec/spec_helper.rb