scholar 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml CHANGED
@@ -1,10 +1,9 @@
1
- branches:
2
- only:
3
- - master
4
1
  rvm:
5
2
  - 1.9.2
6
3
  - 1.9.3
7
4
  - 2.0.0
5
+ - jruby-19mode
6
+ - rbx-19mode
8
7
  script: bundle exec rspec spec
9
8
  notifications:
10
9
  email:
data/Gemfile CHANGED
@@ -3,18 +3,21 @@ source 'https://rubygems.org'
3
3
  gemspec
4
4
 
5
5
  group :development, :test do
6
- gem 'rake'
7
- gem 'pry'
8
- gem 'log_buddy'
6
+ gem 'rake', '~> 10.0'
7
+ gem 'pry', '~> 0.9.11'
8
+ gem 'log_buddy', '~> 0.7.0'
9
9
  end
10
10
 
11
11
  group :test do
12
- gem 'rspec'
12
+ gem 'rspec', '~> 2.12.0'
13
13
 
14
- gem 'factory_girl'
14
+ gem 'vcr', '~> 2.4.0'
15
+ gem 'webmock', '~> 1.8.0'
15
16
 
16
- gem 'spork'
17
- gem 'fuubar'
17
+ gem 'factory_girl', '~> 4.2.0'
18
18
 
19
- gem 'cane'
19
+ gem 'spork', '~> 0.9.2'
20
+ gem 'fuubar', '~> 1.1.0'
21
+
22
+ gem 'cane', :platforms => :mri
20
23
  end
data/Gemfile.lock CHANGED
@@ -1,8 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- scholar (0.1.0)
4
+ scholar (0.1.1)
5
5
  activesupport (~> 3.2.11)
6
+ faraday (~> 0.8.7)
7
+ faraday_middleware (~> 0.9.0)
6
8
 
7
9
  GEM
8
10
  remote: https://rubygems.org/
@@ -10,12 +12,19 @@ GEM
10
12
  activesupport (3.2.11)
11
13
  i18n (~> 0.6)
12
14
  multi_json (~> 1.0)
15
+ addressable (2.3.3)
13
16
  cane (2.5.0)
14
17
  parallel
15
18
  coderay (1.0.8)
19
+ crack (0.3.2)
16
20
  diff-lcs (1.1.3)
17
21
  factory_girl (4.2.0)
18
22
  activesupport (>= 3.0.0)
23
+ faraday (0.8.7)
24
+ multipart-post (~> 1.1)
25
+ faraday_middleware (0.9.0)
26
+ faraday (>= 0.7.4, < 0.9)
27
+ ffi (1.6.0-java)
19
28
  fuubar (1.1.0)
20
29
  rspec (~> 2.0)
21
30
  rspec-instafail (~> 0.2.0)
@@ -24,11 +33,17 @@ GEM
24
33
  log_buddy (0.7.0)
25
34
  method_source (0.8.1)
26
35
  multi_json (1.5.0)
36
+ multipart-post (1.2.0)
27
37
  parallel (0.6.1)
28
38
  pry (0.9.11.4)
29
39
  coderay (~> 1.0.5)
30
40
  method_source (~> 0.8)
31
41
  slop (~> 3.4)
42
+ pry (0.9.11.4-java)
43
+ coderay (~> 1.0.5)
44
+ method_source (~> 0.8)
45
+ slop (~> 3.4)
46
+ spoon (~> 0.0)
32
47
  rake (10.0.3)
33
48
  rspec (2.12.0)
34
49
  rspec-core (~> 2.12.0)
@@ -38,21 +53,30 @@ GEM
38
53
  rspec-expectations (2.12.1)
39
54
  diff-lcs (~> 1.1.3)
40
55
  rspec-instafail (0.2.4)
41
- rspec-mocks (2.12.1)
56
+ rspec-mocks (2.12.2)
42
57
  ruby-progressbar (1.0.2)
43
58
  slop (3.4.3)
59
+ spoon (0.0.3)
60
+ ffi
44
61
  spork (0.9.2)
62
+ vcr (2.4.0)
63
+ webmock (1.8.11)
64
+ addressable (>= 2.2.7)
65
+ crack (>= 0.1.7)
45
66
 
46
67
  PLATFORMS
68
+ java
47
69
  ruby
48
70
 
49
71
  DEPENDENCIES
50
72
  cane
51
- factory_girl
52
- fuubar
53
- log_buddy
54
- pry
55
- rake
56
- rspec
73
+ factory_girl (~> 4.2.0)
74
+ fuubar (~> 1.1.0)
75
+ log_buddy (~> 0.7.0)
76
+ pry (~> 0.9.11)
77
+ rake (~> 10.0)
78
+ rspec (~> 2.12.0)
57
79
  scholar!
