madcowley-jambase4r 1.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,33 @@
1
+ module JamBase4R
2
+
3
+ class Event
4
+ include Model
5
+
6
+ attr_reader :id, :artists, :venue, :ticket_url, :event_url, :date
7
+
8
+ def initialize(element)
9
+ return if element.blank?
10
+ build(element)
11
+ end
12
+
13
+ private
14
+
15
+ def build(element)
16
+ @artists = []
17
+ @id = get_value(element, EVENT_ID)
18
+ @ticket_url = get_value(element, TICKET_URL)
19
+ @event_url = get_value(element, EVENT_URL)
20
+ @date = get_value(element, EVENT_DATE)
21
+ v = element.get_elements(VENUE)
22
+ unless v.blank?
23
+ @venue = Venue.new(v.first)
24
+ end
25
+ artists = element.get_elements(ARTISTS)
26
+ unless artists.blank?
27
+ artists.each {|a| @artists << Artist.new(a) }
28
+ end
29
+ end
30
+
31
+ end
32
+
33
+ end
@@ -0,0 +1,31 @@
1
+ module JamBase4R
2
+ module Model
3
+
4
+ EVENT_ID = "event_id"
5
+ TICKET_URL = "ticket_url"
6
+ EVENT_URL = "event_url"
7
+ VENUE = "venue"
8
+ EVENT_DATE = "event_date"
9
+ ARTISTS = "artists/artist"
10
+ VENUE_ID = "venue_id"
11
+ VENUE_NAME = "venue_name"
12
+ VENUE_CITY = "venue_city"
13
+ VENUE_STATE = "venue_state"
14
+ VENUE_ZIP = "venue_zip"
15
+ ARTIST_ID = "artist_id"
16
+ ARTIST_NAME = "artist_name"
17
+
18
+ def get_value(element, node_name)
19
+ return if element.blank? || node_name.blank?
20
+ collection = element.get_elements(node_name)
21
+ return (collection.blank? ? nil : get_element_value(collection.first))
22
+ end
23
+
24
+ private
25
+
26
+ def get_element_value(element)
27
+ element.nil? ? nil : (element.get_text.nil? ? nil : element.get_text.value)
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,25 @@
1
+ module JamBase4R
2
+
3
+ class Venue
4
+ include Model
5
+
6
+ attr_reader :id, :name, :city, :state, :zip
7
+
8
+ def initialize(element)
9
+ return if element.blank?
10
+ build(element)
11
+ end
12
+
13
+ private
14
+
15
+ def build(element)
16
+ @id = get_value(element, VENUE_ID)
17
+ @name = get_value(element, VENUE_NAME)
18
+ @city = get_value(element, VENUE_CITY)
19
+ @state = get_value(element, VENUE_STATE)
20
+ @zip = get_value(element, VENUE_ZIP)
21
+ end
22
+
23
+ end
24
+
25
+ end
data/lib/utility.rb ADDED
@@ -0,0 +1,13 @@
1
+ module JamBase4R
2
+ module Utility
3
+
4
+ def log_info(msg)
5
+ JamBase4R.logger.info(msg) if JamBase4R.logger && JamBase4R.logger.respond_to?(:info)
6
+ end
7
+
8
+ def log_error(msg)
9
+ JamBase4R.logger.error(msg) if JamBase4R.logger && JamBase4R.logger.respond_to?(:error)
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,167 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe JamBase4R::API do
4
+
5
+ it "should respond_to? search" do
6
+ JamBase4R::API.should respond_to(:search)
7
+ end
8
+
9
+ it "should respond_to? search_by_artist" do
10
+ JamBase4R::API.should respond_to(:search_by_artist)
11
+ end
12
+
13
+ it "should respond_to? search by zip_code" do
14
+ JamBase4R::API.should respond_to(:search_by_zipcode)
15
+ end
16
+
17
+ it "should respond_to? search by user" do
18
+ JamBase4R::API.should respond_to(:search_by_user)
19
+ end
20
+
21
+ it "should respond_to? log_error" do
22
+ JamBase4R::API.should respond_to(:log_error)
23
+ end
24
+
25
+ it "should respond_to? log_info" do
26
+ JamBase4R::API.should respond_to(:log_info)
27
+ end
28
+
29
+ describe "search" do
30
+
31
+ before(:each) do
32
+ @r = stub("Response", :status => "200", :message => "OK", :body => xml_response)
33
+ JamBase4R::API.stub!(:jambase_search).and_return(@r)
34
+ end
35
+
36
+ it "should call jambase_search" do
37
+ JamBase4R::API.should_receive(:jambase_search).with(:band => "monkey").and_return(@r)
38
+ JamBase4R::API.search(:band => "monkey")
39
+ end
40
+
41
+ it "should convert the response to an array" do
42
+ JamBase4R::API.should_receive(:convert).with(xml_response)
43
+ JamBase4R::API.search(:band => "monkey")
44
+ end
45
+
46
+ end
47
+
48
+ describe "custom methods" do
49
+
50
+ before(:each) do
51
+ @r = stub("Response", :status => "200", :message => "OK", :body => xml_response)
52
+ JamBase4R::API.stub!(:search).and_return(@r)
53
+ end
54
+
55
+ describe "search_by_zipcode" do
56
+
57
+ it "should replace the value if :zip in additional filters" do
58
+ options = {:zip => "90210"}
59
+ JamBase4R::API.search_by_zipcode("99999", options)
60
+ options[:zip].should == "99999"
61
+ end
62
+
63
+ it "should call search" do
64
+ JamBase4R::API.should_receive(:search).with({:zip => "99999"}).and_return(@r)
65
+ JamBase4R::API.search_by_zipcode("99999")
66
+ end
67
+
68
+ end
69
+
70
+ describe "search_by_user" do
71
+
72
+ it "should replace the value if :user in additional filters" do
73
+ options = {:user => "A User"}
74
+ JamBase4R::API.search_by_user("A 2nd User", options)
75
+ options[:user].should == "A 2nd User"
76
+ end
77
+
78
+ it "should call search" do
79
+ JamBase4R::API.should_receive(:search).with({:user => "Mr X"}).and_return(@r)
80
+ JamBase4R::API.search_by_user("Mr X")
81
+ end
82
+
83
+ end
84
+
85
+ describe "search_by_artist" do
86
+
87
+ it "should replace the value of :artists in additional filters" do
88
+ options = {:artist => "TEST"}
89
+ JamBase4R::API.search_by_artist("A Band", options)
90
+ options[:artist].should == "A Band"
91
+ end
92
+
93
+ it "should call search" do
94
+ JamBase4R::API.should_receive(:search).with({:artist => "A Band"}).and_return(@r)
95
+ JamBase4R::API.search_by_artist("A Band")
96
+ end
97
+
98
+ end
99
+
100
+ describe "convert" do
101
+
102
+ it "should return an empty array if the xml passed to convert is nil" do
103
+ JamBase4R::API.send(:convert, nil).should be_is_a(Array)
104
+ end
105
+
106
+ it "should return an empty array if the xml passed to convert is empty" do
107
+ JamBase4R::API.send(:convert, "").should be_is_a(Array)
108
+ end
109
+
110
+ it "should convert xml passed to convert into an Array" do
111
+ JamBase4R::API.send(:convert, xml_response).should be_is_a(Array)
112
+ end
113
+
114
+ end
115
+
116
+ describe "Search" do
117
+
118
+ def default_search_args
119
+ {:artist => "Some Band", :zip => "90210", :radius => 50, :user => "Mr X"}
120
+ end
121
+
122
+ before(:each) do
123
+ JamBase4R.configure do |c|
124
+ c.api_key = "dfsffd"
125
+ end
126
+ @resp = mock("Response")
127
+ @resp.stub!(:code).and_return("200")
128
+ @resp.stub!(:message).and_return("OK")
129
+ @resp.stub!(:body).and_return(xml_response)
130
+ @gateway = mock('Gateway')
131
+ @gateway.stub!(:get).and_return(@resp)
132
+ JamBase4R::API.stub!(:jambase_gateway).and_return(@gateway)
133
+ end
134
+
135
+ it "should return nil from jambase_search if search args are nil" do
136
+ JamBase4R::API.send(:jambase_search, nil).should be_nil
137
+ end
138
+
139
+ it "should return nil from jambase_search if search args are empty" do
140
+ JamBase4R::API.send(:jambase_search, {}).should be_nil
141
+ end
142
+
143
+ it "should return nil if after cleaning the search args they are empty" do
144
+ search_args = default_search_args.merge(
145
+ :zip => nil, :radius => "", :artist => "", :user => "")
146
+ JamBase4R::API.send(:jambase_search, search_args).should be_nil
147
+ end
148
+
149
+ it "should raise an exception if both the aliased param and the real param are set" do
150
+ search_args = default_search_args.merge(:band => "The Monkeys")
151
+ lambda {
152
+ JamBase4R::API.send(:jambase_search, search_args)
153
+ }.should raise_error
154
+ end
155
+
156
+ it "should call get on the gateway object with a properly constructed url" do
157
+ url = "http://api.jambase.com/search?band=Some+Band&apikey=dfsffd"
158
+ @gateway.should_receive(:get).with(url).and_return(@resp)
159
+ JamBase4R::API.send(:jambase_search, default_search_args.merge(
160
+ :zip => nil, :radius => nil, :user => nil))
161
+ end
162
+
163
+ end
164
+
165
+ end
166
+
167
+ end
@@ -0,0 +1,38 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe JamBase4R::HttpGateway do
4
+
5
+ GET_URL = "http://api.jambase.com/search?apikey=dsdfgdfg&band=Zero"
6
+
7
+ before(:each) do
8
+ @gate = JamBase4R::HttpGateway.new
9
+ @resp = mock("Response")
10
+ @resp.stub!(:code).and_return("200")
11
+ @resp.stub!(:message).and_return("OK")
12
+ @resp.stub!(:body).and_return("gdfgdfgdfgdfgfgdgdf")
13
+ end
14
+
15
+ it "should respond_to? log_error" do
16
+ @gate.should respond_to(:log_error)
17
+ end
18
+
19
+ it "should respond_to? log_info" do
20
+ @gate.should respond_to(:log_info)
21
+ end
22
+
23
+ it "should use the Net::HTTP::Get class to get" do
24
+ get = Net::HTTP::Get.new(URI.parse(GET_URL).path_with_querystring)
25
+ Net::HTTP.stub!(:start).and_return(@resp)
26
+ Net::HTTP::Get.should_receive(:new).with(URI.parse(GET_URL).path_with_querystring).and_return(get)
27
+ @gate.get(GET_URL)
28
+ end
29
+
30
+ it "should pass the URI path and query string for GET requests" do
31
+ Net::HTTP.stub!(:start).and_return(@resp)
32
+ url = URI.parse(GET_URL)
33
+ URI.stub!(:parse).and_return(url)
34
+ url.should_receive(:path_with_querystring).and_return("/search?apikey=dsdfgdfg&band=Zero")
35
+ @gate.get(GET_URL)
36
+ end
37
+
38
+ end
@@ -0,0 +1,51 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe JamBase4R do
4
+
5
+ it "should respond_to? :configure" do
6
+ JamBase4R.should respond_to(:configure)
7
+ end
8
+
9
+ it "should respond_to? :log_error" do
10
+ JamBase4R.should respond_to(:log_error)
11
+ end
12
+
13
+ it "should respond_to? :log_info" do
14
+ JamBase4R.should respond_to(:log_info)
15
+ end
16
+
17
+ it "should raise an exception if the api key is not set" do
18
+ lambda{
19
+ JamBase4R.configure do |s|
20
+ s.api_key = nil
21
+ end
22
+ }.should raise_error
23
+ end
24
+
25
+ it "should log an error if the api key is not set" do
26
+ log = mock("Logger")
27
+ log.should_receive(:error)
28
+ begin
29
+ JamBase4R.configure do |c|
30
+ c.api_key = nil
31
+ c.logger = log
32
+ end
33
+ rescue
34
+ end
35
+ end
36
+
37
+ it "should not raise an exception when configured with an API key" do
38
+ lambda {
39
+ JamBase4R.configure do |c|
40
+ c.api_key = "dffsdfsfdf"
41
+ end
42
+ }.should_not raise_error
43
+ end
44
+
45
+ it "should raise an exception if configure is not passed a block" do
46
+ lambda {
47
+ JamBase4R.configure
48
+ }.should raise_error
49
+ end
50
+
51
+ end
@@ -0,0 +1,36 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe JamBase4R::Artist do
4
+
5
+ def create_mock_element(expect=true)
6
+ e = mock("Element")
7
+ e.stub!(:blank?).and_return(!expect)
8
+
9
+ id_value = mock("ID Value")
10
+ expect ? id_value.should_receive(:value).and_return(10) : id_value.should_not_receive(:value)
11
+ id = mock("Artist ID")
12
+ expect ? id.should_receive(:get_text).at_least(1).times.and_return(id_value) : id.should_not_receive(:get_text)
13
+
14
+ expect ? e.should_receive(:get_elements).with("artist_id").and_return([id]) :
15
+ e.should_not_receive(:get_elements).with("artist_id")
16
+
17
+ name_value = mock("Name Value")
18
+ expect ? name_value.should_receive(:value).and_return("Joe Artist") : name_value.should_not_receive(:value)
19
+ name = mock("Artist Name")
20
+ expect ? name.should_receive(:get_text).at_least(1).times.and_return(name_value) : name.should_not_receive(:get_text)
21
+
22
+ expect ? e.should_receive(:get_elements).with("artist_name").and_return([name]) :
23
+ e.should_not_receive(:get_elements).with("artist_name")
24
+
25
+ return e
26
+ end
27
+
28
+ it "should build itself from the passed in element" do
29
+ JamBase4R::Artist.new(create_mock_element)
30
+ end
31
+
32
+ it "should return an empty object if the element passed in responds true to blank?" do
33
+ JamBase4R::Artist.new(create_mock_element(false))
34
+ end
35
+
36
+ end
@@ -0,0 +1,102 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe JamBase4R::Event do
4
+
5
+ def create_mock_element(expect=true, create_venue=true, create_artists=true)
6
+ e = mock("Element")
7
+ e.stub!(:blank?).and_return(!expect)
8
+
9
+ #id
10
+ id_value = mock("ID Value")
11
+ expect ? id_value.should_receive(:value).and_return(10) : id_value.should_not_receive(:value)
12
+ id = mock("Event ID")
13
+ expect ? id.should_receive(:get_text).at_least(1).times.and_return(id_value) : id.should_not_receive(:get_text)
14
+
15
+ expect ? e.should_receive(:get_elements).with("event_id").and_return([id]) :
16
+ e.should_not_receive(:get_elements).with("event_id")
17
+
18
+ #ticket url
19
+ ticket_url_value = mock("Ticket Url Value")
20
+ expect ? ticket_url_value.should_receive(:value).and_return("http://test.ca") :
21
+ ticket_url_value.should_not_receive(:value)
22
+ ticket_url = mock("Ticket Url")
23
+ expect ? ticket_url.should_receive(:get_text).at_least(1).times.and_return(ticket_url_value) :
24
+ ticket_url.should_not_receive(:get_text)
25
+
26
+ expect ? e.should_receive(:get_elements).with("ticket_url").and_return([ticket_url]) :
27
+ e.should_not_receive(:get_elements).with("ticket_url")
28
+
29
+ #event url
30
+ event_url_value = mock("Event Url Value")
31
+ expect ? event_url_value.should_receive(:value).and_return("http://test.ca") :
32
+ event_url_value.should_not_receive(:value)
33
+ event_url = mock("Event Url")
34
+ expect ? event_url.should_receive(:get_text).at_least(1).times.and_return(event_url_value) :
35
+ event_url.should_not_receive(:get_text)
36
+
37
+ expect ? e.should_receive(:get_elements).with("event_url").and_return([event_url]) :
38
+ e.should_not_receive(:get_elements).with("event_url")
39
+
40
+ #date
41
+ date_value = mock("Event Date Value")
42
+ expect ? date_value.should_receive(:value).and_return("10/21/2009") :
43
+ date_value.should_not_receive(:value)
44
+ date = mock("Event Date")
45
+ expect ? date.should_receive(:get_text).at_least(1).times.and_return(date_value) :
46
+ date.should_not_receive(:get_text)
47
+
48
+ expect ? e.should_receive(:get_elements).with("event_date").and_return([date]) :
49
+ e.should_not_receive(:get_elements).with("event_date")
50
+
51
+ #venue
52
+ venues = nil
53
+ if create_venue
54
+ venue = mock("Venue")
55
+ venue.stub!(:get_elements).and_return([])
56
+ venues = [venue]
57
+ end
58
+ expect ? e.should_receive(:get_elements).with("venue").and_return(venues) :
59
+ e.should_not_receive(:get_elements).with("venue")
60
+
61
+ #artists
62
+ artists = nil
63
+ if create_artists
64
+ artist = mock("Artist")
65
+ artist.stub!(:get_elements).and_return([])
66
+ artists = [artist]
67
+ end
68
+ expect ? e.should_receive(:get_elements).with("artists/artist").and_return(artists) :
69
+ e.should_not_receive(:get_elements).with("artists/artist")
70
+
71
+ return e
72
+ end
73
+
74
+ it "should build itself from the passed in element" do
75
+ JamBase4R::Event.new(create_mock_element(true, true, false))
76
+ end
77
+
78
+ it "should not build itself if the element passed in responds true to blank?" do
79
+ JamBase4R::Event.new(create_mock_element(false))
80
+ end
81
+
82
+ it "should create a venue if there is venue XML" do
83
+ JamBase4R::Venue.should_receive(:new)
84
+ JamBase4R::Event.new(create_mock_element)
85
+ end
86
+
87
+ it "should not create a venue if there is no venue XML" do
88
+ JamBase4R::Venue.should_not_receive(:new)
89
+ JamBase4R::Event.new(create_mock_element(true, false, false))
90
+ end
91
+
92
+ it "should create artists if there is artist XML" do
93
+ JamBase4R::Artist.should_receive(:new).exactly(1).times
94
+ JamBase4R::Event.new(create_mock_element(true, true, true))
95
+ end
96
+
97
+ it "should not create artists if there is no artist XML" do
98
+ JamBase4R::Artist.should_not_receive(:new)
99
+ JamBase4R::Event.new(create_mock_element(true, true, false))
100
+ end
101
+
102
+ end