openlibrary 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -4,3 +4,4 @@ Gemfile.lock
4
4
  pkg/*
5
5
  .rvmrc
6
6
  *~
7
+ *.swp
@@ -1,9 +1,9 @@
1
1
  == Openlibrary
2
2
 
3
3
 
4
- OPENLIBRARY is a simple wrapper for the Open Library REST-API.
4
+ The openlibrary gem is a simple wrapper for the Open Library REST API.
5
5
 
6
- For more information on the REST calls, you can view the Open Library Books API documentation here[http://openlibrary.org/dev/docs/api/books], or visit the {Open Library Developer Center}[http://openlibrary.org/developers/api].
6
+ For more information on the REST calls, you can view the {Open Library API documentation}[http://openlibrary.org/dev/docs/restful_api]. For more info about Open Library development in general, visit the {Open Library Developer Center}[http://openlibrary.org/developers/api] and {Developer Notes}[http://openlibrary.org/dev].
7
7
 
8
8
 
9
9
  == Installation
@@ -1,16 +1,20 @@
1
1
  require 'openlibrary/version'
2
- require 'json'
3
- require 'rest-client'
4
- require 'uri'
2
+ require 'openlibrary/data'
3
+ require 'openlibrary/view'
4
+ require 'openlibrary/client'
5
+ require 'openlibrary/errors'
6
+ require 'openlibrary/request'
5
7
 
6
8
  module Openlibrary
9
+ # Create a new Openlibrary::Client instance
10
+ #
11
+ def self.new(options={})
12
+ Openlibrary::Client.new(options)
13
+ end
7
14
 
8
- autoload :Data, 'openlibrary/data'
9
- autoload :View, 'openlibrary/view'
10
-
11
- def self.version_string
15
+ # Return the openlibrary gem version
16
+ #
17
+ def self.version
12
18
  "Openlibrary version #{Openlibrary::VERSION}"
13
19
  end
14
-
15
-
16
20
  end
@@ -0,0 +1,27 @@
1
+ require 'openlibrary/request'
2
+ require 'openlibrary/client/books'
3
+
4
+ module Openlibrary
5
+ class Client
6
+ include Openlibrary::Request
7
+ include Openlibrary::Books
8
+
9
+ attr_reader :username, :password
10
+
11
+ # Initialize an Openlibrary::Client instance
12
+ #
13
+ # options[:username] - Username
14
+ # options[:password] - Password
15
+ #
16
+ def initialize(options={})
17
+ unless options.kind_of?(Hash)
18
+ raise ArgumentError, "Options hash required."
19
+ end
20
+
21
+ # For future versions that include the ability to log in
22
+ #
23
+ # @username = options[:username] || Openlibrary.configuration[:username]
24
+ # @password = options[:password] || Openlibrary.configuration[:password]
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,10 @@
1
+ module Openlibrary
2
+ module Books
3
+ # Find books in Open Library by ISBN, OLID, LCCN, or OCLC
4
+ #
5
+ def book(olid)
6
+ data = request("/books/#{olid}")
7
+ Hashie::Mash.new(data)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ module Openlibrary
2
+ class Error < StandardError; end
3
+ class Unauthorized < Error; end
4
+ class NotFound < Error; end
5
+ end
@@ -0,0 +1,43 @@
1
+ require 'rest-client'
2
+ require 'active_support/core_ext'
3
+ require 'json'
4
+ require 'uri'
5
+ require 'hashie'
6
+
7
+ module Openlibrary
8
+ module Request
9
+ API_URL = 'http://www.openlibrary.org'
10
+
11
+ protected
12
+
13
+ # Perform an API request
14
+ #
15
+ # path - Request path
16
+ #
17
+ def request(path)
18
+ # This code is for future versions that allow user login.
19
+ #
20
+ # username = username || Openlibrary.configuration[:username]
21
+ # password = password || Openlibrary.configuration[:password]
22
+
23
+ url = "#{API_URL}#{path}"
24
+
25
+ resp = RestClient.get(url, { accept: :json }) do |response, request, result, &block|
26
+ case response.code
27
+ when 200
28
+ response.return!(request, result, &block)
29
+ when 401
30
+ raise Openlibrary::Unauthorized
31
+ when 404
32
+ raise Openlibrary::NotFound
33
+ end
34
+ end
35
+ parse(resp)
36
+ end
37
+
38
+ def parse(resp)
39
+ object = JSON.parse(resp.body)
40
+ object
41
+ end
42
+ end
43
+ end
@@ -1,3 +1,3 @@
1
1
  module Openlibrary
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -18,8 +18,11 @@ Gem::Specification.new do |s|
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
20
 
21
- # specify any dependencies here; for example:
22
- s.add_development_dependency "rspec"
23
- s.add_runtime_dependency "json"
24
- s.add_runtime_dependency "rest-client"
21
+ s.add_development_dependency 'rspec', '~> 2.13'
22
+ s.add_development_dependency 'webmock', '~> 1.6'
23
+
24
+ s.add_runtime_dependency 'json', '~> 1.7.7'
25
+ s.add_runtime_dependency 'rest-client', '~> 1.6'
26
+ s.add_runtime_dependency 'hashie', '~> 1.0'
27
+ s.add_runtime_dependency 'activesupport', '~> 3'
25
28
  end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Client' do
4
+ let(:client) { Openlibrary::Client.new() }
5
+
6
+ describe '#new' do
7
+ it 'requires an argument' do
8
+ expect { Openlibrary::Client.new(nil) }.
9
+ to raise_error ArgumentError, "Options hash required."
10
+ end
11
+
12
+ it 'requires a hash argument' do
13
+ expect { Openlibrary::Client.new('foo') }.
14
+ to raise_error ArgumentError, "Options hash required."
15
+ end
16
+ end
17
+
18
+ describe '#book' do
19
+ before do
20
+ olid = 'OL23109860M'
21
+ stub_get("/books/#{olid}", 'book.json')
22
+ end
23
+ it 'returns book details' do
24
+ expect { client.book('OL23109860M') }.not_to raise_error
25
+
26
+ book = client.book('OL23109860M')
27
+
28
+ book.should be_a Hashie::Mash
29
+ book.contributors.should be_a Array
30
+ book.covers.should be_a Array
31
+
32
+ book.title.should eq 'The Great Gatsby'
33
+ book.by_statement.should eq 'F. Scott Fitzgerald.'
34
+ book.number_of_pages.should eq 180
35
+ book.contributors[0].name.should eq 'Francis Cugat'
36
+ book.contributors[0].role.should eq 'Cover Art'
37
+ book.copyright_date.should eq '1925'
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,59 @@
1
+ { "authors" : [ { "key" : "/authors/OL27349A" } ],
2
+ "by_statement" : "F. Scott Fitzgerald.",
3
+ "classifications" : { },
4
+ "contributors" : [ { "name" : "Francis Cugat",
5
+ "role" : "Cover Art"
6
+ } ],
7
+ "copyright_date" : "1925",
8
+ "covers" : [ 6776207,
9
+ 6725316,
10
+ 6390463
11
+ ],
12
+ "created" : { "type" : "/type/datetime",
13
+ "value" : "2009-02-18T18:42:07.162489"
14
+ },
15
+ "dewey_decimal_class" : [ "813" ],
16
+ "genres" : [ "Fiction." ],
17
+ "ia_box_id" : [ "IA119605" ],
18
+ "identifiers" : { "goodreads" : [ "4671" ],
19
+ "google" : [ "iXn5U2IzVH0C" ],
20
+ "librarything" : [ "2964" ]
21
+ },
22
+ "isbn_10" : [ "0743273567" ],
23
+ "isbn_13" : [ "9780743273565" ],
24
+ "key" : "/books/OL23109860M",
25
+ "languages" : [ { "key" : "/languages/eng" } ],
26
+ "last_modified" : { "type" : "/type/datetime",
27
+ "value" : "2011-08-12T01:22:25.394416"
28
+ },
29
+ "latest_revision" : 12,
30
+ "lc_classifications" : [ "PS3511.I9 G7 2004" ],
31
+ "lccn" : [ "25010468" ],
32
+ "notes" : { "type" : "/type/text",
33
+ "value" : "* [Publisher's Website for this edition][1]\r\n\r\n\r\n [1]: http://books.simonandschuster.com/Great-Gatsby/F-Scott-Fitzgerald/9780743273565"
34
+ },
35
+ "number_of_pages" : 180,
36
+ "ocaid" : "greatgat00fitz",
37
+ "oclc_number" : [ "57215622" ],
38
+ "oclc_numbers" : [ "57215622" ],
39
+ "pagination" : "180 p. ;",
40
+ "physical_format" : "Trade Paperback",
41
+ "publish_country" : "nyu",
42
+ "publish_date" : "2004",
43
+ "publish_places" : [ "New York, New York, USA" ],
44
+ "publishers" : [ "Scribner" ],
45
+ "revision" : 12,
46
+ "source_records" : [ "marc:SanFranPL14/SanFranPL14.out:71962031:12930",
47
+ "ia:greatgat00fitz"
48
+ ],
49
+ "subjects" : [ "Traffic accidents -- Fiction",
50
+ "First loves -- Fiction",
51
+ "Rich people -- Fiction",
52
+ "Mistresses -- Fiction",
53
+ "Revenge -- Fiction",
54
+ "Long Island (N.Y.) -- Fiction"
55
+ ],
56
+ "title" : "The Great Gatsby",
57
+ "type" : { "key" : "/type/edition" },
58
+ "works" : [ { "key" : "/works/OL468431W" } ]
59
+ }
@@ -1,12 +1,12 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Openlibrary do
3
+ describe 'Openlibrary' do
4
4
  it 'should return correct version string' do
5
- Openlibrary.version_string.should == "Openlibrary version #{Openlibrary::VERSION}"
5
+ Openlibrary.version.should == "Openlibrary version #{Openlibrary::VERSION}"
6
6
  end
7
7
  end
8
8
 
9
- describe Openlibrary::View do
9
+ describe 'Openlibrary::View' do
10
10
  before do
11
11
  @book_view = Openlibrary::View.new
12
12
  end
@@ -22,7 +22,7 @@ describe Openlibrary::View do
22
22
  it { should respond_to(:thumbnail_url) }
23
23
  end
24
24
 
25
- describe Openlibrary::Data do
25
+ describe 'Openlibrary::Data' do
26
26
  before do
27
27
  @book_data = Openlibrary::Data.new
28
28
  end
@@ -1,8 +1,35 @@
1
- require 'rspec'
1
+ $:.unshift File.expand_path("../..", __FILE__)
2
+
2
3
  require 'openlibrary'
4
+ require 'rspec'
3
5
  require 'rest-client'
6
+ require 'webmock'
7
+ require 'webmock/rspec'
4
8
 
5
9
  RSpec.configure do |config|
6
10
  config.color_enabled = true
7
11
  config.formatter = 'documentation'
8
12
  end
13
+
14
+ def stub_get(path, fixture_name)
15
+ stub_request(:get, api_url(path)).
16
+ with(:headers => {'Accept'=>'application/json'}).
17
+ to_return(
18
+ :status => 200,
19
+ :body => fixture(fixture_name),
20
+ :headers => {}
21
+ )
22
+ end
23
+
24
+ def fixture_path(file=nil)
25
+ path = File.expand_path("../fixtures", __FILE__)
26
+ file.nil? ? path : File.join(path, file)
27
+ end
28
+
29
+ def fixture(file)
30
+ File.read(fixture_path(file))
31
+ end
32
+
33
+ def api_url(path)
34
+ "#{Openlibrary::Request::API_URL}#{path}"
35
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: openlibrary
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.0.0
5
+ version: 1.0.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jay Fajardo
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2013-02-02 00:00:00 Z
13
+ date: 2013-03-02 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
@@ -18,33 +18,66 @@ dependencies:
18
18
  requirement: &id001 !ruby/object:Gem::Requirement
19
19
  none: false
20
20
  requirements:
21
- - - ">="
21
+ - - ~>
22
22
  - !ruby/object:Gem::Version
23
- version: "0"
23
+ version: "2.13"
24
24
  type: :development
25
25
  version_requirements: *id001
26
26
  - !ruby/object:Gem::Dependency
27
- name: json
27
+ name: webmock
28
28
  prerelease: false
29
29
  requirement: &id002 !ruby/object:Gem::Requirement
30
30
  none: false
31
31
  requirements:
32
- - - ">="
32
+ - - ~>
33
33
  - !ruby/object:Gem::Version
34
- version: "0"
35
- type: :runtime
34
+ version: "1.6"
35
+ type: :development
36
36
  version_requirements: *id002
37
37
  - !ruby/object:Gem::Dependency
38
- name: rest-client
38
+ name: json
39
39
  prerelease: false
40
40
  requirement: &id003 !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
- - - ">="
43
+ - - ~>
44
44
  - !ruby/object:Gem::Version
45
- version: "0"
45
+ version: 1.7.7
46
46
  type: :runtime
47
47
  version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: rest-client
50
+ prerelease: false
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ version: "1.6"
57
+ type: :runtime
58
+ version_requirements: *id004
59
+ - !ruby/object:Gem::Dependency
60
+ name: hashie
61
+ prerelease: false
62
+ requirement: &id005 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ~>
66
+ - !ruby/object:Gem::Version
67
+ version: "1.0"
68
+ type: :runtime
69
+ version_requirements: *id005
70
+ - !ruby/object:Gem::Dependency
71
+ name: activesupport
72
+ prerelease: false
73
+ requirement: &id006 !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ~>
77
+ - !ruby/object:Gem::Version
78
+ version: "3"
79
+ type: :runtime
80
+ version_requirements: *id006
48
81
  description: Openlibrary.org API Interface
49
82
  email:
50
83
  - jmrfajardo@gmail.com
@@ -60,11 +93,17 @@ files:
60
93
  - README.rdoc
61
94
  - Rakefile
62
95
  - lib/openlibrary.rb
96
+ - lib/openlibrary/client.rb
97
+ - lib/openlibrary/client/books.rb
63
98
  - lib/openlibrary/data.rb
64
99
  - lib/openlibrary/details.rb
100
+ - lib/openlibrary/errors.rb
101
+ - lib/openlibrary/request.rb
65
102
  - lib/openlibrary/version.rb
66
103
  - lib/openlibrary/view.rb
67
104
  - openlibrary.gemspec
105
+ - spec/client_spec.rb
106
+ - spec/fixtures/book.json
68
107
  - spec/openlibrary_spec.rb
69
108
  - spec/spec_helper.rb
70
109
  homepage: http://www.proudcloud.net
@@ -95,5 +134,7 @@ signing_key:
95
134
  specification_version: 3
96
135
  summary: Ruby Interface for the Openlibrary.org API
97
136
  test_files:
137
+ - spec/client_spec.rb
138
+ - spec/fixtures/book.json
98
139
  - spec/openlibrary_spec.rb
99
140
  - spec/spec_helper.rb