rnvivo 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 [Fernando Blat]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,43 @@
1
+ # Nvivo API digester
2
+
3
+ A small Ruby class for consuming [Nvivo API](http://www.nvivo.es/api/index.php).
4
+
5
+ ## Dependencies
6
+
7
+ Just run `bundle install`.
8
+
9
+ ## How to use it
10
+
11
+ Once you have your API Key, just initalize the client and run the methods you want:
12
+
13
+ nvivo = Rnvivo::Nvivo.new NVIVO_API_KEY
14
+ events = nvivo.cityGetEvents('Madrid')
15
+
16
+ It returns an array of hashes with the name and the nvivo URL of each one:
17
+
18
+ 'events' => {
19
+ 'event' => [
20
+ {
21
+ 'name' => 'Wadus event',
22
+ 'url' => 'http://wadus.com/wadus-event'
23
+ },
24
+ {
25
+ 'name' => 'Wadus event 2',
26
+ 'url' => 'http://wadus.com/wadus-event-2'
27
+ }
28
+ ]
29
+ }
30
+
31
+ Very simple.
32
+
33
+ ## TODO
34
+
35
+ - refactor, refactor, refactor
36
+
37
+ ## Special thanks
38
+
39
+ - Sergio Arbeo [@serabe](https://twitter.com/serabe) for his PRs
40
+
41
+ - Nvivo team. They are really nice!
42
+
43
+ Copyright (c) 2009 [Fernando Blat](http://fernando.blat.es), released under the MIT license
@@ -0,0 +1,5 @@
1
+ class String
2
+ def blank?
3
+ self !~ /[^[:space:]]/
4
+ end
5
+ end
@@ -0,0 +1,133 @@
1
+ %w{rubygems httparty timeout}.each { |x| require x }
2
+
3
+ require 'core_ext/string'
4
+
5
+ $:.unshift(File.dirname(__FILE__)) unless
6
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
7
+
8
+ module Rnvivo
9
+ class NvivoError < StandardError; end
10
+ class NvivoTimeoutError < NvivoError; end
11
+ class NvivoResultError < NvivoError
12
+ attr_reader :message
13
+ def initialize(message)
14
+ @message = message
15
+ end
16
+ end
17
+
18
+ class Nvivo
19
+ include HTTParty
20
+
21
+ API_URL = "http://www.nvivo.es/api/request.php"
22
+ format :xml
23
+ attr_reader :api_key, :timeout
24
+
25
+ def initialize(key, timeout = 30)
26
+ @api_key = key
27
+ @timeout = timeout
28
+ end
29
+
30
+ def cityGetEvents(city, country = 'all')
31
+ raise ArgumentError, 'You must indicate a city' if city.blank?
32
+
33
+ options = query 'city.getEvents',
34
+ :city => city,
35
+ :country_iso => country
36
+
37
+ deal_with_response_for options do |r|
38
+ return nested_result r, 'response', 'events', 'event'
39
+ end
40
+ end
41
+
42
+ def artistGetEvents(artist, country='all', past = false)
43
+ raise ArgumentError, 'You must indicate an artist' if artist.blank?
44
+
45
+ options = query 'artist.getEvents',
46
+ :artist => artist,
47
+ :county_iso => country,
48
+ :past => past
49
+
50
+ deal_with_response_for options do |r|
51
+ return nested_result r, 'response', 'events', 'event'
52
+ end
53
+ end
54
+
55
+ def venueGetEvents(venue_id, past = false)
56
+ raise ArgumentError, 'You must indicate a venue' if venue_id.blank?
57
+
58
+ options = query 'venue.getEvents',
59
+ :venue_id => venue_id,
60
+ :past => past
61
+
62
+ deal_with_response_for options do |r|
63
+ return nested_result r, 'response', 'events', 'event'
64
+ end
65
+ end
66
+
67
+ def userGetEvents(user)
68
+ raise ArgumentError, 'You must indicate a user' if user.blank?
69
+
70
+ options = query 'user.getEvents',
71
+ :user => user
72
+
73
+ deal_with_response_for(options) do |r|
74
+ return nested_result r, 'response', 'events', 'event'
75
+ end
76
+ end
77
+
78
+ def venueFind(venue_name, country='all')
79
+ raise ArgumentError, 'You must indicate a venue name' if venue_name.blank?
80
+
81
+ options = query 'venue.find',
82
+ :venue_name => venue_name,
83
+ :country_iso => country
84
+
85
+ deal_with_response_for options do |r|
86
+ return nested_result r, 'response', 'venues', 'venue'
87
+ end
88
+ end
89
+
90
+ def venueGet(venue_id)
91
+ raise ArgumentError, 'You must inidicate a venue id' if venue_id.blank?
92
+
93
+ options = query 'venue.get',
94
+ :venue_id => venue_id
95
+
96
+ deal_with_response_for options do |r|
97
+ return nested_result r, 'response', 'venue'
98
+ end
99
+ end
100
+
101
+ protected
102
+
103
+ def query(method, *args)
104
+ {
105
+ query: args.first.merge({
106
+ method: method, api_key: @api_key
107
+ })
108
+ }
109
+ end
110
+
111
+ def nested_result(result, *args)
112
+ args.inject(result) do |memo, obj|
113
+ memo && memo[obj.to_s]
114
+ end || []
115
+ end
116
+
117
+ def deal_with_response_for(options, &block)
118
+ Timeout::timeout(@timeout) do
119
+ deal_with_result self.class.get(API_URL, options), &block
120
+ end
121
+ rescue Timeout::Error
122
+ raise NvivoTimeoutError
123
+ end
124
+
125
+ def deal_with_result(result)
126
+ if result['response']['status'] == 'success' && block_given?
127
+ return yield result
128
+ elsif result['response']['status'] == 'error'
129
+ return raise NvivoResultError.new(result['response']['error'])
130
+ end
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,3 @@
1
+ module Rnvivo
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,66 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),"..","test_helper"))
2
+ module Rnvivo
3
+
4
+ class ArtistGetEvents < MiniTest::Unit::TestCase
5
+ def test_artist_get_events_raises_nvivo_timeout_if_timeout
6
+ Nvivo.stubs(:get).raises(Timeout::Error)
7
+ n = Nvivo.new(FAKE_API_KEY)
8
+ assert_raises NvivoTimeoutError do
9
+ response = n.artistGetEvents('Madrid', 'es')
10
+ end
11
+ end
12
+
13
+ def test_artist_get_events_should_get_artist_events
14
+ result_200_ok = {
15
+ 'response' => {
16
+ 'status' => 'success',
17
+ 'events' => {
18
+ 'event' => [
19
+ {
20
+ 'name' => 'Wadus event',
21
+ 'url' => 'http://wadus.com/wadus-event'
22
+ },
23
+ {
24
+ 'name' => 'Wadus event 2',
25
+ 'url' => 'http://wadus.com/wadus-event-2'
26
+ }
27
+ ]
28
+ }
29
+ }
30
+ }
31
+ Nvivo.stubs(:get).returns(result_200_ok)
32
+ n = Nvivo.new(FAKE_API_KEY)
33
+ response = n.artistGetEvents('Wadus artist')
34
+ assert_equal response, result_200_ok['response']['events']['event']
35
+ end
36
+
37
+ def test_artist_get_events_should_return_error
38
+ result_error = {
39
+ 'response' => {
40
+ 'status' => 'error',
41
+ 'error' => {
42
+ 'id' => 7,
43
+ 'message' => 'Error message'
44
+ }
45
+ }
46
+ }
47
+ Nvivo.stubs(:get).returns(result_error)
48
+ n = Nvivo.new(FAKE_API_KEY)
49
+ assert_raises NvivoResultError do
50
+ response = n.artistGetEvents('Wadus artist')
51
+ end
52
+ end
53
+
54
+ def test_artist_get_events_should_get_artist_events_emtpy
55
+ result_200_ok = {
56
+ 'response' => {
57
+ 'status' => 'success'
58
+ }
59
+ }
60
+ Nvivo.stubs(:get).returns(result_200_ok)
61
+ n = Nvivo.new(FAKE_API_KEY)
62
+ response = n.artistGetEvents('Madrid', 'es')
63
+ assert_equal response, []
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,68 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),"..","test_helper"))
2
+
3
+ module Rnvivo
4
+
5
+ class CityGetEvents < MiniTest::Unit::TestCase
6
+
7
+ def test_city_get_events_should_raise_nvivo_timeout_if_timeout
8
+ Nvivo.stubs(:get).raises(Timeout::Error)
9
+ n = Nvivo.new(FAKE_API_KEY)
10
+ assert_raises NvivoTimeoutError do
11
+ response = n.cityGetEvents('Madrid', 'es')
12
+ end
13
+ end
14
+
15
+ def test_city_get_events_should_get_city_events_if_city_param
16
+ result_200_ok = {
17
+ 'response' => {
18
+ 'status' => 'success',
19
+ 'events' => {
20
+ 'event' => [
21
+ {
22
+ 'name' => 'Wadus event',
23
+ 'url' => 'http://wadus.com/wadus-event'
24
+ },
25
+ {
26
+ 'name' => 'Wadus event 2',
27
+ 'url' => 'http://wadus.com/wadus-event-2'
28
+ }
29
+ ]
30
+ }
31
+ }
32
+ }
33
+ Nvivo.stubs(:get).returns(result_200_ok)
34
+ n = Nvivo.new(FAKE_API_KEY)
35
+ response = n.cityGetEvents('Madrid', 'es')
36
+ assert_equal response, result_200_ok['response']['events']['event']
37
+ end
38
+
39
+ def test_city_get_events_should_get_city_events_emtpy
40
+ result_200_ok = {
41
+ 'response' => {
42
+ 'status' => 'success'
43
+ }
44
+ }
45
+ Nvivo.stubs(:get).returns(result_200_ok)
46
+ n = Nvivo.new(FAKE_API_KEY)
47
+ response = n.cityGetEvents('Madrid', 'es')
48
+ assert_equal response, []
49
+ end
50
+
51
+ def test_city_get_events_should_return_error
52
+ result_error = {
53
+ 'response' => {
54
+ 'status' => 'error',
55
+ 'error' => {
56
+ 'id' => 7,
57
+ 'message' => 'Error message'
58
+ }
59
+ }
60
+ }
61
+ Nvivo.stubs(:get).returns(result_error)
62
+ n = Nvivo.new(FAKE_API_KEY)
63
+ assert_raises NvivoResultError do
64
+ response = n.cityGetEvents('Madriz', 'es')
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,68 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),"..","test_helper"))
2
+
3
+ module Rnvivo
4
+
5
+ class UserGetEvents < MiniTest::Unit::TestCase
6
+
7
+ def test_user_get_events_should_raise_nvivo_timeout_if_timeout
8
+ Nvivo.stubs(:get).raises(Timeout::Error)
9
+ n = Nvivo.new(FAKE_API_KEY)
10
+ assert_raises NvivoTimeoutError do
11
+ response = n.userGetEvents('wadus')
12
+ end
13
+ end
14
+
15
+ def test_user_get_events_should_get_user_events_if_user_param
16
+ result_200_ok = {
17
+ 'response' => {
18
+ 'status' => 'success',
19
+ 'events' => {
20
+ 'event' => [
21
+ {
22
+ 'name' => 'Wadus event',
23
+ 'url' => 'http://wadus.com/wadus-event'
24
+ },
25
+ {
26
+ 'name' => 'Wadus event 2',
27
+ 'url' => 'http://wadus.com/wadus-event-2'
28
+ }
29
+ ]
30
+ }
31
+ }
32
+ }
33
+ Nvivo.stubs(:get).returns(result_200_ok)
34
+ n = Nvivo.new(FAKE_API_KEY)
35
+ response = n.userGetEvents('wadus')
36
+ assert_equal response, result_200_ok['response']['events']['event']
37
+ end
38
+
39
+ def test_user_get_events_should_get_user_events_emtpy
40
+ result_200_ok = {
41
+ 'response' => {
42
+ 'status' => 'success'
43
+ }
44
+ }
45
+ Nvivo.stubs(:get).returns(result_200_ok)
46
+ n = Nvivo.new(FAKE_API_KEY)
47
+ response = n.userGetEvents('wadus')
48
+ assert_equal response, []
49
+ end
50
+
51
+ def test_user_get_events_should_return_error
52
+ result_error = {
53
+ 'response' => {
54
+ 'status' => 'error',
55
+ 'error' => {
56
+ 'id' => 7,
57
+ 'message' => 'Error message'
58
+ }
59
+ }
60
+ }
61
+ Nvivo.stubs(:get).returns(result_error)
62
+ n = Nvivo.new(FAKE_API_KEY)
63
+ assert_raises NvivoResultError do
64
+ response = n.userGetEvents('wadus')
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,68 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),"..","test_helper"))
2
+
3
+ module Rnvivo
4
+
5
+ class VenueFind < MiniTest::Unit::TestCase
6
+
7
+ def test_venue_find_should_raise_nvivo_timeout_if_timeout
8
+ Nvivo.stubs(:get).raises(Timeout::Error)
9
+ n = Nvivo.new(FAKE_API_KEY)
10
+ assert_raises NvivoTimeoutError do
11
+ response = n.venueFind('Sala Wadus')
12
+ end
13
+ end
14
+
15
+ def test_venue_find_should_get_venues
16
+ result_200_ok = {
17
+ 'response' => {
18
+ 'status' => 'success',
19
+ 'venues' => {
20
+ 'venue' => [
21
+ {
22
+ 'name' => 'Wadus room',
23
+ 'url' => 'http://wadus.com/wadus-event'
24
+ },
25
+ {
26
+ 'name' => 'Sala Wadus',
27
+ 'url' => 'http://wadus.com/wadus-event-2'
28
+ }
29
+ ]
30
+ }
31
+ }
32
+ }
33
+ Nvivo.stubs(:get).returns(result_200_ok)
34
+ n = Nvivo.new(FAKE_API_KEY)
35
+ response = n.venueFind('Sala Wadus')
36
+ assert_equal response, result_200_ok['response']['venues']['venue']
37
+ end
38
+
39
+ def test_venue_find_should_get_emtpy_venues
40
+ result_200_ok = {
41
+ 'response' => {
42
+ 'status' => 'success'
43
+ }
44
+ }
45
+ Nvivo.stubs(:get).returns(result_200_ok)
46
+ n = Nvivo.new(FAKE_API_KEY)
47
+ response = n.venueFind('Sala Wadus')
48
+ assert_equal response, []
49
+ end
50
+
51
+ def test_venue_find_should_return_error
52
+ result_error = {
53
+ 'response' => {
54
+ 'status' => 'error',
55
+ 'error' => {
56
+ 'id' => 7,
57
+ 'message' => 'Error message'
58
+ }
59
+ }
60
+ }
61
+ Nvivo.stubs(:get).returns(result_error)
62
+ n = Nvivo.new(FAKE_API_KEY)
63
+ assert_raises NvivoResultError do
64
+ response = n.venueFind('Sala Wadus')
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,68 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),"..","test_helper"))
2
+
3
+ module Rnvivo
4
+
5
+ class VenueGetEvents < MiniTest::Unit::TestCase
6
+
7
+ def test_venue_get_events_should_raise_nvivo_timeout_if_timeout
8
+ Nvivo.stubs(:get).raises(Timeout::Error)
9
+ n = Nvivo.new(FAKE_API_KEY)
10
+ assert_raises NvivoTimeoutError do
11
+ response = n.venueGetEvents('91')
12
+ end
13
+ end
14
+
15
+ def test_venue_get_events_should_get_venue_events_if_venue_names
16
+ result_200_ok = {
17
+ 'response' => {
18
+ 'status' => 'success',
19
+ 'events' => {
20
+ 'event' => [
21
+ {
22
+ 'name' => 'Wadus event',
23
+ 'url' => 'http://wadus.com/wadus-event'
24
+ },
25
+ {
26
+ 'name' => 'Wadus event 2',
27
+ 'url' => 'http://wadus.com/wadus-event-2'
28
+ }
29
+ ]
30
+ }
31
+ }
32
+ }
33
+ Nvivo.stubs(:get).returns(result_200_ok)
34
+ n = Nvivo.new(FAKE_API_KEY)
35
+ response = n.venueGetEvents('91')
36
+ assert_equal response, result_200_ok['response']['events']['event']
37
+ end
38
+
39
+ def test_venue_get_events_should_get_venue_events_emtpy
40
+ result_200_ok = {
41
+ 'response' => {
42
+ 'status' => 'success'
43
+ }
44
+ }
45
+ Nvivo.stubs(:get).returns(result_200_ok)
46
+ n = Nvivo.new(FAKE_API_KEY)
47
+ response = n.venueGetEvents('91')
48
+ assert_equal response, []
49
+ end
50
+
51
+ def test_venue_get_events_should_return_error
52
+ result_error = {
53
+ 'response' => {
54
+ 'status' => 'error',
55
+ 'error' => {
56
+ 'id' => 7,
57
+ 'message' => 'Error message'
58
+ }
59
+ }
60
+ }
61
+ Nvivo.stubs(:get).returns(result_error)
62
+ n = Nvivo.new(FAKE_API_KEY)
63
+ assert_raises NvivoResultError do
64
+ response = n.venueGetEvents('91')
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,60 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),"..","test_helper"))
2
+
3
+ module Rnvivo
4
+
5
+ class VenueGet < MiniTest::Unit::TestCase
6
+
7
+ def test_venue_get_should_raise_nvivo_timeout_if_timeout
8
+ Nvivo.stubs(:get).raises(Timeout::Error)
9
+ n = Nvivo.new(FAKE_API_KEY)
10
+ assert_raises NvivoTimeoutError do
11
+ response = n.venueGet('Sala Wadus')
12
+ end
13
+ end
14
+
15
+ def test_venue_get_should_get_venues
16
+ result_200_ok = {
17
+ 'response' => {
18
+ 'status' => 'success',
19
+ 'venue' => {
20
+ 'name' => 'Wadus room',
21
+ 'url' => 'http://wadus.com/wadus-event'
22
+ }
23
+ }
24
+ }
25
+ Nvivo.stubs(:get).returns(result_200_ok)
26
+ n = Nvivo.new(FAKE_API_KEY)
27
+ response = n.venueGet('91')
28
+ assert_equal response, result_200_ok['response']['venue']
29
+ end
30
+
31
+ def test_venue_get_should_get_emtpy_venues
32
+ result_200_ok = {
33
+ 'response' => {
34
+ 'status' => 'success'
35
+ }
36
+ }
37
+ Nvivo.stubs(:get).returns(result_200_ok)
38
+ n = Nvivo.new(FAKE_API_KEY)
39
+ response = n.venueGet('91')
40
+ assert_equal response, []
41
+ end
42
+
43
+ def test_venue_get_should_return_error
44
+ result_error = {
45
+ 'response' => {
46
+ 'status' => 'error',
47
+ 'error' => {
48
+ 'id' => 7,
49
+ 'message' => 'Error message'
50
+ }
51
+ }
52
+ }
53
+ Nvivo.stubs(:get).returns(result_error)
54
+ n = Nvivo.new(FAKE_API_KEY)
55
+ assert_raises NvivoResultError do
56
+ response = n.venueGet('91')
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,7 @@
1
+ require 'minitest/autorun'
2
+ require File.expand_path(File.join(File.dirname(__FILE__), '/../lib/rnvivo'))
3
+ require 'minitest/reporters'
4
+ MiniTest::Reporters.use!
5
+ require 'mocha/setup'
6
+
7
+ FAKE_API_KEY = 'wadus'
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rnvivo
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Fernando Blat
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httparty
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: A simple Ruby library for the nvivo API
31
+ email:
32
+ - ferblape@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/core_ext/string.rb
38
+ - lib/rnvivo.rb
39
+ - lib/version.rb
40
+ - test/nvivo/artistGetEvents_test.rb
41
+ - test/nvivo/cityGetEvents_test.rb
42
+ - test/nvivo/userGetEvents_test.rb
43
+ - test/nvivo/venueFind_test.rb
44
+ - test/nvivo/venueGet_test.rb
45
+ - test/nvivo/venueGetEvents_test.rb
46
+ - test/test_helper.rb
47
+ - MIT-LICENSE
48
+ - README.md
49
+ homepage: http://github.com/ferblape/rnvivo
50
+ licenses: []
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 1.8.24
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: nvivo API client in Ruby
73
+ test_files: []