snafu 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. data/.gitignore +1 -0
  2. data/CHANGELOG.textile +12 -1
  3. data/README.textile +68 -7
  4. data/lib/snafu.rb +9 -2
  5. data/lib/snafu/achievements.rb +17 -0
  6. data/lib/snafu/client.rb +33 -23
  7. data/lib/snafu/giants.rb +35 -0
  8. data/lib/snafu/models.rb +4 -1
  9. data/lib/snafu/models/achievement.rb +24 -0
  10. data/lib/snafu/models/giant.rb +21 -0
  11. data/lib/snafu/models/glitch_time.rb +116 -0
  12. data/lib/snafu/models/hub.rb +5 -2
  13. data/lib/snafu/models/location.rb +3 -3
  14. data/lib/snafu/models/street.rb +13 -5
  15. data/lib/snafu/util.rb +12 -0
  16. data/lib/snafu/version.rb +1 -1
  17. data/snafu.gemspec +2 -2
  18. data/spec/snafu/achievements_spec.rb +51 -0
  19. data/spec/snafu/client_spec.rb +37 -15
  20. data/spec/snafu/giants_spec.rb +60 -0
  21. data/spec/snafu/locations_spec.rb +33 -32
  22. data/spec/snafu/models/achievement_spec.rb +31 -0
  23. data/spec/snafu/models/giant_spec.rb +38 -0
  24. data/spec/snafu/models/glitch_image_spec.rb +7 -17
  25. data/spec/snafu/models/glitch_time_spec.rb +176 -0
  26. data/spec/snafu/models/hub_spec.rb +2 -2
  27. data/spec/snafu/models/street_spec.rb +1 -1
  28. data/spec/snafu/models/util_spec.rb +22 -0
  29. data/spec/snafu_spec.rb +4 -3
  30. data/spec/spec_helper.rb +17 -16
  31. metadata +46 -54
  32. data/spec/fixtures/vcr_cassettes/Snafu_Client/call_should_accept_a_hash_of_POST_arguments.yml +0 -39
  33. data/spec/fixtures/vcr_cassettes/Snafu_Client/call_should_automatically_pass_in_the_oauth_token_if.yml +0 -36
  34. data/spec/fixtures/vcr_cassettes/Snafu_Client/call_should_automatically_pass_in_the_oauth_token_if_authenticate.yml +0 -36
  35. data/spec/fixtures/vcr_cassettes/Snafu_Client/call_should_automatically_pass_in_the_oauth_token_if_authenticate_called_with_true.yml +0 -36
  36. data/spec/fixtures/vcr_cassettes/Snafu_Client/call_should_automatically_pass_in_the_oauth_token_if_called_with_authenticate.yml +0 -36
  37. data/spec/fixtures/vcr_cassettes/Snafu_Client/call_should_automatically_pass_in_the_oauth_token_if_called_with_authenticate_true.yml +0 -36
  38. data/spec/fixtures/vcr_cassettes/Snafu_Client/call_should_automatically_pass_in_the_oauth_token_if_called_with_true.yml +0 -36
  39. data/spec/fixtures/vcr_cassettes/Snafu_Client/call_should_raise_a_GlitchAPIError_if_receiving_an_unsucessful_response_from_the_Glitch_API.yml +0 -36
  40. data/spec/fixtures/vcr_cassettes/Snafu_Locations/get_hub_should_return_a_valid_hub_if_given_a_valid_Hub_ID.yml +0 -39
  41. data/spec/fixtures/vcr_cassettes/Snafu_Locations/get_hub_should_return_an_array_of_valid_street_information_if_given_a_valid_Hub_ID.yml +0 -39
  42. data/spec/fixtures/vcr_cassettes/Snafu_Locations/get_hubs_should_populated_the_returned_Hub_objects_with_an_id_and_name.yml +0 -39
  43. data/spec/fixtures/vcr_cassettes/Snafu_Locations/get_hubs_should_return_an_array_of_Hub_objects.yml +0 -39
  44. data/spec/fixtures/vcr_cassettes/Snafu_Locations/get_hubs_should_return_an_array_of_all_hubs.yml +0 -39
  45. data/spec/fixtures/vcr_cassettes/Snafu_Locations/get_street_should_return_a_complete_street_if_given_a_valid_street_ID.yml +0 -40
  46. data/spec/fixtures/vcr_cassettes/Snafu_Models_Hub/initialize_should_populate_values_if_given_the_raw_JSON_response_from_HTTParty.yml +0 -39
  47. data/spec/fixtures/vcr_cassettes/Snafu_Models_Hub/initialize_should_populate_values_if_given_the_raw_response_from_Glitch.yml +0 -39
  48. data/spec/fixtures/vcr_cassettes/Snafu_Models_Street/initialize_should_populate_values_if_given_the_raw_JSON_response_from_HTTParty.yml +0 -40
  49. data/spec/fixtures/vcr_cassettes/Snafu_Models_Street_new/should_populate_values_if_given_the_raw_JSON_response_from_HTTParty.yml +0 -40
  50. data/spec/fixtures/vcr_cassettes/calendar_getHoldays/valid_calendar.yml +0 -39
