nobel 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in nobel.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Peter Hellberg
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.
@@ -0,0 +1,97 @@
1
+ # Nobel
2
+
3
+ A small API client for the Nobel Prize API.
4
+
5
+ You can find out more on the official [Developer Zone](http://www.nobelprize.org/nobel_organizations/nobelmedia/nobelprize_org/developer/)
6
+
7
+ There is also an API Console on [API HQ](http://console.apihq.com/nobel-prize-api)
8
+
9
+ ## Dependencies
10
+
11
+ Nope.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ gem 'nobel'
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install nobel
26
+
27
+ ## Usage
28
+
29
+ It is probably a good idea to take a look in `specs`
30
+
31
+ ### Prize
32
+
33
+ ```ruby
34
+ # All Nobel Prizes in 2010
35
+ Nobel::Prize.all year: 2010
36
+
37
+ # All Peace Prizes
38
+ Nobel::Prize.all category: 'peace'
39
+ ```
40
+
41
+ ### Laureate
42
+
43
+ The laureate data contains their prizes (and affiliations)
44
+
45
+ ```ruby
46
+ # Find a Nobel Laureate by ID
47
+ nelly = Nobel::Laureate.find 640
48
+
49
+ # Show the categories Nelly won
50
+ nelly.categories #=> ["literature"]
51
+
52
+ # Show the years Nelly won
53
+ nelly.years #=> [1966]
54
+
55
+ # Find all living female Nobel Laureates
56
+ # (`query` is an alias to `all`)
57
+ Nobel::Laureate.query diedDate: '0000', gender: 'female'
58
+ ```
59
+
60
+ ### Country
61
+
62
+ The country data only contains the country code and name.
63
+
64
+ ```ruby
65
+ # To return a list of all known countries
66
+ Nobel::Country.all
67
+
68
+ # To find a specific country
69
+ Nobel::Country.find 'FR' #=> #<Nobel::Country:0x007fda82b78220 @code="FR", @name="France">
70
+
71
+ # You can also find by name
72
+ Nobel::Country.find 'Germany', :name #=> #<Nobel::Country:0x007fda82b90b40 @code="DE", @name="Germany">
73
+ ```
74
+
75
+ ### Configuration
76
+
77
+ Nothing needs to be configured out of the box, but you can change the
78
+ `api_host`, `api_version`, `json_parser`, `http_client` and `debug`
79
+ settings like this:
80
+
81
+ ```ruby
82
+ Nobel.configure do |c|
83
+ c.api_host = 'nobel.api'
84
+ c.api_version = 'v42'
85
+ c.json_parser = ->(json) { Oj.load(json) }
86
+ c.http_client = MyOwnHttpClient
87
+ c.debug = true
88
+ end
89
+ ```
90
+
91
+ ## Contributing
92
+
93
+ 1. Fork it
94
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
95
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
96
+ 4. Push to the branch (`git push origin my-new-feature`)
97
+ 5. Create new Pull Request
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+
3
+ require "rake/testtask"
4
+ require "bundler/gem_tasks"
5
+
6
+ task :default => :spec
7
+
8
+ Rake::TestTask.new(:spec) do |t|
9
+ t.test_files = FileList['spec/**/*_spec.rb']
10
+ end
11
+
12
+ desc "Console"
13
+ task :console do
14
+ exec 'pry -r./lib/nobel'
15
+ end
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+
3
+ require "nobel/api"
4
+ require "nobel/config"
5
+ require "nobel/country"
6
+ require "nobel/laureate"
7
+ require "nobel/affiliation"
8
+ require "nobel/prize"
9
+ require "nobel/version"
10
+
11
+ module Nobel
12
+ class << self
13
+ def configure(&block)
14
+ yield(config)
15
+ end
16
+
17
+ def config
18
+ @config ||= Config.new
19
+ end
20
+
21
+ def api
22
+ @api ||= Api.new(config)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+
3
+ module Nobel
4
+ class Affiliation
5
+ attr_reader :name, :country, :city
6
+
7
+ def initialize(data = nil)
8
+ data ||= {}
9
+
10
+ data.tap do |d|
11
+ @name = d['name']
12
+ @country = d['country']
13
+ @city = d['city']
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,59 @@
1
+ # encoding: utf-8
2
+
3
+ require 'uri'
4
+ require 'net/http'
5
+ require 'json'
6
+
7
+ module Nobel
8
+ class Api
9
+ attr_accessor :config
10
+
11
+ def initialize(config)
12
+ @config = config
13
+ end
14
+
15
+ def prize(params = {})
16
+ get_json :prize, params
17
+ end
18
+
19
+ def laureate(params = {})
20
+ get_json :laureate, params
21
+ end
22
+
23
+ def country
24
+ get_json :country
25
+ end
26
+
27
+ def get_json(endpoint, params = {})
28
+ load_json get("#{endpoint}.json", params)
29
+ end
30
+
31
+ def get(path, params = {})
32
+ uri = prepared_uri(path, params)
33
+
34
+ puts "GET: #{uri}" if @config.debug?
35
+
36
+ http_get(uri)
37
+ end
38
+
39
+ private
40
+
41
+ def http_get(uri)
42
+ @config.http_client.get(uri)
43
+ end
44
+
45
+ def load_json(doc)
46
+ @config.json_parser.call(doc)
47
+ end
48
+
49
+ def prepared_uri(endpoint, params = {})
50
+ endpoint_uri(endpoint).tap do |uri|
51
+ uri.query = URI.encode_www_form(params)
52
+ end
53
+ end
54
+
55
+ def endpoint_uri(endpoint)
56
+ URI.parse("#{@config.base_url}/#{endpoint}")
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+
3
+ module Nobel
4
+ class Config
5
+ attr_accessor :api_host, :api_version,
6
+ :json_parser, :http_client, :debug
7
+
8
+ def initialize
9
+ @api_host = 'api.nobelprize.org'
10
+ @api_version = 'v1'
11
+ @json_parser = ->(d) { JSON.parse(d) }
12
+ @http_client = Net::HTTP
13
+ @debug = false
14
+
15
+ yield self if block_given?
16
+ end
17
+
18
+ def base_url
19
+ "http://#{api_host}/#{api_version}"
20
+ end
21
+
22
+ def debug?
23
+ debug
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,40 @@
1
+ # encoding: utf-8
2
+
3
+ module Nobel
4
+ class Country
5
+ class << self
6
+ def all
7
+ data.map { |c| new(c) }
8
+ end
9
+
10
+ def find(value, key = 'code')
11
+ value = key == 'code' ? value.to_s.upcase : value.to_s
12
+ new data.detect { |c| c[key.to_s] == value }
13
+ end
14
+
15
+ def data
16
+ @data ||= get_data
17
+ end
18
+
19
+ def reload!
20
+ @data = get_data
21
+ self
22
+ end
23
+
24
+ private
25
+
26
+ def get_data
27
+ Nobel.api.country['countries'] || []
28
+ end
29
+ end
30
+
31
+ attr_reader :name, :code
32
+
33
+ def initialize(data)
34
+ data ||= { 'code' => 'ZZ', 'name' => 'Unknown' }
35
+
36
+ @name = data['name']
37
+ @code = data['code']
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,83 @@
1
+ # encoding: utf-8
2
+
3
+ require 'date'
4
+
5
+ module Nobel
6
+ class Laureate
7
+ class << self
8
+ def find(id)
9
+ data = get_data(id: id)
10
+ new(data.first) if data.any?
11
+ end
12
+
13
+ def all(params = {})
14
+ get_data(params).map { |c| new(c) }
15
+ end
16
+
17
+ alias :query :all
18
+
19
+ def get_data(params = {})
20
+ Nobel.api.laureate(params)['laureates'] || []
21
+ end
22
+ end
23
+
24
+ attr_reader :data, :id, :firstname, :surname, :share, :motivation,
25
+ :born, :born_city, :born_country, :born_country_code,
26
+ :died, :died_city, :died_country, :died_country_code
27
+
28
+ def initialize(data)
29
+ @data = data || {}
30
+
31
+ year_regex = /^\d{4}-[0-1][1-9]-\d{2}$/
32
+
33
+ @data.tap do |d|
34
+ @id = Integer(d['id'])
35
+
36
+ @firstname = d['firstname']
37
+ @surname = d['surname']
38
+
39
+ @share = Integer(d['share']) if d['share']
40
+ @motivation = d['motivation'] if d['motivation']
41
+
42
+ @born = Date.parse d['born'] if d['born'] =~ year_regex
43
+ @born_city = d['bornCity'] if d['bornCity']
44
+ @born_country = d['bornCountry'] if d['bornCountry']
45
+ @born_country_code = d['bornCountryCode'] if d['bornCountryCode']
46
+
47
+ @died = Date.parse d['died'] if d['died'] =~ year_regex
48
+ @died_city = d['diedCity'] if d['diedCity']
49
+ @died_country = d['diedCountry'] if d['diedCountry']
50
+ @died_country_code = d['diedCountryCode'] if d['diedCountryCode']
51
+ end
52
+ end
53
+
54
+ def reload!
55
+ initialize self.class.get_data(id: id).first
56
+ self
57
+ end
58
+
59
+ def name
60
+ "#{firstname} #{surname}".strip
61
+ end
62
+
63
+ def prizes
64
+ @prizes ||= (data['prizes'] || []).map do |p|
65
+ Prize.new(p)
66
+ end
67
+ end
68
+
69
+ def categories
70
+ won(:category)
71
+ end
72
+
73
+ def years
74
+ won(:year)
75
+ end
76
+
77
+ private
78
+
79
+ def won(field)
80
+ prizes.map(&field).uniq
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,42 @@
1
+ # encoding: utf-8
2
+
3
+ module Nobel
4
+ class Prize
5
+ class << self
6
+ def all(params = {})
7
+ get_data(params).map { |c| new(c) }
8
+ end
9
+
10
+ alias :query :all
11
+
12
+ def get_data(params = {})
13
+ Nobel.api.prize(params)['prizes'] || []
14
+ end
15
+ end
16
+
17
+ attr_reader :data, :year, :category, :share, :motivation
18
+
19
+ def initialize(data)
20
+ @data = data || {}
21
+
22
+ @data.tap do |d|
23
+ @year = Integer(d['year'])
24
+ @category = d['category']
25
+ @share = Integer(d['share']) if d['share']
26
+ @motivation = d['motivation']
27
+ end
28
+ end
29
+
30
+ def affiliations
31
+ @affiliations ||= (data['affiliations'] || []).map do |a|
32
+ Affiliation.new(a) if a.respond_to?(:has_key?)
33
+ end.compact
34
+ end
35
+
36
+ def laureates
37
+ @laureates ||= (data['laureates'] || []).map do |l|
38
+ Laureate.new(l)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,5 @@
1
+ # encoding: utf-8
2
+
3
+ module Nobel
4
+ VERSION = "0.0.1"
5
+ end
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'nobel/version'
6
+
7
+ Gem::Specification.new do |gem|
8
+ gem.required_ruby_version = '>= 1.9.3'
9
+
10
+ gem.name = "nobel"
11
+ gem.version = Nobel::VERSION
12
+ gem.authors = ["Peter Hellberg"]
13
+ gem.email = ["peter@c7.se"]
14
+ gem.summary = %q{API client for the Nobel Prize API}
15
+ gem.homepage = "https://github.com/peterhellberg/nobel"
16
+ gem.license = 'MIT'
17
+
18
+ gem.files = `git ls-files`.split($/)
19
+ gem.test_files = gem.files.grep(%r{^spec/})
20
+ gem.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,10 @@
1
+ {
2
+ "countries":[
3
+ {"name":"Germany (now Russia)","code":"RU"},
4
+ {"name":"Tuscany (now Italy)","code":"IT"},
5
+ {"name":"Poland (now Lithuania)","code":"LT"},
6
+ {"name":"Madagascar","code":"MG"},
7
+ {"name":"Free City of Danzig (now Poland)","code":"PL"},
8
+ {"name":"Austrian Empire (now Czech Republic)","code":"CZ"}
9
+ ]
10
+ }
@@ -0,0 +1,86 @@
1
+ {
2
+ "laureates": [
3
+ {
4
+ "id": "79",
5
+ "firstname": "Maria",
6
+ "surname": "Goeppert Mayer",
7
+ "born": "1906-06-28",
8
+ "died": "1972-02-20",
9
+ "bornCountry": "Germany (now Poland)",
10
+ "bornCountryCode": "PL",
11
+ "bornCity": "Kattowitz (now Katowice)",
12
+ "diedCountry": "USA",
13
+ "diedCountryCode": "US",
14
+ "diedCity": "San Diego, CA",
15
+ "gender": "female",
16
+ "prizes": [
17
+ {
18
+ "year": "1963",
19
+ "category": "physics",
20
+ "share": "4",
21
+ "motivation": "\"for their discoveries concerning nuclear shell structure\"",
22
+ "affiliations": [
23
+ {
24
+ "name": "University of California",
25
+ "city": "La Jolla, CA",
26
+ "country": "USA"
27
+ }
28
+ ]
29
+ }
30
+ ]
31
+ },
32
+ {
33
+ "id": "453",
34
+ "firstname": "Christiane",
35
+ "surname": "Nüsslein-Volhard",
36
+ "born": "1942-10-20",
37
+ "died": "0000-00-00",
38
+ "bornCountry": "Germany",
39
+ "bornCountryCode": "DE",
40
+ "bornCity": "Magdeburg",
41
+ "gender": "female",
42
+ "prizes": [
43
+ {
44
+ "year": "1995",
45
+ "category": "medicine",
46
+ "share": "3",
47
+ "motivation": "\"for their discoveries concerning the genetic control of early embryonic development\"",
48
+ "affiliations": [
49
+ {
50
+ "name": "Max-Planck-Institut für Entwicklungsbiologie",
51
+ "city": "Tübingen",
52
+ "country": "Federal Republic of Germany"
53
+ }
54
+ ]
55
+ }
56
+ ]
57
+ },
58
+ {
59
+ "id": "640",
60
+ "firstname": "Nelly",
61
+ "surname": "Sachs",
62
+ "born": "1891-12-10",
63
+ "died": "1970-05-12",
64
+ "bornCountry": "Germany",
65
+ "bornCountryCode": "DE",
66
+ "bornCity": "Berlin",
67
+ "diedCountry": "Sweden",
68
+ "diedCountryCode": "SE",
69
+ "diedCity": "Stockholm",
70
+ "gender": "female",
71
+ "prizes": [
72
+ {
73
+ "year": "1966",
74
+ "category": "literature",
75
+ "share": "2",
76
+ "motivation": "\"for her outstanding lyrical and dramatic writing, which interprets Israel's destiny with touching strength\" ",
77
+ "affiliations": [
78
+ [
79
+
80
+ ]
81
+ ]
82
+ }
83
+ ]
84
+ }
85
+ ]
86
+ }
@@ -0,0 +1,56 @@
1
+ {
2
+ "prizes": [
3
+ {
4
+ "year": "2012",
5
+ "category": "peace",
6
+ "laureates": [
7
+ {
8
+ "id": "881",
9
+ "firstname": "European Union (EU)",
10
+ "motivation": "&quot;for over six decades contributed to the advancement of peace and reconciliation, democracy and human rights in Europe&quot;",
11
+ "share": "1"
12
+ }
13
+ ]
14
+ },
15
+ {
16
+ "year": "2011",
17
+ "category": "peace",
18
+ "laureates": [
19
+ {
20
+ "id": "869",
21
+ "firstname": "Ellen",
22
+ "surname": "Johnson Sirleaf",
23
+ "motivation": "&quot;for their non-violent struggle for the safety of women and for women’s rights to full participation in peace-building work&quot;",
24
+ "share": "3"
25
+ },
26
+ {
27
+ "id": "870",
28
+ "firstname": "Leymah",
29
+ "surname": "Gbowee",
30
+ "motivation": "&quot;for their non-violent struggle for the safety of women and for women’s rights to full participation in peace-building work&quot;",
31
+ "share": "3"
32
+ },
33
+ {
34
+ "id": "871",
35
+ "firstname": "Tawakkol",
36
+ "surname": "Karman",
37
+ "motivation": "&quot;for their non-violent struggle for the safety of women and for women’s rights to full participation in peace-building work&quot;",
38
+ "share": "3"
39
+ }
40
+ ]
41
+ },
42
+ {
43
+ "year": "2010",
44
+ "category": "peace",
45
+ "laureates": [
46
+ {
47
+ "id": "855",
48
+ "firstname": "Liu",
49
+ "surname": "Xiaobo",
50
+ "motivation": "&quot;for his long and non-violent struggle for fundamental human rights in China&quot;",
51
+ "share": "1"
52
+ }
53
+ ]
54
+ }
55
+ ]
56
+ }
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative '../spec_helper'
4
+
5
+ describe "Affiliation" do
6
+ subject { Nobel::Affiliation }
7
+
8
+ it "can be blank" do
9
+ a = subject.new
10
+
11
+ a.name.must_equal nil
12
+ a.country.must_equal nil
13
+ a.city.must_equal nil
14
+ end
15
+
16
+ it "returns the affiliation name" do
17
+ subject.new('name' => 'foo').name.must_equal 'foo'
18
+ end
19
+
20
+ it "returns the affiliation country" do
21
+ subject.new('country' => 'bar').country.must_equal 'bar'
22
+ end
23
+
24
+ it "returns the affiliation city" do
25
+ subject.new('city' => 'baz').city.must_equal 'baz'
26
+ end
27
+ end
@@ -0,0 +1,42 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative '../spec_helper'
4
+
5
+ describe "Api" do
6
+ subject { Nobel::Api.new(config) }
7
+
8
+ let(:config) {
9
+ Nobel::Config.new.tap do |c|
10
+ c.api_host = api_host
11
+ c.api_version = api_version
12
+ c.http_client = BounceURI
13
+ end
14
+ }
15
+
16
+ let(:api_host) { 'api.test' }
17
+ let(:api_version) { '42' }
18
+
19
+ describe "get" do
20
+ it "should print debugging info if @config.debug?" do
21
+ ->{
22
+ subject.config.debug = true
23
+ subject.get('foo') }.must_output "GET: http://api.test/42/foo?\n"
24
+ end
25
+ end
26
+
27
+ describe "country" do
28
+ it "parses the country.json" do
29
+ subject.stub(:http_get, fixture(:country)) do
30
+ subject.country['countries'].first['name'].must_equal 'Germany (now Russia)'
31
+ end
32
+ end
33
+ end
34
+
35
+ describe "get_json" do
36
+ it "returns parsed json" do
37
+ subject.stub(:http_get, fixture(:country)) do
38
+ subject.get_json(:country)['countries'][2]['code'].must_equal 'LT'
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,56 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative '../spec_helper'
4
+
5
+ describe "Config" do
6
+ subject {
7
+ Nobel::Config.new do |c|
8
+ c.api_host = api_host
9
+ c.api_version = api_version
10
+ end
11
+ }
12
+
13
+ let(:api_host) { 'config.test' }
14
+ let(:api_version) { '2' }
15
+
16
+ it "returns the api_host" do
17
+ subject.api_host.must_equal api_host
18
+ end
19
+
20
+ it "returns the api_version" do
21
+ subject.api_version.must_equal api_version
22
+ end
23
+
24
+ it "returns the base_url" do
25
+ subject.base_url.must_equal 'http://config.test/2'
26
+ end
27
+
28
+ describe "json_parser" do
29
+ it "is a lambda by default" do
30
+ subject.json_parser.
31
+ call('{"foo":123}')['foo'].must_equal 123
32
+ end
33
+
34
+ it "can be changed" do
35
+ subject.json_parser = "foo"
36
+ subject.json_parser.must_equal "foo"
37
+ end
38
+ end
39
+
40
+ describe "http_client" do
41
+ it "is Net::HTTP by default" do
42
+ subject.http_client.must_equal Net::HTTP
43
+ end
44
+
45
+ it "can be changed" do
46
+ subject.http_client = "bar"
47
+ subject.http_client.must_equal "bar"
48
+ end
49
+ end
50
+
51
+ it "responds to debug?" do
52
+ subject.debug?.must_equal false
53
+ subject.debug = true
54
+ subject.debug?.must_equal true
55
+ end
56
+ end
@@ -0,0 +1,57 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative '../spec_helper'
4
+
5
+ describe "Country" do
6
+ subject { Nobel::Country }
7
+
8
+ describe "all" do
9
+ it "returns all the known countries" do
10
+ with_fixture :country do
11
+ subject.all.tap do |a|
12
+ a.size.must_equal 6
13
+ a.last.code.must_equal 'CZ'
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ describe "find" do
20
+ it "retrieves based on the code per default" do
21
+ with_fixture :country do
22
+ subject.find(:mg).name.must_equal 'Madagascar'
23
+ end
24
+ end
25
+
26
+ it "can also find based on the name" do
27
+ with_fixture :country do
28
+ subject.find('Tuscany (now Italy)', :name).code.must_equal 'IT'
29
+ end
30
+ end
31
+ end
32
+
33
+ describe "data" do
34
+ it "retrieves the country data" do
35
+ with_fixture :country do
36
+ subject.data.must_equal country_data
37
+ end
38
+ end
39
+ end
40
+
41
+ describe "reload!" do
42
+ it "reloads the data" do
43
+ with_fixture :country do
44
+ subject.instance_eval do
45
+ data.must_equal country_data
46
+
47
+ @data = []
48
+ data.must_equal []
49
+
50
+ reload!
51
+
52
+ data.must_equal country_data
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,110 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative '../spec_helper'
4
+
5
+ describe "Laureate" do
6
+ subject { Nobel::Laureate }
7
+
8
+ let(:nelly) {
9
+ subject.new(parsed_fixture(:laureate)["laureates"][2])
10
+ }
11
+
12
+ describe "find" do
13
+ it "finds a laureate based on id" do
14
+ with_data([parsed_fixture(:laureate)["laureates"][1]]) do
15
+ l = subject.find(453)
16
+
17
+ last_params.must_equal id: 453
18
+
19
+ l.name.must_equal 'Christiane Nüsslein-Volhard'
20
+ end
21
+ end
22
+ end
23
+
24
+ describe "all" do
25
+ it "returns all laureates per default" do
26
+ with_data(parsed_fixture(:laureate)["laureates"]) do
27
+ laureates = subject.all
28
+ laureates.size.must_equal 3
29
+
30
+ last_params.must_equal({})
31
+
32
+ laureates[2].name.must_equal 'Nelly Sachs'
33
+ end
34
+ end
35
+
36
+ it "queries using a params hash" do
37
+ with_data([]) do
38
+ params = { bornDate: 1960 }
39
+ subject.query(params)
40
+ last_params.must_equal params
41
+
42
+ params = { diedCity: 'Stockholm' }
43
+ subject.query(params)
44
+ last_params.must_equal params
45
+ end
46
+ end
47
+ end
48
+
49
+ describe "initialize" do
50
+ it "populates the accessors" do
51
+ nelly.instance_eval do
52
+ id.must_equal 640
53
+
54
+ firstname.must_equal "Nelly"
55
+ surname.must_equal "Sachs"
56
+
57
+ share.must_equal nil
58
+ motivation.must_equal nil
59
+
60
+ born.year.must_equal 1891
61
+ born_city.must_equal "Berlin"
62
+ born_country.must_equal "Germany"
63
+ born_country_code.must_equal "DE"
64
+
65
+ died.year.must_equal 1970
66
+ died_city.must_equal "Stockholm"
67
+ died_country.must_equal "Sweden"
68
+ died_country_code.must_equal "SE"
69
+ end
70
+ end
71
+ end
72
+
73
+ describe "reload!" do
74
+ it "reloads the data" do
75
+ with_data([parsed_fixture(:laureate)["laureates"][2]]) do
76
+ subject.new('id' => 640).instance_eval do
77
+ name.must_equal ''
78
+ reload!
79
+ name.must_equal 'Nelly Sachs'
80
+ end
81
+
82
+ last_params.must_equal({ id: 640 })
83
+ end
84
+ end
85
+ end
86
+
87
+ describe "name" do
88
+ it "returns the firstname and surname" do
89
+ nelly.name.must_equal "Nelly Sachs"
90
+ end
91
+ end
92
+
93
+ describe "prizes" do
94
+ it "returns an Array of Prize objects" do
95
+ nelly.prizes.first.year.must_equal 1966
96
+ end
97
+ end
98
+
99
+ describe "categories" do
100
+ it "returns a list of categories won" do
101
+ nelly.categories.must_equal(["literature"])
102
+ end
103
+ end
104
+
105
+ describe "years" do
106
+ it "returns a list of years won" do
107
+ nelly.years.must_equal([1966])
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative '../spec_helper'
4
+
5
+ describe "Prize" do
6
+ subject { Nobel::Prize }
7
+
8
+ let(:peace_2012) {
9
+ subject.new(parsed_fixture(:peace_prize)["prizes"][0])
10
+ }
11
+
12
+ describe "all" do
13
+ it "returns all prizes per default" do
14
+ with_data(parsed_fixture(:peace_prize)["prizes"]) do
15
+ prizes = subject.query
16
+ prizes.size.must_equal 3
17
+
18
+ last_params.must_equal({})
19
+
20
+ prizes[2].year.must_equal 2010
21
+ end
22
+ end
23
+
24
+ it "queries using a params hash" do
25
+ with_data([]) do
26
+ params = { bornDate: 1960 }
27
+ subject.query(params)
28
+ last_params.must_equal params
29
+
30
+ params = { diedCity: 'Stockholm' }
31
+ subject.query(params)
32
+ last_params.must_equal params
33
+ end
34
+ end
35
+ end
36
+
37
+ describe "initialize" do
38
+ it "populates the accessors" do
39
+ peace_2012.instance_eval do
40
+ year.must_equal 2012
41
+ category.must_equal "peace"
42
+ share.must_equal nil
43
+ motivation.must_equal nil
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative '../spec_helper'
4
+
5
+ describe "Version" do
6
+ subject { Nobel::VERSION }
7
+
8
+ it "returns a version string" do
9
+ subject.must_match(/^\d+\.\d+\.\d+$/)
10
+ end
11
+ end
@@ -0,0 +1,37 @@
1
+ # encoding: utf-8
2
+
3
+ describe "Nobel" do
4
+ subject { Nobel }
5
+
6
+ describe "configure" do
7
+ it "can be configured" do
8
+ subject.config.api_version.must_equal 'v1'
9
+
10
+ subject.configure do |c|
11
+ c.api_version = 'v2'
12
+ end
13
+
14
+ subject.config.api_version.must_equal 'v2'
15
+ end
16
+
17
+ it "can change the json parser lamda" do
18
+ subject.configure do |c|
19
+ c.json_parser = ->{ 'foo' }
20
+ end
21
+
22
+ subject.config.json_parser.call.must_equal 'foo'
23
+ end
24
+ end
25
+
26
+ describe "config" do
27
+ it "returns a (default) config object" do
28
+ subject.config.must_be_instance_of Nobel::Config
29
+ end
30
+ end
31
+
32
+ describe "api" do
33
+ it "returns a (default) api object" do
34
+ subject.api.must_be_instance_of Nobel::Api
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,44 @@
1
+ # encoding: utf-8
2
+
3
+ require "minitest/spec"
4
+ require "minitest/pride"
5
+ require "minitest/autorun"
6
+
7
+ require_relative "../lib/nobel"
8
+
9
+ class BounceURI
10
+ def self.get(uri)
11
+ uri
12
+ end
13
+ end
14
+
15
+ Nobel.configure do |c|
16
+ c.http_client = BounceURI
17
+ end
18
+
19
+ $loaded_fixtures = {}
20
+
21
+ def fixture(name)
22
+ $loaded_fixtures[name] ||= IO.read("spec/fixtures/#{name}.json")
23
+ end
24
+
25
+ def parsed_fixture(name)
26
+ JSON.parse fixture(name)
27
+ end
28
+
29
+ def with_fixture(name, &block)
30
+ Nobel.api.stub(:get_json, parsed_fixture(name), &block)
31
+ end
32
+
33
+ def with_data(data, &block)
34
+ @last_params = nil
35
+ subject.stub :get_data, ->(params){ @last_params = params; data }, &block
36
+ end
37
+
38
+ def last_params
39
+ @last_params
40
+ end
41
+
42
+ def country_data
43
+ parsed_fixture(:country)['countries']
44
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nobel
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Peter Hellberg
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-30 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description:
15
+ email:
16
+ - peter@c7.se
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - lib/nobel.rb
27
+ - lib/nobel/affiliation.rb
28
+ - lib/nobel/api.rb
29
+ - lib/nobel/config.rb
30
+ - lib/nobel/country.rb
31
+ - lib/nobel/laureate.rb
32
+ - lib/nobel/prize.rb
33
+ - lib/nobel/version.rb
34
+ - nobel.gemspec
35
+ - spec/fixtures/country.json
36
+ - spec/fixtures/laureate.json
37
+ - spec/fixtures/peace_prize.json
38
+ - spec/nobel/affiliation_spec.rb
39
+ - spec/nobel/api_spec.rb
40
+ - spec/nobel/config_spec.rb
41
+ - spec/nobel/country_spec.rb
42
+ - spec/nobel/laureate_spec.rb
43
+ - spec/nobel/prize_spec.rb
44
+ - spec/nobel/version_spec.rb
45
+ - spec/nobel_spec.rb
46
+ - spec/spec_helper.rb
47
+ homepage: https://github.com/peterhellberg/nobel
48
+ licenses:
49
+ - MIT
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: 1.9.3
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 1.8.24
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: API client for the Nobel Prize API
72
+ test_files:
73
+ - spec/fixtures/country.json
74
+ - spec/fixtures/laureate.json
75
+ - spec/fixtures/peace_prize.json
76
+ - spec/nobel/affiliation_spec.rb
77
+ - spec/nobel/api_spec.rb
78
+ - spec/nobel/config_spec.rb
79
+ - spec/nobel/country_spec.rb
80
+ - spec/nobel/laureate_spec.rb
81
+ - spec/nobel/prize_spec.rb
82
+ - spec/nobel/version_spec.rb
83
+ - spec/nobel_spec.rb
84
+ - spec/spec_helper.rb
85
+ has_rdoc: