leanpub_api 0.0.3

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: 37bc58a39fa57fd3944ef066cbda7f59948b25e6
4
+ data.tar.gz: 171a4ae7ebae429ecdddb68e3a0b9ec732c2a815
5
+ SHA512:
6
+ metadata.gz: be1129d41c89d80c54b0810fd266f406dfb767684ddff8686c85c76a00b66adf7de2dfc676f73b0570a67cdce795b86c2399a9b4caee9f92aab8e643786a0e15
7
+ data.tar.gz: 66083bedd5b5305584f475843a323820fb2ffe7239f503977e9152bd5170095ef13b7542acff520142b61f822a1a3fdbc1082762c144caa7aac6dbb618201784
data/.gitignore ADDED
@@ -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
+ .env
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in leanpub.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Aaron Sumner
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # Leanpub API wrapper for Ruby
2
+
3
+ A simple Ruby wrapper for the [Leanpub API](https://leanpub.com/help/api). Currently supports the API's book summary functions, with aspirations to do
4
+ more.
5
+
6
+ **Disclaimer:** Leanpub is a service of Ruboss Technology Corporation, a
7
+ corporation incorporated in British Columbia, Canada. I self-publish a book
8
+ using their service, but am otherwise not affiliated with them in any way.
9
+
10
+ ## Requirements
11
+
12
+ This library has been tested against the following Ruby versions:
13
+
14
+ - 2.0.0
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ gem 'leanpub_api'
21
+
22
+ And then execute:
23
+
24
+ $ bundle
25
+
26
+ Or install it yourself as:
27
+
28
+ $ gem install leanpub_api
29
+
30
+ ## Usage
31
+
32
+ You'll need your book slug and API key values, both of which can be retrieved
33
+ by [accessing your Leanpub account](https://leanpub.com/dashboard).
34
+
35
+ The gem currently supports the Leanpub API's book summary functions.
36
+
37
+ ```ruby
38
+ require 'leanpub_api_'
39
+
40
+ book = LeanpubAPI::BookSummary.new('<book-slug>', '<api-key>')
41
+
42
+ puts book.title
43
+ puts book.subtitle
44
+ puts book.slug
45
+ puts book.author_string
46
+ puts book.url
47
+ puts book.title_page_url
48
+ puts book.minimum_price
49
+ puts book.suggested_price
50
+ puts book.total_copies_sold
51
+ puts book.total_revenue
52
+ puts book.possible_reader_count
53
+ ```
54
+
55
+ ## Development
56
+
57
+ Tests expect a `.env` file at the project's root, with the following
58
+ environment variables:
59
+
60
+ - `LEANPUB_BOOK_SLUG` Your book's Leanpub slug
61
+ - `LEANPUB_API_KEY` Your Leanpub API key
62
+
63
+ ## TODO
64
+
65
+ Add wrappers for other API functionality:
66
+
67
+ - Sales summary
68
+ - All sales data
69
+ - Preview functions
70
+ - Publish functions
71
+ - Preview/publish job status
72
+ - Coupon management
73
+
74
+ Improve configuration
75
+
76
+ ## Contributing
77
+
78
+ 1. Fork it
79
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
80
+ 3. Commit your changes, with tests (`git commit -am 'Add some feature'`)
81
+ 4. Push to the branch (`git push origin my-new-feature`)
82
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.test_files = FileList['spec/lib/leanpub_api/*_spec.rb']
6
+ t.verbose = true
7
+ end
8
+
9
+ task :default => :test
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'leanpub_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "leanpub_api"
8
+ spec.version = LeanpubAPI::VERSION
9
+ spec.authors = ["Aaron Sumner"]
10
+ spec.email = ["asumner@mac.com"]
11
+ spec.description = %q{A simple Ruby wrapper for the Leanpub API.}
12
+ spec.summary = %q{A simple Ruby wrapper for the Leanpub API. Not affiliated with Leanpub or Ruboss Technology Corporation.}
13
+ spec.homepage = "https://github.com/ruralocity/leanpub-api"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "httparty", "~> 0.12.0"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake", "~> 10.1.1"
25
+ spec.add_development_dependency "webmock", "~> 1.14.0"
26
+ spec.add_development_dependency "vcr", "~> 2.6.0"
27
+ spec.add_development_dependency "dotenv", "~> 0.9.0"
28
+ end
@@ -0,0 +1,29 @@
1
+ module LeanpubAPI
2
+ class BookSummary
3
+ include HTTParty
4
+ base_uri 'https://leanpub.com'
5
+
6
+ def initialize(book_slug, api_key)
7
+ @book_slug = book_slug
8
+ @api_key = api_key
9
+ end
10
+
11
+ def info(force = false)
12
+ force ? @info = get_info : @info ||= get_info
13
+ end
14
+
15
+ def method_missing(name, *args, &block)
16
+ info.has_key?(name.to_s) ? info[name.to_s] : super
17
+ end
18
+
19
+ private
20
+
21
+ def get_info
22
+ self.class.get(path)
23
+ end
24
+
25
+ def path
26
+ "/#{@book_slug}.json?api_key=#{@api_key}"
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module LeanpubAPI
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,8 @@
1
+ require "leanpub_api/version"
2
+ require "httparty"
3
+
4
+ module LeanpubAPI
5
+ Dir[File.dirname(__FILE__) + '/leanpub_api/*.rb'].each do |file|
6
+ require file
7
+ end
8
+ end
@@ -0,0 +1,61 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://leanpub.com/<LEANPUB_BOOK_SLUG>.json?api_key=<LEANPUB_API_KEY>
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers: {}
10
+ response:
11
+ status:
12
+ code: 200
13
+ message: OK
14
+ headers:
15
+ Cache-Control:
16
+ - max-age=0, private, must-revalidate
17
+ - no-cache="set-cookie"
18
+ Content-Type:
19
+ - application/json; charset=utf-8
20
+ Date:
21
+ - Thu, 13 Feb 2014 05:42:21 GMT
22
+ Etag:
23
+ - '"1ad41ab72329fc3914991bdc9c94ae44"'
24
+ Server:
25
+ - nginx + Phusion Passenger 4.0.29
26
+ Set-Cookie:
27
+ - AWSELB=BF7915350C754439EF4952CB0CB801868D298314801C9FE6ECB1E0A62F0A1CC471D3F7B3BC89A85AAC39AEABF40B5E0269382C4B41DE08103BD974E66E11B0D967B3BF599E;PATH=/
28
+ - _lpub_session=BAh7BkkiD3Nlc3Npb25faWQGOgZFRkkiJTJlNTczNGU1YmVkNzU4ZTg4MTA5MmI5ODRjYTM1NjBhBjsAVA%3D%3D--8e1db4d52cb04c458d447acd0e92ddb5c7c071b9;
29
+ path=/; secure; HttpOnly
30
+ - cookied-flash=%7B%7D; path=/; secure
31
+ - domain-info=%7B%22root%22%3A%22https%3A%2F%2Fleanpub.com%2F%22%7D; path=/;
32
+ expires=Fri, 14-Feb-2014 05:42:21 GMT; secure
33
+ - remember_token=d1248d8ebf3303d1f74c2788df68b618f841c5e2; path=/; expires=Fri,
34
+ 13-Feb-2015 05:42:21 GMT; secure
35
+ - user-info=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT; secure
36
+ Status:
37
+ - 200 OK
38
+ Strict-Transport-Security:
39
+ - max-age=31536000
40
+ X-Powered-By:
41
+ - Phusion Passenger 4.0.29
42
+ X-Rack-Cache:
43
+ - miss
44
+ X-Request-Id:
45
+ - b65392997af166ca2d77300fe819d548
46
+ X-Runtime:
47
+ - '0.059839'
48
+ X-Ua-Compatible:
49
+ - IE=Edge,chrome=1
50
+ Content-Length:
51
+ - '443'
52
+ Connection:
53
+ - keep-alive
54
+ body:
55
+ encoding: UTF-8
56
+ string: '{"slug":"<LEANPUB_BOOK_SLUG>","subtitle":"A practical approach to test-driven
57
+ development","title":"Everyday Rails Testing with RSpec","total_copies_sold":3513,"total_revenue":"12345.67","author_string":"Aaron
58
+ Sumner","url":"http://leanpub.com/<LEANPUB_BOOK_SLUG>","title_page_url":"https://s3.amazonaws.com/titlepages.leanpub.com/<LEANPUB_BOOK_SLUG>/original?1392171401","minimum_price":"14.0","suggested_price":"19.0","possible_reader_count":12}'
59
+ http_version:
60
+ recorded_at: Thu, 13 Feb 2014 05:41:53 GMT
61
+ recorded_with: VCR 2.6.0
@@ -0,0 +1,72 @@
1
+ require_relative '../../spec_helper'
2
+
3
+ describe LeanpubAPI::BookSummary do
4
+ describe "default attributes" do
5
+ it "include HTTParty methods" do
6
+ LeanpubAPI::BookSummary.must_include HTTParty
7
+ end
8
+
9
+ it "have the base URL set to the Leanpub API endpoint" do
10
+ LeanpubAPI::BookSummary.base_uri.must_equal 'https://leanpub.com'
11
+ end
12
+ end
13
+
14
+ describe "GET book info" do
15
+ let(:summary) { LeanpubAPI::BookSummary.new(book_slug, api_key) }
16
+
17
+ before :each do
18
+ VCR.insert_cassette 'book_summary', record: :new_episodes
19
+ end
20
+
21
+ after :each do
22
+ VCR.eject_cassette
23
+ end
24
+
25
+ it "has data" do
26
+ summary.must_respond_to :info
27
+ end
28
+
29
+ it "parses the JSON response to a hash" do
30
+ summary.info.must_be_instance_of Hash
31
+ end
32
+
33
+ it "performs the request and gets the data" do
34
+ summary.info["author_string"].must_equal "Aaron Sumner"
35
+ summary.info["title"].must_equal "Everyday Rails Testing with RSpec"
36
+ summary.info["subtitle"].must_equal "A practical approach to test-driven development"
37
+ summary.info["total_copies_sold"].must_equal 3513
38
+ summary.info["total_revenue"].must_equal "12345.67"
39
+ summary.info["minimum_price"].must_equal "14.0"
40
+ summary.info["suggested_price"].must_equal "19.0"
41
+ end
42
+
43
+ describe "dynamic attributes" do
44
+ before do
45
+ summary.info
46
+ end
47
+
48
+ it "returns an attribute if present in the info" do
49
+ summary.title.must_equal "Everyday Rails Testing with RSpec"
50
+ end
51
+
52
+ it "raises method missing if an attribute is not present in the info" do
53
+ lambda { summary.bad_attribute }.must_raise NoMethodError
54
+ end
55
+ end
56
+
57
+ describe "caching" do
58
+ before do
59
+ summary.info
60
+ stub_request(:any, /leanpub.com/).to_timeout
61
+ end
62
+
63
+ it "caches the info" do
64
+ summary.info.must_be_instance_of Hash
65
+ end
66
+
67
+ it "refreshes the info if forced" do
68
+ lambda { summary.info(true) }.must_raise Timeout::Error
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,29 @@
1
+ require_relative '../lib/leanpub_api'
2
+
3
+ require 'minitest/autorun'
4
+ require 'webmock/minitest'
5
+ require 'vcr'
6
+ require 'dotenv'
7
+
8
+ Dotenv.load
9
+
10
+ VCR.configure do |config|
11
+ config.cassette_library_dir = 'spec/fixtures/leanpub_cassettes'
12
+ config.hook_into :webmock
13
+
14
+ config.filter_sensitive_data("<LEANPUB_BOOK_SLUG>") do
15
+ book_slug
16
+ end
17
+
18
+ config.filter_sensitive_data("<LEANPUB_API_KEY>") do
19
+ api_key
20
+ end
21
+ end
22
+
23
+ def book_slug
24
+ ENV.fetch 'LEANPUB_BOOK_SLUG'
25
+ end
26
+
27
+ def api_key
28
+ ENV.fetch 'LEANPUB_API_KEY'
29
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: leanpub_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Aaron Sumner
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-13 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.12.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.12.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 10.1.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 10.1.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.14.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 1.14.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: vcr
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 2.6.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: 2.6.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: dotenv
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: 0.9.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: 0.9.0
97
+ description: A simple Ruby wrapper for the Leanpub API.
98
+ email:
99
+ - asumner@mac.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - leanpub_api.gemspec
110
+ - lib/leanpub_api.rb
111
+ - lib/leanpub_api/book_summary.rb
112
+ - lib/leanpub_api/version.rb
113
+ - spec/fixtures/leanpub_cassettes/book_summary.yml
114
+ - spec/lib/leanpub_api/book_summary_spec.rb
115
+ - spec/spec_helper.rb
116
+ homepage: https://github.com/ruralocity/leanpub-api
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.2.2
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: A simple Ruby wrapper for the Leanpub API. Not affiliated with Leanpub or
140
+ Ruboss Technology Corporation.
141
+ test_files:
142
+ - spec/fixtures/leanpub_cassettes/book_summary.yml
143
+ - spec/lib/leanpub_api/book_summary_spec.rb
144
+ - spec/spec_helper.rb