@@ -3,7 +3,7 @@ module Snafu
3
3
  # Defines a class for Glitch Hubs, which are the various regions in the game.
4
4
  #
5
5
  class Hub < Location
6
- attr_reader :streets
6
+ attr_reader :streets, :id
7
7
 
8
8
  # Accepts either the raw JSON-formatted response from the HTTParty get request or an options
9
9
  # hash.
@@ -15,7 +15,7 @@ module Snafu
15
15
  @name = options["name"]
16
16
  @streets = []
17
17
  options["streets"].each do |street_id, street|
18
- @streets << {:id => street_id, :name => street["name"]}
18
+ @streets << Street.new(:id => street_id, :name => street["name"])
19
19
  end
20
20
  else
21
21
  @id = options[:id]
@@ -27,6 +27,9 @@ module Snafu
27
27
  end
28
28
  end
29
29
  end
30
+ def to_s
31
+ "Glitch Hub - ID: #{self.id} Name: #{self.name}"
32
+ end
30
33
  end
31
34
  end
32
35
  end
@@ -9,12 +9,12 @@ module Snafu
9
9
  @id = options[:id].to_s
10
10
  @name = options[:name]
11
11
  end
12
- def to_s
13
- "#{self.id} - #{self.name}"
14
- end
15
12
  def id
16
13
  @id.to_s
17
14
  end
15
+ def to_s
16
+ "Glitch Generic Location: ID: #{self.id} - Name: #{self.name}"
17
+ end
18
18
  end
19
19
  end
20
20
  end
@@ -3,6 +3,7 @@ module Snafu
3
3
  class Street < Location
4
4
  attr_reader :hub, :connections, :mote, :features, :image, :active_project
5
5
  alias_method :active_project?, :active_project
6
+ alias_method :tsid, :id
6
7
  def initialize(options = {})
7
8
  if options.class == HTTParty::Response
8
9
  @id = options["tsid"]
@@ -19,11 +20,15 @@ module Snafu
19
20
  )
20
21
  end
21
22
  @mote = Hub.new(:id => options["mote"]["id"], :name => options["mote"]["name"])
22
- @image = GlitchImage.new(
23
- :url => options["image"]["url"],
24
- :width => options["image"]["w"],
25
- :height => options["image"]["h"]
26
- )
23
+
24
+ unless options["image"].nil?
25
+ @image = GlitchImage.new(
26
+ :url => options["image"]["url"],
27
+ :width => options["image"]["w"],
28
+ :height => options["image"]["h"]
29
+ )
30
+ end
31
+
27
32
  @active_project = options["active_project"]
28
33
  else
29
34
  @id = options[:id]
@@ -36,6 +41,9 @@ module Snafu
36
41
  @active_project = options[:active_project] || false