58
- spork
80
+ spork (~> 0.9.2)
81
+ vcr (~> 2.4.0)
82
+ webmock (~> 1.8.0)
data/README.md CHANGED
@@ -1,6 +1,32 @@
1
1
  # Scholar [![Build Status](https://secure.travis-ci.org/noted/scholar.png)](https://travis-ci.org/noted/scholar)
2
2
 
3
- Scholar is a Ruby library for building [MLA citations](http://www.mla.org/) for scholarly works.
3
+ Scholar is a Ruby library for building [MLA citations](http://www.mla.org/) for scholarly works. Just pass Scholar your data and it'll do the rest.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your `Gemfile`:
8
+
9
+ ```ruby
10
+ gem 'scholar'
11
+ ```
12
+
13
+ And then run:
14
+
15
+ ```
16
+ $ bundle install
17
+ ```
18
+
19
+ Or install system-wide with:
20
+
21
+ ```
22
+ $ gem install scholar
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ### Citation
28
+
29
+ To create a citation, create a new `Citation` object.
4
30
 
5
31
  ```ruby
6
32
  salinger = Scholar::Citation.new({
@@ -22,18 +48,20 @@ salinger = Scholar::Citation.new({
22
48
  salinger.html # => Salinger, J. D. <em>Catcher in the Rye</em>. Boston: Little, Brown, 1995.
23
49
  ```
24
50
 
25
- ## Usage
51
+ `Scholar::Citation.new` only takes a `Hash` with the attributes associated with the source.
26
52
 
27
- To create a citation, you pass `Scholar::Citation` a hash with the attributes associated with the source. See the specific type on [the wiki](https://github.com/noted/scholar/wiki/Supported-Types) for the attributes available.
53
+ ### Required Keys
28
54
 
29
- ### Global Attributes
55
+ #### Global Attributes
30
56
 
31
- All source types can take these fields.
57
+ All source types can (and must) take these fields.
32
58
 
33
- * `:type` &mdash; The type of publication you're citing ([see the wiki for a list of supported types](https://github.com/noted/scholar/wiki/Supported-Types)).
34
- * `:contributors` &mdash; An array of contributors (see below for details).
59
+ Key | Description
60
+ ----------------|-----------------------------------------
61
+ `:type` | The type of publication you're citing ([see the wiki for a list of supported types](types)). Must be a `Symbol` object.
62
+ `:contributors` | An array of contributors (see below for details). If there are no contributors, pass an empty `Array`.
35
63
 
36
- #### Contributors
64
+ ##### Contributors
37
65
 
38
66
  The `:contributors` key is an array of hashes that define contributors. Here's an example.
39
67
 
@@ -69,13 +97,26 @@ The `:contributors` key is an array of hashes that define contributors. Here's a
69
97
  ]
70
98
  ```
71
99
 
72
- Each hash can take the following fields:
100
+ Each hash can take the following key-values:
73
101
 
74
- * `:role` &mdash; Can be `:author`, `:editor`, `:translator`, `:compiler`.
75
- * `:first` &mdash; The person's name.
76
- * `:middle` &mdash; The person's middle name (will convert to middle initial).
77
- * `:last` &mdash; The person's surname.
78
- * `:suffix` &mdash; Any suffixes the person has ("PhD", "Esq", "Jr", etc). Omit periods.
102
+ Key | Description
103
+ ----------|---------------------------------------
104
+ `:role` | Must be `:author`, `:editor`, `:translator`, `:compiler`.
105
+ `:first` | The contributor's first name (name).
106
+ `:middle` | The contributor's middle name (will be shortened to a middle initial).
107
+ `:last` | The contributor's last name (surname).
108
+ `:suffix` | Any suffixes or titles the contributor has ("PhD", "Esq", "Jr", "KBD", et cetera). Omit any periods.
109
+
110
+ ## Supported Ruby Versions
111
+
112
+ Scholar is tested on MRI version 1.9.2, 1.9.3, and 2.0.0.
113
+
114
+ ### Search
115
+
116
+ ```ruby
117
+ search = Scholar::Search.new("harry potter")
118
+ search.results # => [#<Scholar::Citation>, #<Scholar::Citation>, ...]
119
+ ```
79
120
 
80
121
  ## Contributing
81
122
 
@@ -89,6 +130,7 @@ Each hash can take the following fields:
89
130
  8. Add, commit, and push your changes.
90
131
  9. [Submit a pull request.][pr]
91
132
 
133
+ [types]: https://github.com/noted/scholar/wiki/Supported-Types
92
134
  [fork]: http://help.github.com/fork-a-repo/
93
135
  [branch]: http://learn.github.com/p/branching.html
94
136
  [pr]: http://help.github.com/send-pull-requests/
@@ -4,6 +4,9 @@ module Scholar
4
4
  # citation and the HTML citation itself.
5
5
  class Citation
6
6
 
7
+ # The raw hash given Citation.
8
+ attr_reader :raw
9
+
7
10
  # The pieces of data in the Citation.
8
11
  attr_reader :attributes
9
12
 
@@ -41,9 +44,13 @@ module Scholar
41
44
  source = "Scholar::Sources::#{options[:type].to_s.camelize}".constantize
42
45
 
43
46
  @attributes = options
47
+ @raw = @attributes.clone
44
48
  @attributes.delete(:type)
45
49
 
46
- @attributes = Scholar::Utilities.contributors!(@attributes)
50
+ if @attributes[:contributors]
51
+ @attributes = Scholar::Utilities.contributors!(@attributes)
52
+ end
53
+
47
54
  @attributes = Scholar::Utilities.order!(@attributes, source.sequence)
48
55
 
49
56
  attributes = @attributes.clone
@@ -51,5 +58,21 @@ module Scholar
51
58
 
52
59
  @html = Scholar::Utilities.concatenate!(attributes)
53
60
  end
61
+
62
+ # Returns the Citation object in Hash form.
63
+ def to_hash
64
+ hash = {}
65
+
66
+ instance_variables.each do |v|
67
+ hash[v.to_s[1..-1].to_sym] = instance_variable_get(v)
68
+ end
69
+
70
+ hash
71
+ end
72
+
73
+ # Returns the Citation object in JSON form.
74
+ def to_json
75
+ hash = self.to_hash.to_json
76
+ end
54
77
  end
55
78
  end
@@ -0,0 +1,75 @@
1
+ module Scholar
2
+
3
+ # A Search object containing search results
4
+ # for a query (currently only searches the
5
+ # Google Books API).
6
+ class Search
7
+
8
+ # The query performed.
9
+ attr_reader :query
10
+
11
+ # The results of the query.
12
+ attr_reader :results
13
+
14
+ # Searches for sources in Google Books.
15
+ #
16
+ # ==== Attributes
17
+ #
18
+ # * +query+ - The search term.
19
+ def initialize(query)
20
+ @@connection = Faraday.new(:url => "https://www.googleapis.com") do |f|
21
+ f.request :json
22
+ f.response :json, :content_type => /\bjson$/
23
+ f.adapter Faraday.default_adapter
24
+ end
25
+
26
+ # Remember the query.
27
+ @query = query
28
+
29
+ # Get the raw data.
30
+ @results = perform!(query)
31
+
32
+ # Turn that data into something a Citation class can understand.
33
+ @results = format!(@results)
34
+
35
+ self
36
+ end
37
+
38
+ private
39
+
40
+ def perform!(query)
41
+ request = @@connection.get do |r|
42
+ r.url "/books/v1/volumes", :q => query
43
+ end
44
+
45
+ request.body
46
+ end
47
+
48
+ def format!(hash)
49
+ arr = []
50
+
51
+ hash["items"].each do |i|
52
+ vol = i["volumeInfo"]
53
+
54
+ authors = vol["authors"]
55
+
56
+ contributors = []
57
+ authors.each do |a|
58
+ contributors << Scholar::Utilities::Contributor.new(a).attributes
59
+ end
60
+
61
+ citation = Scholar::Citation.new({
62
+ :type => :book,
63
+ :contributors => contributors,
64
+ :title => vol["title"],
65
+ :publisher => vol["publisher"],
66
+ :year => vol["publishedDate"][0,4]
67
+ })
68
+
69
+ arr << citation
70
+ end
71
+
72
+ arr
73
+ end
74
+ end
75
+ end
@@ -9,7 +9,8 @@ module Scholar
9
9
  #
10
10
  # ==== Attributes
11
11
  #
12
- # * +arr+ - Array of Symbols (keys). This is the order the hash's key will be in.
12
+ # * +arr+ - Array of Symbols (keys). This is the order the hash's key
13
+ # will be in.
13
14
  def sequence(arr = nil)
14
15
  unless arr.nil?
15
16
  @@sequence = arr
@@ -22,7 +23,8 @@ module Scholar
22
23
  #
23
24
  # ==== Attributes
24
25
  #
25
- # * +block+ - A block of +rule+ definitions. The rule definition should call a method in Scholar::Utilities::Formatters.
26
+ # * +block+ - A block of +rule+ definitions. The rule definition should
27
+ # call a method in Scholar::Utilities::Formatters.
26
28
  #
27
29
  # ==== Example
28
30
  #
@@ -44,7 +46,8 @@ module Scholar
44
46
  # ==== Attributes
45
47
  #
46
48
  # * +key+ - The key of the hash to take the action on. Symbol.
47
- # * +action+ - The action to take on the key. Should be a method within Scholar::Utilities::Formatters.
49
+ # * +action+ - The action to take on the key. Should be a method within
50
+ # Scholar::Utilities::Formatters.
48
51
  def rule(key, &action)
49
52
  @@rules << [key, action]
50
53
  end
@@ -1,4 +1,6 @@
1
1
  module Scholar
2
+
3
+ # Generic utilities.
2
4
  class Utilities
3
5
 
4
6
  # A person associated with a work.
@@ -14,23 +16,33 @@ module Scholar
14
16
  #
15
17
  # ==== Attributes
16
18
  #
17
- # * +hash+ - The data associated with the Contributor.
19
+ # * +data+ - The data associated with the Contributor.
18
20
  # * +order+ - Last-name first (+:last+) or first-name first (+:first+).
19
21
  #
20
22
  # ==== Options
21
23
  #
22
24
  # * +:first+ - The name of the Contributor.
23
- # * +:middle+ - The middle name of the Contributor. Will be shortened to an initial.
25
+ # * +:middle+ - The middle name of the Contributor.
26
+ # Will be shortened to an initial.
24
27
  # * +:last+ - The surname of the Contributor.
25
28
  # * +:suffix+ - The suffix of the Contributor (PhD, Esq, etc).
26
29
  #
27
30
  # ==== Examples
28
31
  #
29
- # Scholar::Utilities::Contributor.new({:first => "Douglas", :last => "Adams"})
30
- def initialize(hash, order = :first)
32
+ # Scholar::Utilities::Contributor.new({
33
+ # :first => "Douglas",
34
+ # :last => "Adams"
35
+ # })
36
+ def initialize(data, order = :first)
37
+ if data.is_a?(String)
38
+ data = hash!(data)
39
+
40
+ data[:role] = :author
41
+ end
42
+
31
43
  @order = order
32
44
 
33
- @attributes = order!(hash)
45
+ @attributes = order!(data)
34
46
  @name = name!(@attributes)
35
47
  end
36
48
 
@@ -54,6 +66,23 @@ module Scholar
54
66
 
55
67
  private
56
68
 
69
+ # Make a Hash out of a String.
70
+ def hash!(str)
71
+ data = str.split(" ")
72
+
73
+ result = Hash.new
74
+
75
+ result[:first] = data.first
76
+
77
+ result[:last] = data.last
78
+
79
+ if data[2]
80
+ result[:middle] = data[1]
81
+ end
82
+
83
+ result
84
+ end
85
+
57
86
  # Order according to the sequence.
58
87
  def order!(hash, method = @order)
59
88
  if method == :last
@@ -1,4 +1,6 @@
1
1
  module Scholar
2
+
3
+ # Generic utilities.
2
4
  class Utilities
3
5
 
4
6
  # A list of Contributors.
@@ -1,4 +1,6 @@
1
1
  module Scholar
2
+
3
+ # Generic utilities.
2
4
  class Utilities
3
5
 
4
6
  # Utilities for dealing with hashes and actually creating the
@@ -1,4 +1,6 @@
1
1
  module Scholar
2
+
3
+ # Generic utilities.
2
4
  class Utilities
3
5
 
4
6
  # String formatters.
data/lib/scholar.rb CHANGED
@@ -1,9 +1,14 @@
1
1
  require 'json'
2
+
2
3
  require 'active_support/all'
3
4
 
5
+ require 'faraday'
6
+ require 'faraday_middleware'
7
+
4
8
  require_relative 'scholar/utilities'
5
9
  require_relative 'scholar/citation'
6
10
  require_relative 'scholar/source'
11
+ require_relative 'scholar/search'
7
12
 
8
13
  # Allows for the creation of scholarly MLA citations.
9
14
  module Scholar
data/scholar.gemspec CHANGED
@@ -5,7 +5,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
 
6
6
  Gem::Specification.new do |gem|
7
7
  gem.name = "scholar"
8
- gem.version = "0.1.0"
8
+ gem.version = "0.1.1"
9
9
  gem.authors = ["Ethan Turkeltaub"]
10
10
  gem.email = ["ethan.turkeltaub@gmail.com"]
11
11
  gem.description = %q{Generate valid MLA citations.}
@@ -19,4 +19,6 @@ Gem::Specification.new do |gem|
19
19
  gem.require_paths = ["lib"]
20
20
 
21
21
  gem.add_dependency 'activesupport', '~> 3.2.11'
22
+ gem.add_dependency 'faraday', '~> 0.8.7'
23
+ gem.add_dependency 'faraday_middleware', '~> 0.9.0'
22
24
  end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe Scholar::Search do
4
+ before do
5
+ VCR.insert_cassette 'search', :record => :all
6
+ end
7
+
8
+ describe ".new" do
9
+ let(:search) do
10
+ Scholar::Search.new("foobar")
11
+ end
12
+
13
+ it { search.query.should eql "foobar" }
14
+ it { search.results.should be_an_instance_of Array }
15
+ it { search.results[0].should be_an_instance_of Scholar::Citation }
16
+ end
17
+
18
+ after do
19
+ VCR.eject_cassette
20
+ end
21
+ end
@@ -81,7 +81,7 @@ describe Scholar::Utilities::Data do
81
81
 
82
82
  describe "#order!" do
83
83
  let(:hash) do
84
- { :foobar => "Foo to the bar to the foo.", :bar => "Hello there.", :abc => "123" }
84
+ { :foobar => "Foobar.", :bar => "Hello there.", :abc => "123" }
85
85
  end
86
86
 
87
87
  let(:sequence) do
data/spec/spec_helper.rb CHANGED
@@ -2,6 +2,9 @@ require 'spork'
2
2
 
3
3
  Spork.prefork do
4
4
  require 'rspec'
5
+ require 'vcr'
6
+ require 'webmock/rspec'
7
+
5
8
  require 'factory_girl'
6
9
 
7
10
  require 'log_buddy'
@@ -13,4 +16,12 @@ Spork.prefork do
13
16
  RSpec.configure do |c|
14
17
  c.include FactoryGirl::Syntax::Methods
15
18
  end
19
+
20
+ VCR.configure do |c|
21
+ c.configure_rspec_metadata!
22
+
23
+ c.cassette_library_dir = 'spec/cassettes'
24
+ c.default_cassette_options = { :record => :new_episodes }
25
+ c.hook_into :webmock
26
+ end
16
27
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scholar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-11 00:00:00.000000000 Z
12
+ date: 2013-03-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -27,6 +27,38 @@ dependencies:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
29
  version: 3.2.11
30
+ - !ruby/object:Gem::Dependency
31
+ name: faraday
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 0.8.7
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 0.8.7
46
+ - !ruby/object:Gem::Dependency
47
+ name: faraday_middleware
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 0.9.0
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.9.0
30
62
  description: Generate valid MLA citations.
31
63
  email:
32
64
  - ethan.turkeltaub@gmail.com
@@ -45,6 +77,7 @@ files:
45
77
  - Rakefile
46
78
  - lib/scholar.rb
47
79
  - lib/scholar/citation.rb
80
+ - lib/scholar/search.rb
48
81
  - lib/scholar/source.rb
49
82
  - lib/scholar/sources/book.rb
50
83
  - lib/scholar/utilities.rb
@@ -55,6 +88,7 @@ files:
55
88
  - scholar.gemspec
56
89
  - spec/factories/contributor.rb
57
90
  - spec/factories/contributor_list.rb
91
+ - spec/scholar/search_spec.rb
58
92
  - spec/scholar/source_spec.rb
59
93
  - spec/scholar/sources/book_spec.rb
60
94
  - spec/scholar/utilities/contributor_list_spec.rb
@@ -77,7 +111,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
77
111
  version: '0'
78
112
  segments:
79
113
  - 0
80
- hash: -551428721
114
+ hash: 1067264813
81
115
  required_rubygems_version: !ruby/object:Gem::Requirement
82
116
  none: false
83
117
  requirements:
@@ -86,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
86
120
  version: '0'
87
121
  segments:
88
122
  - 0
89
- hash: -551428721
123
+ hash: 1067264813
90
124
  requirements: []
91
125
  rubyforge_project:
92
126
  rubygems_version: 1.8.25
@@ -96,6 +130,7 @@ summary: Generate valid MLA citations.
96
130
  test_files:
97
131
  - spec/factories/contributor.rb
98
132
  - spec/factories/contributor_list.rb
133
+ - spec/scholar/search_spec.rb
99
134
  - spec/scholar/source_spec.rb
100
135
  - spec/scholar/sources/book_spec.rb
101
136
  - spec/scholar/utilities/contributor_list_spec.rb