boogle 0.0.0 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eef6b4fa7ee0f2a43dfd7b67ecf43e077f5faf67
4
- data.tar.gz: 26f34e9ea8a84f292dc787f9c6ad0acec08a438a
3
+ metadata.gz: 240af5c6cda7443ced93e8e4f40cc361bf1f7cf2
4
+ data.tar.gz: aadf3010e3b63d7f39188284dfb163af1c85f674
5
5
  SHA512:
6
- metadata.gz: 2d5b5b84d26beeac3c60ab9721483cec112e18db5053fb1ac5a85eb32b8401e3819ee0c06d2e3318dcdcacbe34e9dfb3b8680c8f44a8a5731050bc3bb0ac2a0c
7
- data.tar.gz: d15c90229fd3a39b2f896df054fa28c53a9977d8cb1fbe86a58ea65fdd0a38baef9b8d5650e8333268b36eebd4fcd01bca3c22feeebe0bc2084d7e452a463749
6
+ metadata.gz: 1a1b13067929259131a322bab92ed214fdfb0b86cedb2f07c9e0848dc919dab019fc216e2c7b948f9396fbaa75ac52d10c604299cccfab86c15f24ed952c0b8a
7
+ data.tar.gz: 137ac017c58ee136fcb90773247231f54521a1d913246f74d7e03601e5503b56e0e6797c3ba3f7b2e77fadf8926e0f312b5aff028bf3b533b03b2e75fea4e030
data/.byebug_history ADDED
File without changes
data/README.md CHANGED
@@ -1,9 +1,6 @@
1
1
  # Boogle
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/boogle`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
6
-
3
+ Boogle is a gem that takes off your hands from the haslle of dealing with [google books APIs](https://www.google.com)
7
4
  ## Installation
8
5
 
9
6
  Add this line to your application's Gemfile:
@@ -21,21 +18,66 @@ Or install it yourself as:
21
18
  $ gem install boogle
22
19
 
23
20
  ## Usage
21
+ #### 1 - Set the base connection
22
+ ```ruby
23
+ boogle = Boogle::Client.new(key: 'api_key')
24
+ ```
25
+
26
+ #### 2 - search for books
27
+ search by key word only
28
+ ```ruby
29
+ boogle.volume.search(keyword: 'flowers')
30
+ ```
31
+ you can choose to search in specific field by
32
+ ```ruby
33
+ booble.volume.search(keyword: 'flower', field: 'intitle')
34
+ ```
35
+ [here is a list of other feilds](https://developers.google.com/books/docs/v1/using#PerformingSearch)
24
36
 
25
- TODO: Write usage instructions here
37
+ #### 3 - get book by id
38
+
39
+ ```ruby
40
+ boogle.volume.find(id: '_oG_iTxP1pIC')
41
+ ```
42
+
43
+ #### 4 - list of books
44
+ ```ruby
45
+ boogle.volume.list(user_id: '', bookshelf_id: '')
46
+ ```
47
+ #### 5 - return libarary books
48
+ ```ruby
49
+ boogle.volume.mylibrary(auth_token: '', bookshelf_id: '')
50
+ ```
51
+ #### 6 - remove a book
52
+ ```ruby
53
+ boogle.volume.remove(auth_token: '', book_id: '', bookshelf_id: '')
54
+ ```
55
+ #### 7 - Clearing all volumes from my bookshelf
56
+ ```ruby
57
+ boogle.volume.clear(auth_token: '', bookshelf_id: '')
58
+ ```
26
59
 
27
- ## Development
60
+ ## Dealing with bookshelf
28
61
 
29
- 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.
62
+ #### 1- list user's bookshelves
63
+ ```ruby
64
+ boogle.bookshelf.list(user_id: '')
65
+ ```
30
66
 
31
- 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
67
+ #### 2 - find specific bookshelf
68
+ ```ruby
69
+ boogle.bookshelf.find(user_id: '', bookshelf_id: '')
70
+ ```
32
71
 
72
+ #### 3- my libarary bookshelves
73
+ ```ruby
74
+ boogle.bookshelf.mylibarary(auth_token: '')
75
+ ```
33
76
  ## Contributing
34
77
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/boogle. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
78
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hassantc/boogle. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
79
 
37
80
 
38
81
  ## License
39
82
 
40
83
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
-
data/boogle.gemspec CHANGED
@@ -20,8 +20,11 @@ Gem::Specification.new do |spec|
20
20
  spec.bindir = "exe"
21
21
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
22
  spec.require_paths = ["lib"]
23
-
23
+ spec.add_dependency('faraday')
24
24
  spec.add_development_dependency "bundler", "~> 1.14"
25
25
  spec.add_development_dependency "rake", "~> 10.0"
26
26
  spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency 'mocha', '~> 1.0'
28
+ spec.add_development_dependency "fakeweb", ["~> 1.3"]
29
+ spec.add_development_dependency 'byebug'
27
30
  end
data/lib/boogle.rb CHANGED
@@ -1,5 +1,17 @@
1
- require "boogle/version"
1
+ require 'boogle/version'
2
+ require 'boogle/client' # user will deal with it
3
+
4
+ # google server will deal with those
5
+ require 'boogle/service/connection'
6
+ require 'boogle/service/request'
7
+
8
+ # how the object will render
9
+ require 'boogle/traits/volume'
10
+ require 'boogle/traits/bookshelf'
11
+
12
+ # object operations
13
+ require 'boogle/volume'
14
+ require 'boogle/bookshelf'
2
15
 
3
16
  module Boogle
4
- # Your code goes here...
5
17
  end
@@ -0,0 +1,21 @@
1
+ module Boogle
2
+ class Bookshelf
3
+ attr_accessor :key
4
+
5
+ def initialize(key)
6
+ @key = key
7
+ end
8
+
9
+ def list(user_id: '')
10
+ Boogle::Service::Request.search(@key, "users/#{user_id}/bookshelves", {})
11
+ end
12
+
13
+ def find(user_id: '', bookshelf_id: '')
14
+ Boogle::Service::Request.find(@key, "users/#{user_id}/bookshelves", bookshelf_id)
15
+ end
16
+
17
+ def mylibrary(auth_token: nil)
18
+ Boogle::Service::Request.mylibrary(auth_token, @key, 'mylibrary/bookshelves')
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,20 @@
1
+ module Boogle
2
+ class MisconfiguredClientError < StandardError; end
3
+
4
+ class Client
5
+ attr_accessor :key
6
+
7
+ def initialize(key: nil)
8
+ raise MisconfiguredClientError if key.nil? || key.empty? || !key.is_a?(String)
9
+ @key = key
10
+ end
11
+
12
+ def volume
13
+ Boogle::Volume.new(key)
14
+ end
15
+
16
+ def bookshelf
17
+ Boogle::Bookshelf.new(key)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ module Boogle
2
+ # base class exception from which all public Google books exceptions
3
+ class BoogleErrors < StandardError; end
4
+ # 400: Bad Request
5
+ class BadRequest < BoogleErrors; end
6
+ # 400: Invalid Sharing Request
7
+ # 401: Invalid Credentials
8
+ class InvalidCredentials < BoogleError; end
9
+ # 403: Daily Limit Exceeded
10
+ # 403: User Rate Limit Exceeded
11
+ # 403: Rate Limit Exceeded
12
+ # 403: Sharing Rate Limit Exceeded
13
+ class LimitExceedeed < BoogleError; end
14
+ # 404: resource not found: {fileId}
15
+ class ResourceNotFound < BoogleError; end
16
+ # 429: Too Many Requests
17
+ class TooManyRequests < BoogleError; end
18
+ # 500: Backend Error
19
+ class UnexpectedError < BoogleError; end
20
+ end
@@ -0,0 +1,19 @@
1
+ require 'faraday'
2
+ require 'json'
3
+
4
+ module Boogle
5
+ module Service
6
+ class Connection
7
+ BASE_URL = 'https://www.googleapis.com/books/v1/'
8
+
9
+ def self.api(key)
10
+ Faraday.new(url: BASE_URL) do |faraday|
11
+ faraday.response :logger
12
+ faraday.adapter Faraday.default_adapter
13
+ faraday.headers['Content-Type'] = 'application/json'
14
+ faraday.params['key'] = key
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,89 @@
1
+ require 'byebug'
2
+
3
+ module Boogle
4
+ module Service
5
+ class Request
6
+ class << self
7
+
8
+ def find(key, path, id)
9
+ response = api(key).get("#{path}/#{id}")
10
+ response.status == 200 ? handle_find_response(response.body) : errors(response)
11
+ end
12
+
13
+ def add(auth_token, key, path)
14
+ response = api(key).post do |req|
15
+ req.url path
16
+ req.headers['Authorization'] = auth_token
17
+ end
18
+ response.status == 200 ? true : errors(response)
19
+ end
20
+
21
+ def remove(auth_token, key, path)
22
+ response = api(key).post do |req|
23
+ req.url path
24
+ req.headers['Authorization'] = auth_token
25
+ end
26
+ response.status == 200 ? true : errors(response)
27
+ end
28
+
29
+ def clear(auth_token, key, path)
30
+ response = api(key).post do |req|
31
+ req.url path
32
+ req.headers['Authorization'] = auth_token
33
+ end
34
+ response.status == 200 ? true : errors(response)
35
+ end
36
+
37
+ def search(key, path, params)
38
+ response = api(key).get do |req|
39
+ req.url path, params
40
+ end
41
+ response.status == 200 ? handle_search_response(response.body) : errors(response)
42
+ end
43
+
44
+ def mylibrary(auth_token, key, path)
45
+ response = api(key).get do |req|
46
+ req.url path
47
+ req.headers['Authorization'] = auth_token
48
+ end
49
+ response.status == 200 ? handle_search_response(response.body) : errors(response)
50
+ end
51
+
52
+ def errors(response)
53
+ case response.status
54
+ when 400
55
+ raise Boogle::BadRequest, 'Bad rquest, please check your path again'
56
+ when 401
57
+ raise Boogle::InvalidCredentials, 'Check your Credentials'
58
+ when 403
59
+ raise Boogle::LimitExceedeed, 'you reached your limits'
60
+ when 404
61
+ raise Boogle::ResourceNotFound, 'Resource Not Found'
62
+ when 429
63
+ raise Boogle::TooManyRequests, 'Too many Requests'
64
+ else
65
+ raise Boogle::UnexpectedError, 'Something went wrong'
66
+ end
67
+ end
68
+
69
+ def handle_find_response(response)
70
+ response = JSON.parse(response)
71
+ response['kind'].include?('books#bookshelves') ? Boogle::Traits::Bookshelf.new(response) : Boogle::Traits::Volume.new(response)
72
+ end
73
+
74
+ def handle_search_response(response)
75
+ response = JSON.parse(response)
76
+ return [] unless response['items']
77
+ klass = response['kind'].include?('books#bookshelves') ? Boogle::Traits::Bookshelf : Boogle::Traits::Volume
78
+ response['items'].map do |item|
79
+ klass.new(item)
80
+ end
81
+ end
82
+
83
+ def api(key)
84
+ Boogle::Service::Connection.api(key)
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,26 @@
1
+ module Boogle
2
+ module Traits
3
+ class Bookshelf
4
+ attr_accessor :id, :kind, :self_link, :title, :description, :access
5
+ attr_accessor :created, :volume_count, :volumes_last_updated
6
+
7
+ def initialize(bookshelf_response)
8
+ convert_response_to_obj(bookshelf_response)
9
+ end
10
+
11
+ private
12
+
13
+ def convert_response_to_obj(bookshelf)
14
+ @id = bookshelf['id']
15
+ @kind = bookshelf['kind']
16
+ @self_link = bookshelf['selfLink']
17
+ @title = bookshelf['title']
18
+ @description = bookshelf['description']
19
+ @access = bookshelf['access']
20
+ @created = bookshelf['created']
21
+ @volume_count = bookshelf['volumeCount']
22
+ @volumes_last_updated = bookshelf['volumesLastUpdated']
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,57 @@
1
+ module Boogle
2
+ module Traits
3
+ class Volume
4
+ attr_accessor :page_count, :printed_page_count, :print_type, :average_rating
5
+ attr_accessor :kind, :self_link, :etag, :authors, :published_date, :language
6
+ attr_accessor :allow_a_non_logging, :content_version, :panelization_summary
7
+ attr_accessor :publisher, :description, :industry_identifiers, :categories
8
+ attr_accessor :ratings_count, :maturity_rating, :image_links, :layer_info
9
+ attr_accessor :offers, :access_info, :quote_sharing_allowed, :volume_info
10
+ attr_accessor :id, :title, :access_view_status, :preview_link, :infoLink
11
+ attr_accessor :sale_info, :retail_price, :web_reader_link, :buy_link
12
+ attr_accessor :canonical_volume_link
13
+
14
+ def initialize(volume_response)
15
+ convert_response_to_obj(volume_response)
16
+ end
17
+
18
+ private
19
+
20
+ def convert_response_to_obj(volume)
21
+ @kind = volume['kind']
22
+ @id = volume['id']
23
+ @volume_info = volume['volumeInfo']
24
+ @title = volume['volumeInfo']['title']
25
+ @authors = volume['volumeInfo']['authors']
26
+ @publisher = volume['publisher']
27
+ @published_date = volume['publishedDate']
28
+ @description = volume['description']
29
+ @industry_identifiers = volume['industryIdentifiers']
30
+ @page_count = volume['pageCount']
31
+ @printed_page_count = volume['printedPageCount']
32
+ @print_type = volume['printType']
33
+ @categories = volume['categories']
34
+ @average_rating = volume['averageRating']
35
+ @ratings_count = volume['ratingsCount']
36
+ @maturity_rating = volume['maturityRating']
37
+ @allow_a_non_logging = volume['allowAnonLogging']
38
+ @content_version = volume['contentVersion']
39
+ @panelization_summary = volume['panelizationSummary']
40
+ @image_links = volume['imageLinks']
41
+ @language = volume['language']
42
+ @preview_link = volume['preview_link']
43
+ @info_link = volume['infoLink']
44
+ @canonical_volume_link = volume['canonicalVolumeLink']
45
+ @layer_info = volume['layerInfo']
46
+ @sale_info = volume['saleInfo']
47
+ @retail_price = volume['retailPrice']
48
+ @buy_link = volume['buyLink']
49
+ @offers = volume['offers']
50
+ @access_info = volume['accessInfo']
51
+ @web_reader_link = volume['webReaderLink']
52
+ @access_view_status = volume['accessViewStatus']
53
+ @quote_sharing_allowed = volume['quoteSharingAllowed']
54
+ end
55
+ end
56
+ end
57
+ end
@@ -1,3 +1,3 @@
1
1
  module Boogle
2
- VERSION = "0.0.0"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -0,0 +1,46 @@
1
+ module Boogle
2
+ class Volume
3
+ attr_accessor :key
4
+ def initialize(key)
5
+ @key = key
6
+ end
7
+
8
+ def find(id: '')
9
+ Boogle::Service::Request.find(@key, 'volumes', id)
10
+ end
11
+
12
+ def search(keyword: '', field: nil, download: nil, filter: nil, page: 1, per_page: 5, order_by: nil, projection: nil, type: 'all')
13
+ search_params = { q: query(keyword, field), download: download,
14
+ filter: filter, startIndex: page, maxResults: per_page,
15
+ printType: type, projection: projection,
16
+ orderBy: order_by }.reject { |_, v| v.nil? }
17
+ Boogle::Service::Request.search(@key, 'volumes', search_params)
18
+ end
19
+
20
+ def list(user_id: '', bookshelf_id: '')
21
+ Boogle::Service::Request.search(@key, "users/#{user_id}/bookshelves/#{bookshelf_id}/volumes", {})
22
+ end
23
+
24
+ def mylibrary(auth_token: nil, bookshelf_id: '')
25
+ Booogle::Service::Request.mylibrary(auth_token, @key, "mylibrary/bookshelves/#{bookshelf_id}/volumes")
26
+ end
27
+
28
+ def add(auth_token: nil, book_id: '', bookshelf_id: '')
29
+ Booogle::Service::Request.add(auth_token, @key, "mylibrary/bookshelves/#{bookshelf_id}/addVolume?volumeId=#{book_id}")
30
+ end
31
+
32
+ def remove(auth_token: nil, book_id: '', bookshelf_id: '')
33
+ Booogle::Service::Request.add(auth_token, @key, "mylibrary/bookshelves/#{bookshelf_id}/removeVolume?volumeId=#{book_id}")
34
+ end
35
+
36
+ def clear(auth_token: nil, bookshelf_id: '')
37
+ Booogle::Service::Request.clear(auth_token, @key, "mylibrary/bookshelves/#{bookshelf_id}/clearVolumes")
38
+ end
39
+
40
+ private
41
+
42
+ def query(keyword, field)
43
+ field ? "#{keyword}+#{field}:keys" : keyword
44
+ end
45
+ end
46
+ end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: boogle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - HassanTC
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-11-25 00:00:00.000000000 Z
11
+ date: 2017-12-05 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
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'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +66,48 @@ dependencies:
52
66
  - - "~>"
53
67
  - !ruby/object:Gem::Version
54
68
  version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mocha
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: fakeweb
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.3'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.3'
97
+ - !ruby/object:Gem::Dependency
98
+ name: byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
55
111
  description: boogle is a gem that handle the google books apis hassle
56
112
  email:
57
113
  - hassan.mahmoud1@outlook.com
@@ -59,6 +115,7 @@ executables: []
59
115
  extensions: []
60
116
  extra_rdoc_files: []
61
117
  files:
118
+ - ".byebug_history"
62
119
  - ".gitignore"
63
120
  - ".rspec"
64
121
  - ".travis.yml"
@@ -71,7 +128,15 @@ files:
71
128
  - bin/setup
72
129
  - boogle.gemspec
73
130
  - lib/boogle.rb
131
+ - lib/boogle/bookshelf.rb
132
+ - lib/boogle/client.rb
133
+ - lib/boogle/errors.rb
134
+ - lib/boogle/service/connection.rb
135
+ - lib/boogle/service/request.rb
136
+ - lib/boogle/traits/bookshelf.rb
137
+ - lib/boogle/traits/volume.rb
74
138
  - lib/boogle/version.rb
139
+ - lib/boogle/volume.rb
75
140
  homepage: https://github.com/HassanTC/boogle
76
141
  licenses:
77
142
  - MIT