37
42
  end
38
43
  end
44
+ def to_s
45
+ "Glitch Street: #{self.id} - #{self.name}"
46
+ end
39
47
  end
40
48
  end
41
49
  end
@@ -0,0 +1,12 @@
1
+ module Snafu
2
+ # Contains convenience methods that don't seem to fit anywhere else
3
+ module Util
4
+ def glitch_time(timestamp = nil)
5
+ if timestamp
6
+ Snafu::Models::GlitchTime.new(timestamp)
7
+ else
8
+ Snafu::Models::GlitchTime.new
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,3 +1,3 @@
1
1
  module Snafu
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -10,8 +10,7 @@ Gem::Specification.new do |s|
10
10
  s.email = ["jeff@jkbrowning.com"]
11
11
  s.homepage = "https://github.com/jbrowning/snafu"
12
12
  s.summary = %q{A library for the Glitch API}
13
- s.description = %q{Snafu is an interface to API for Glitch, an online multi-player
14
- game created by Tiny Speck.}
13
+ s.description = %q{Snafu is a Ruby gem that provides an interface to the API for Glitch, a browser-based MMO created by Tiny Speck.}
15
14
 
16
15
  s.required_ruby_version = '>= 1.9.2'
17
16
 
@@ -27,5 +26,6 @@ Gem::Specification.new do |s|
27
26
  s.add_development_dependency 'vcr'
28
27
  s.add_development_dependency 'fakeweb'
29
28
  s.add_development_dependency 'rake'
29
+ s.add_development_dependency 'timecop'
30
30
  s.add_runtime_dependency 'httparty', '~> 0.8'
31
31
  end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ describe Snafu::Achievements do
4
+ before(:all) do
5
+ @snafu = Snafu.new
6
+ end
7
+
8
+ let(:all_achievements) { @snafu.get_achievements }
9
+
10
+ describe "#get_achievements_count" do
11
+ it "should return the count of all achievements in Glitch" do
12
+ VCR.use_cassette("achievements.listAll") do
13
+ achievement_count = @snafu.call("achievements.listAll").parsed_response["total"]
14
+ @snafu.achievement_count.should eql(achievement_count)
15
+ end
16
+ end
17
+ end
18
+
19
+ describe "#get_achievements" do
20
+ it "should return an array", :vcr do
21
+ all_achievements.should be_an(Array)
22
+ end
23
+
24
+ it "should return an array of the same size as the total number of achievements" do
25
+ VCR.use_cassette("achievements.listAll", :record => :new_episodes) do
26
+ all_achievements = @snafu.get_achievements
27
+ achievements_count = @snafu.last_request_result["total"]
28
+ all_achievements.count.should eql(achievements_count)
29
+ end
30
+ end
31
+
32
+ it "should return an array of Achievement objects", :vcr do
33
+ all_achievements.each do |achievement|
34
+ achievement.should be_a(Snafu::Models::Achievement)
35
+ end
36
+ end
37
+
38
+ it "should return an array of fully-populated achievements" do
39
+ VCR.use_cassette("achievements.listAll") do
40
+ all_achievements.each do |achievement|
41
+ achievement.name.should_not be_nil
42
+ achievement.desc.should_not be_nil
43
+ achievement.url.should_not be_nil
44
+ achievement.image_60.should_not be_nil
45
+ achievement.image_180.should_not be_nil
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ end
@@ -2,15 +2,13 @@ require 'spec_helper'
2
2
 
3
3
  describe Snafu::Client do
4
4
  before(:each) do
5
- @snafu = Snafu.new(:api_key => GLITCH_API_KEY, :oauth_token => GLITCH_OAUTH_TOKEN)
5
+ @snafu = Snafu.new
6
6
  end
7
7
 
8
- describe 'call()' do
8
+ describe '#call' do
9
9
  it 'should receive a successful response from the Glitch API if given an valid method', :vcr do
10
- VCR.use_cassette "calendar.getHoldays/valid_calendar" do
11
- result = @snafu.call("calendar.getHolidays")
12
- result['ok'].should eql(1)
13
- end
10
+ result = @snafu.call("calendar.getHolidays")
11
+ result['ok'].should eql(1)
14
12
  end
15
13
 
16
14
  it 'should raise a GlitchAPIError if receiving an unsucessful response from the Glitch API', :vcr do
@@ -24,18 +22,42 @@ describe Snafu::Client do
24
22
  result["name"].should eql(hub_name)
25
23
  end
26
24
 
27
- it "should automatically pass in the oauth token if called with authenticate", :vcr do
28
- expect { response = @snafu.call("players.stats", :authenticate => true) }.to_not raise_exception
25
+ if defined? GLITCH_OAUTH_READ_TOKEN
26
+ it "should automatically pass in the oauth token if called with authenticate", :vcr do
27
+ snafu = Snafu.new(:oauth_token => GLITCH_OAUTH_READ_TOKEN)
28
+ expect { response = snafu.call("players.stats", :authenticate => true) }.to_not raise_exception
29
+ end
29
30
  end
30
- end
31
31
 
32
- context "authentication" do
33
- describe "call()" do
34
- context "without oauth token"
35
- it "should raise a GlitchAPIError if trying to do an authenticated call without an oauth token" do
36
- snafu = Snafu.new()
37
- expect { snafu.call("secureMethod", :authenticate => true) }.to raise_error(Snafu::GlitchAPIError, /oauth token/)
32
+ context "authentication" do
33
+ it "should raise a GlitchAuthenticationError if trying to perform an authenticated call without an oauth token", :vcr do
34
+ snafu = Snafu.new()
35
+ expect { snafu.call("secureMethod", :authenticate => true) }.to raise_error(Snafu::GlitchAuthenticationError, /oauth token/)
36
+ end
37
+
38
+ it "should raise a GlitchAuthenticationError if calling the API with an invalid OAuth token", :vcr, :record => :all do
39
+ snafu = Snafu.new(:oauth_token => "invalid token")
40
+ expect { snafu.call("giants.getFavor", :authenticate => true) }.to raise_error(Snafu::GlitchAuthenticationError, /invalid token/i)
41
+ end
42
+
43
+ it 'should raise a GlitchAuthenticationError if calling an authenticated API method without a token', :vcr do
44
+ expect { @snafu.call("giants.getFavor") }.to raise_error(Snafu::GlitchAuthenticationError)
45
+ end
46
+
47
+ if defined? GLITCH_OAUTH_READ_TOKEN
48
+ it "should raise a GlitchAuthenticationError if calling a method that requires a higher scope", :vcr do
49
+ snafu = Snafu.new(:oauth_token => GLITCH_OAUTH_IDENTITY_TOKEN)
50
+ expect { snafu.call("giants.getFavor", :authenticate => true) }.to raise_error(/insufficient scope/)
38
51
  end
52
+ end
39
53
  end
40
54
  end
55
+
56
+ describe "#last_request_result" do
57
+ it "should return the raw contents of the last request result", :vcr do
58
+ result = @snafu.call("calendar.getHolidays")
59
+ @snafu.last_request_result.should eql(result)
60
+ end
61
+ end
62
+
41
63
  end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ describe Snafu::Giants do
4
+ before(:each) do
5
+ @snafu = Snafu.new()
6
+ end
7
+
8
+ describe "#get_giants" do
9
+ let(:giants) { @snafu.get_giants }
10
+
11
+ it "should return a non-empty array", :vcr do
12
+ giants.should be_an Array
13
+ giants.should_not be_empty
14
+ end
15
+
16
+ it "should return an array of Giant objects", :vcr do
17
+ giants.each do |element|
18
+ element.should be_a Snafu::Models::Giant
19
+ end
20
+ end
21
+
22
+ it "should have populate the returned Giant objects with a name", :vcr do
23
+ giants.each do |giant|
24
+ giant.name.should_not be_empty
25
+ end
26
+ end
27
+ end
28
+
29
+ describe "#get_giants_favor", :if => defined?(GLITCH_OAUTH_READ_TOKEN) do
30
+
31
+ before(:each) do
32
+ @snafu = Snafu.new(:oauth_token => GLITCH_OAUTH_READ_TOKEN)
33
+ end
34
+
35
+ it "should raise an error if there is no OAuth token set" do
36
+ snafu = Snafu.new
37
+ expect { snafu.get_giants_favor }.to raise_error(Snafu::GlitchAuthenticationError)
38
+ end
39
+
40
+ it "should return a non-empty Array", :vcr do
41
+ giants_favor = @snafu.get_giants_favor
42
+ giants_favor.should be_an Array
43
+ giants_favor.should_not be_empty
44
+ end
45
+
46
+
47
+ it "should return an Array of fully-populated Giant objects", :vcr do
48
+ giants_favor = @snafu.get_giants_favor
49
+ giants_favor.each do |giant|
50
+ giant.should be_a(Snafu::Models::Giant)
51
+ giant.name.should_not be_empty
52
+ giant.cur_favor.should_not be_nil
53
+ giant.max_favor.should_not be_nil
54
+ giant.cur_daily_favor.should_not be_nil
55
+ giant.max_daily_favor.should_not be_nil
56
+ end
57
+ end
58
+ end
59
+
60
+ end
@@ -2,60 +2,61 @@ require 'spec_helper'
2
2
 
3
3
  describe Snafu::Locations do
4
4
  before(:each) do
5
- @snafu = Snafu.new(:api_key => GLITCH_API_KEY, :oauth_token => GLITCH_OAUTH_TOKEN)
6
- @test_hub_id = 27
7
- @test_hub_name = "Ix"
8
- @test_street_id = "LM416LNIKVLM1"
9
- @test_street_name = "Baby Steppes"
5
+ @snafu = Snafu.new
10
6
  end
11
7
 
12
- context "get_hubs()" do
13
- before(:each) do
14
- @hubs = @snafu.get_hubs
15
- end
8
+ let(:test_hub) { Snafu::Models::Hub.new(:id => 27, :name => "Ix") }
9
+ let(:test_street_id) { "LM416LNIKVLM1" }
10
+ let(:test_street_name) { "Baby Steppes" }
11
+ let(:test_hub_id) { "27" }
12
+
13
+ context "#get_hubs" do
16
14
 
15
+ let(:hubs) { @snafu.get_hubs }
16
+
17
17
  it "should return an array of all hubs", :vcr do
18
- @hubs.should_not be_empty
18
+ hubs.should_not be_empty
19
19
  end
20
20
 
21
21
  it "should return an array of Hub objects", :vcr do
22
- @hubs.first.should be_a Snafu::Models::Hub
22
+ hubs.first.should be_a Snafu::Models::Hub
23
23
  end
24
24
 
25
- it "should populated the returned Hub objects with an id and name", :vcr do
26
- first_hub = @hubs.first
25
+ it "should populate the returned Hub objects with an id and name", :vcr do
26
+ first_hub = hubs.first
27
27
  first_hub.id.should_not be_nil
28
28
  first_hub.name.should_not be_nil
29
29
  end
30
30
  end
31
31
 
32
- context "get_hub" do
32
+ context "#get_hub" do
33
33
  it "should return a valid hub if given a valid Hub ID", :vcr do
34
- hub = @snafu.get_hub(@test_hub_id)
35
- hub.name.should eql(@test_hub_name)
34
+ hub = @snafu.get_hub(test_hub_id)
35
+ hub.should be_a Snafu::Models::Hub
36
+ hub.name.should eql(test_hub.name)
36
37
  end
37
38
 
38
39
  it "should return an array of valid street information if given a valid Hub ID", :vcr do
39
- test_array = {:id => @test_street_id, :name => @test_street_name}
40
- hub = @snafu.get_hub(@test_hub_id)
41
- hub.streets.should include(test_array)
40
+ hub = @snafu.get_hub(test_hub_id)
41
+ hub.streets.each do |street|
42
+ street.should be_a Snafu::Models::Street
43
+ street.id.should_not be_nil
44
+ street.name.should_not be_nil
45
+ end
42
46
  end
43
47
  end
44
48
 
45
- context "get_street" do
46
- before(:each) do
47
- @test_street = @snafu.get_street(@test_street_id)
48
- end
49
-
49
+ context "#get_street" do
50
50
  it "should return a complete street if given a valid street ID", :vcr do
51
- @test_street.id.should_not be_nil
52
- @test_street.name.should_not be_nil
53
- @test_street.mote.should_not be_nil
54
- @test_street.active_project?.should_not be_nil
55
- @test_street.connections.should_not be_empty
56
- @test_street.image.url.should_not be_nil
57
- @test_street.image.width.should_not be_nil
58
- @test_street.image.height.should_not be_nil
51
+ test_street = @snafu.get_street(test_street_id)
52
+ test_street.id.should_not be_nil
53
+ test_street.name.should_not be_nil
54
+ test_street.mote.should_not be_nil
55
+ test_street.active_project?.should_not be_nil
56
+ test_street.connections.should_not be_empty
57
+ test_street.image.url.should_not be_nil
58
+ test_street.image.width.should_not be_nil
59
+ test_street.image.height.should_not be_nil
59
60
  end
60
61
  end
61
62
  end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ module Snafu
4
+ module Models
5
+ describe Snafu::Models::Achievement do
6
+ before(:each) do
7
+ @valid_options = {
8
+ "name" => "a name",
9
+ "desc" => "a description",
10
+ "url" => "http://example.com",
11
+ "image_60" => "http://example.com/60.png",
12
+ "image_180" => "http://example.com/180.png"
13
+ }
14
+ @achievement = Achievement.new @valid_options
15
+ end
16
+
17
+ it "should populate values if given an options hash" do
18
+ @achievement.name.should eql(@valid_options["name"])
19
+ @achievement.description.should eql(@valid_options["desc"])
20
+ @achievement.url.should eql(@valid_options["url"])
21
+ @achievement.image_60.url.should eql(@valid_options["image_60"])
22
+ @achievement.image_180.url.should eql(@valid_options["image_180"])
23
+ end
24
+
25
+ it "should alias #description as #desc" do
26
+ @achievement.desc.should eql(@achievement.description)
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ module Snafu
4
+ module Models
5
+ describe Snafu::Models::Giant do
6
+
7
+ GIANT_LIST = ["Alph", "Cosma", "Friendly", "Grendaline",
8
+ "Humbaba", "Lem", "Mab", "Pot", "Spriggan",
9
+ "Tii", "Zille" ]
10
+
11
+ describe "#initialize" do
12
+ let(:random_giant) { GIANT_LIST.sample }
13
+
14
+ it "should populate values if given an options hash" do
15
+ expected_name = random_giant
16
+ expected_cur_favor = rand(1000)
17
+ expected_max_favor = expected_cur_favor + 1
18
+ expected_cur_daily_favor = rand(1000)
19
+ expected_max_daily_favor = expected_cur_daily_favor + 1
20
+
21
+ giant = Giant.new(
22
+ :name => expected_name,
23
+ :cur_favor => expected_cur_favor.to_s,
24
+ :max_favor => expected_max_favor.to_s,
25
+ :cur_daily_favor => expected_cur_daily_favor.to_s,
26
+ :max_daily_favor => expected_max_daily_favor.to_s
27
+ )
28
+
29
+ giant.name.should eql(expected_name)
30
+ giant.cur_favor.should eql(expected_cur_favor)
31
+ giant.max_favor.should eql(expected_max_favor)
32
+ giant.cur_daily_favor.should eql(expected_cur_daily_favor)
33
+ giant.max_daily_favor.should eql(expected_max_daily_favor)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end