snafu 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/.gitignore +5 -0
  2. data/Gemfile +4 -0
  3. data/Guardfile +10 -0
  4. data/LICENSE +7 -0
  5. data/README.textile +65 -0
  6. data/Rakefile +6 -0
  7. data/lib/snafu.rb +6 -0
  8. data/lib/snafu/client.rb +68 -0
  9. data/lib/snafu/locations.rb +32 -0
  10. data/lib/snafu/models.rb +4 -0
  11. data/lib/snafu/models/glitch_image.rb +17 -0
  12. data/lib/snafu/models/hub.rb +32 -0
  13. data/lib/snafu/models/location.rb +17 -0
  14. data/lib/snafu/models/street.rb +41 -0
  15. data/lib/snafu/version.rb +3 -0
  16. data/snafu.gemspec +31 -0
  17. data/spec/fixtures/vcr_cassettes/Snafu_Client/call_should_accept_a_hash_of_POST_arguments.yml +39 -0
  18. data/spec/fixtures/vcr_cassettes/Snafu_Client/call_should_automatically_pass_in_the_oauth_token_if.yml +36 -0
  19. data/spec/fixtures/vcr_cassettes/Snafu_Client/call_should_automatically_pass_in_the_oauth_token_if_authenticate.yml +36 -0
  20. data/spec/fixtures/vcr_cassettes/Snafu_Client/call_should_automatically_pass_in_the_oauth_token_if_authenticate_called_with_true.yml +36 -0
  21. data/spec/fixtures/vcr_cassettes/Snafu_Client/call_should_automatically_pass_in_the_oauth_token_if_called_with_authenticate.yml +36 -0
  22. data/spec/fixtures/vcr_cassettes/Snafu_Client/call_should_automatically_pass_in_the_oauth_token_if_called_with_authenticate_true.yml +36 -0
  23. data/spec/fixtures/vcr_cassettes/Snafu_Client/call_should_automatically_pass_in_the_oauth_token_if_called_with_true.yml +36 -0
  24. data/spec/fixtures/vcr_cassettes/Snafu_Client/call_should_raise_a_GlitchAPIError_if_receiving_an_unsucessful_response_from_the_Glitch_API.yml +36 -0
  25. data/spec/fixtures/vcr_cassettes/Snafu_Locations/get_hub_should_return_a_valid_hub_if_given_a_valid_Hub_ID.yml +39 -0
  26. data/spec/fixtures/vcr_cassettes/Snafu_Locations/get_hubs_should_populated_the_returned_Hub_objects_with_an_id_and_name.yml +39 -0
  27. data/spec/fixtures/vcr_cassettes/Snafu_Locations/get_hubs_should_return_an_array_of_Hub_objects.yml +39 -0
  28. data/spec/fixtures/vcr_cassettes/Snafu_Locations/get_hubs_should_return_an_array_of_all_hubs.yml +39 -0
  29. data/spec/fixtures/vcr_cassettes/Snafu_Locations/get_street_should_return_a_complete_street_if_given_a_valid_street_ID.yml +40 -0
  30. data/spec/fixtures/vcr_cassettes/Snafu_Models_Hub/initialize_should_populate_values_if_given_the_raw_JSON_response_from_HTTParty.yml +39 -0
  31. data/spec/fixtures/vcr_cassettes/Snafu_Models_Hub/initialize_should_populate_values_if_given_the_raw_response_from_Glitch.yml +39 -0
  32. data/spec/fixtures/vcr_cassettes/Snafu_Models_Street/initialize_should_populate_values_if_given_the_raw_JSON_response_from_HTTParty.yml +40 -0
  33. data/spec/fixtures/vcr_cassettes/calendar_getHoldays/valid_calendar.yml +39 -0
  34. data/spec/snafu/client_spec.rb +41 -0
  35. data/spec/snafu/locations_spec.rb +55 -0
  36. data/spec/snafu/models/glitch_image_spec.rb +36 -0
  37. data/spec/snafu/models/hub_spec.rb +43 -0
  38. data/spec/snafu/models/street_spec.rb +81 -0
  39. data/spec/snafu/models_spec.rb +9 -0
  40. data/spec/snafu_spec.rb +14 -0
  41. data/spec/spec_helper.rb +28 -0
  42. metadata +179 -0
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ api_key.rb
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in snafu.gemspec
4
+ gemspec
@@ -0,0 +1,10 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :version => 2 do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch(%r{^lib/snafu/(.+)\.rb$}) { |m| "spec/snafu/#{m[1]}_spec.rb" }
8
+ watch('spec/spec_helper.rb') { "spec" }
9
+ end
10
+
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2011 Jeff Browning
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,65 @@
1
+ h1. Snafu
2
+
3
+ Snafu is a Ruby gem that provides an interface to the "Glitch API":http://api.glitch.com. "Glitch":http://glitch.com is an online multi-player game created by Tiny Speck and provides a decent API for interacting with its world.
4
+
5
+ h2. Usage
6
+
7
+ To get started, instantiate a client by passing in an optional oauth token of an authenticated user. Be sure to pass in an OAuth token with the appropriate scope level for API method you wish to call.
8
+
9
+ <pre lang="ruby">
10
+ snafu = Snafu.new(:oauth_token => "your-oauth-token")
11
+ </pre>
12
+
13
+ Snafu currently does not support Glitch's OAuth authentication. There are other gems that support authenticating with Glitch including the excellent "OmniAuth":https://github.com/intridea/omniauth.
14
+
15
+ h3. Supported Methods
16
+
17
+ Currently, only the locations.* methods are supported via helper methods. Each method returns a Ruby object representing the returned data.
18
+
19
+ <pre lang="ruby">
20
+ # locations.*
21
+ hubs = snafu.get_hubs # "locations.getHubs"
22
+ hub = snafu.get_hub(hub_id) # "locations.getStreets"
23
+ street = snafu.get_street(street_tsid) # "locations.streetInfo"
24
+ </pre>
25
+
26
+ The rest of the API will be supported in due time.
27
+
28
+ You can also receive raw data from the Glitch API by passing in any method as a string and supplying required parameters via an options hash. This returns a Hash representation of the JSON returned by Glitch:
29
+
30
+ <pre lang="ruby">
31
+ snafu.call("calendar.getHolidays")
32
+ => {
33
+ => "ok"=>1,
34
+ => "days"=>
35
+ => [{"month"=>1, "day"=>5, "name"=>"AlphCon"},
36
+ => {"month"=>2, "day"=>2, "name"=>"Lemadan"},
37
+ => {"month"=>3, "day"=>3, "name"=>"Pot Twoday"},
38
+ => {"month"=>4, "day"=>2, "name"=>"Root"},
39
+ => {"month"=>4, "day"=>3, "name"=>"Root"},
40
+ => {"month"=>4, "day"=>4, "name"=>"Root"},
41
+ => {"month"=>4, "day"=>11, "name"=>"Sprinkling"},
42
+ => {"month"=>6, "day"=>17, "name"=>"Croppaday"},
43
+ => {"month"=>7, "day"=>1, "name"=>"Belabor Day"},
44
+ => {"month"=>8, "day"=>37, "name"=>"Zilloween"},
45
+ => {"month"=>11, "day"=>11, "name"=>"Recurse Eve"}]
46
+ => }
47
+
48
+ snafu.call("locations.getStreets", :hub_id => 27)
49
+ => {
50
+ => "ok"=>1,
51
+ => "hub_id"=>"27",
52
+ => "name"=>"Ix",
53
+ => "streets"=>
54
+ => {
55
+ => "LM416LNIKVLM1"=>{"name"=>"Baby Steppes"},
56
+ => "LM413SQ6LRN58"=>{"name"=>"East Spice"},
57
+ => "LM410QQ0CHARO"=>{"name"=>"Flipside"},
58
+ => "LM4115NJ46G8M"=>{"name"=>"Groddle Ladder"},
59
+ => "LM4109NI2R640"=>{"name"=>"Guillermo Gamera Way"},
60
+ => "LM410TMR0S2S1"=>{"name"=>"Ruta Asuncion"},
61
+ => "LM413RQ6LRG9N"=>{"name"=>"West Spice"}
62
+ => }
63
+ => }
64
+ </pre>
65
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,6 @@
1
+ require 'httparty'
2
+
3
+ require "snafu/version"
4
+ require "snafu/locations"
5
+ require "snafu/client"
6
+ require "snafu/models"
@@ -0,0 +1,68 @@
1
+ module Snafu
2
+ API_URL = 'http://api.glitch.com/simple'
3
+
4
+ def self.new(options={})
5
+ Snafu::Client.new(options)
6
+ end
7
+
8
+ class Client
9
+ # it's p-p-p-party time
10
+ include HTTParty
11
+
12
+ # include other classes & modules so they can be called on the returned client object
13
+ include Snafu::Locations
14
+
15
+ base_uri API_URL
16
+
17
+ attr_accessor :oauth_token
18
+
19
+ def initialize(options={})
20
+ @oauth_token = options[:oauth_token]
21
+ end
22
+
23
+ # Make a raw call to the Glitch API.
24
+ #
25
+ # snafu = Snafu.new(:oauth_token => "some-token")
26
+ # snafu.call("calendar.getHolidays")
27
+ #
28
+ # For Glitch methods which require authentication, set <tt>:authentication => true</tt>
29
+ #
30
+ # snafu = snafu.call("players.stats", :authenticate => true)
31
+ #
32
+ # Invalid method calls will raise a <tt>GlitchAPIError</tt>
33
+
34
+ def call(method, query_parameters={})
35
+ if method.is_a? String
36
+ options = { :format => :json }
37
+ unless query_parameters.empty?
38
+ options[:query] = query_parameters
39
+ if options[:query].has_key?(:authenticate) && options[:query][:authenticate] == true
40
+ if self.oauth_token.nil?
41
+ raise GlitchAPIError.new("You cannot do an authenticated call without an oauth token")
42
+ end
43
+
44
+ # Replace the authenticate
45
+ options[:query].delete(:authenticate)
46
+ options[:query].update(:oauth_token => GLITCH_OAUTH_TOKEN)
47
+ end
48
+ end
49
+ request_uri = "/#{method}"
50
+ parse_response(self.class.get(request_uri, options))
51
+ else
52
+ raise ArgumentError.new("Method argument must be a string")
53
+ end
54
+ end
55
+
56
+ def parse_response(response)
57
+ if response["ok"] == 0
58
+ raise GlitchAPIError.new(response["error"])
59
+ else
60
+ response
61
+ end
62
+ end
63
+ end
64
+
65
+
66
+
67
+ class GlitchAPIError < StandardError;end
68
+ end
@@ -0,0 +1,32 @@
1
+ module Snafu
2
+ module Locations
3
+
4
+ # Accesses the "locations.getHubs" method from the Glitch API
5
+ # and returns an Array of the returned Hub information.
6
+ #
7
+ def get_hubs
8
+ hubs = []
9
+ response = self.call("locations.getHubs")
10
+ response["hubs"].each do |key,value|
11
+ hubs << Models::Hub.new(:id => key, :name => value["name"])
12
+ end
13
+ hubs
14
+ end
15
+
16
+ # Accesses the "locations.getStreets" method from the Glitch API passing in
17
+ # the supplied Hub ID.
18
+ #
19
+ def get_hub(hub_id)
20
+ response = self.call("locations.getStreets", :hub_id => hub_id)
21
+ Models::Hub.new(response)
22
+ end
23
+
24
+ # Accesses the "locations.streetInfo" method from the Glitch API padding in
25
+ # the supplied street ID
26
+ #
27
+ def get_street(street_id)
28
+ response = self.call("locations.streetInfo", :street_tsid => street_id)
29
+ Models::Street.new(response)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,4 @@
1
+ require 'snafu/models/glitch_image'
2
+ require 'snafu/models/location'
3
+ require 'snafu/models/hub'
4
+ require 'snafu/models/street'
@@ -0,0 +1,17 @@
1
+ module Snafu
2
+ module Models
3
+ # Provides a simple class to represent image information returned by the
4
+ # Glitch API.
5
+ #
6
+ class GlitchImage
7
+ attr_reader :url, :width, :height
8
+ # Accepts an options hash with <tt>:url</tt>, <tt>:width</tt>, and
9
+ # <tt>:height</tt> properties
10
+ def initialize(options={})
11
+ @url = options[:url]
12
+ @width = options[:width].to_i || 0
13
+ @height = options[:height].to_i || 0
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,32 @@
1
+ module Snafu
2
+ module Models
3
+ # Defines a class for Glitch Hubs, which are the various regions in the game.
4
+ #
5
+ class Hub < Location
6
+ attr_reader :streets
7
+
8
+ # Accepts either the raw JSON-formatted response from the HTTParty get request or an options
9
+ # hash.
10
+ #
11
+ def initialize(options = {})
12
+ if options.class == HTTParty::Response
13
+ # construct the street information
14
+ @id = options["hub_id"]
15
+ @name = options["name"]
16
+ @streets = []
17
+ options["streets"].each do |street_id, street|
18
+ @streets << {:id => street_id, :name => street[:name]}
19
+ end
20
+ else
21
+ @id = options[:id]
22
+ @name = options[:name]
23
+ if options[:streets].is_a? Array
24
+ @streets = options[:streets]
25
+ else
26
+ @streets = [options[:streets]]
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,17 @@
1
+ module Snafu
2
+ module Models
3
+ # Defines a generic location class with <tt>id</tt> and <tt>name</tt>
4
+ # properties.
5
+ class Location
6
+ attr_reader :id, :name
7
+ # Accepts an options hash with <tt>:id</tt> or <tt>:name</tt>
8
+ def initializer(options={})
9
+ @id = options[:id]
10
+ @name = options[:name]
11
+ end
12
+ def to_s
13
+ "#{self.id} - #{self.name}"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,41 @@
1
+ module Snafu
2
+ module Models
3
+ class Street < Location
4
+ attr_reader :hub, :connections, :mote, :features, :image, :active_project
5
+ alias_method :active_project?, :active_project
6
+ def initialize(options = {})
7
+ if options.class == HTTParty::Response
8
+ @id = options["tsid"]
9
+ @name = options["name"]
10
+ @hub = Hub.new(:id => options["hub"]["id"], :name => options["hub"]["name"])
11
+ @features = options["features"]
12
+ @connections = []
13
+ options["connections"].each do |street_id, street|
14
+ @connections << Street.new(
15
+ :id => street_id,
16
+ :name => street["name"],
17
+ :hub => Hub.new(:id => options["hub"]["id"], :name => options["hub"]["name"]),
18
+ :mote => Hub.new(:id => options["mote"]["id"], :name => options["mote"]["name"]),
19
+ )
20
+ end
21
+ @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
+ )
27
+ @active_project = options["active_project"]
28
+ else
29
+ @id = options[:id]
30
+ @name = options[:name]
31
+ @hub = options[:hub]
32
+ @features = options[:features]
33
+ @connections = options[:connections] || []
34
+ @mote = options[:mote]
35
+ @image = options[:image]
36
+ @active_project = options[:active_project] || false
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ module Snafu
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "snafu/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "snafu"
7
+ s.version = Snafu::VERSION
8
+ s.authors = ["Jeff Browning"]
9
+ s.license = 'MIT'
10
+ s.email = ["jeff@jkbrowning.com"]
11
+ s.homepage = "https://github.com/jbrowning/snafu"
12
+ s.summary = %q{A library for the Glitch API}
13
+ s.description = %q{Snafu is an interface to the Glitch API. Glitch is an online
14
+ multi-player game created by Tiny Speck.}
15
+
16
+ s.required_ruby_version = '>= 1.9.2'
17
+
18
+ s.rubyforge_project = "snafu"
19
+
20
+ s.files = `git ls-files`.split("\n")
21
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
22
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
23
+ s.require_paths = ["lib"]
24
+
25
+ s.add_development_dependency 'rspec'
26
+ s.add_development_dependency 'guard-rspec'
27
+ s.add_development_dependency 'vcr'
28
+ s.add_development_dependency 'fakeweb'
29
+ s.add_development_dependency 'rake'
30
+ s.add_runtime_dependency 'httparty', '~> 0.8'
31
+ end
@@ -0,0 +1,39 @@
1
+ ---
2
+ - !ruby/struct:VCR::HTTPInteraction
3
+ request: !ruby/struct:VCR::Request
4
+ method: :get
5
+ uri: http://api.glitch.com:80/simple/locations.getStreets?hub_id=27
6
+ body: !!null
7
+ headers: !!null
8
+ response: !ruby/struct:VCR::Response
9
+ status: !ruby/struct:VCR::ResponseStatus
10
+ code: 200
11
+ message: OK
12
+ headers:
13
+ access-control-allow-credentials:
14
+ - 'false'
15
+ access-control-allow-methods:
16
+ - POST, GET, OPTIONS
17
+ access-control-allow-origin:
18
+ - ! '*'
19
+ access-control-max-age:
20
+ - '1728000'
21
+ cache-control:
22
+ - no-cache="set-cookie"
23
+ content-type:
24
+ - text/plain; charset=utf-8
25
+ date:
26
+ - Thu, 01 Dec 2011 01:34:59 GMT
27
+ server:
28
+ - Apache/2.2.17
29
+ set-cookie:
30
+ - AWSELB=F7715B6B10BD5947A6E9DAE19D6F6DD7D91A271FB744B8EE156EEC745EF275A9089C697163F6DA6B492CB3CBE399C5977DF92D1F3093BD75659503606D3AB37D325E15FFCF;PATH=/;MAX-AGE=600
31
+ content-length:
32
+ - '329'
33
+ connection:
34
+ - Close
35
+ body: ! '{"ok":1,"hub_id":"27","name":"Ix","streets":{"LM416LNIKVLM1":{"name":"Baby
36
+ Steppes"},"LM413SQ6LRN58":{"name":"East Spice"},"LM410QQ0CHARO":{"name":"Flipside"},"LM4115NJ46G8M":{"name":"Groddle
37
+ Ladder"},"LM4109NI2R640":{"name":"Guillermo Gamera Way"},"LM410TMR0S2S1":{"name":"Ruta
38
+ Asuncion"},"LM413RQ6LRG9N":{"name":"West Spice"}}}'
39
+ http_version: '1.1'
@@ -0,0 +1,36 @@
1
+ ---
2
+ - !ruby/struct:VCR::HTTPInteraction
3
+ request: !ruby/struct:VCR::Request
4
+ method: :get
5
+ uri: http://api.glitch.com:80/simple/players.stats?oauth_token=dj0xJmk9Mjg3MzgmYz03NDZmMDYyM2M1ODQ4YmVjMGY2OWZhOTY1YWM0NTZhMw
6
+ body: !!null
7
+ headers: !!null
8
+ response: !ruby/struct:VCR::Response
9
+ status: !ruby/struct:VCR::ResponseStatus
10
+ code: 200
11
+ message: OK
12
+ headers:
13
+ access-control-allow-credentials:
14
+ - 'false'
15
+ access-control-allow-methods:
16
+ - POST, GET, OPTIONS
17
+ access-control-allow-origin:
18
+ - ! '*'
19
+ access-control-max-age:
20
+ - '1728000'
21
+ cache-control:
22
+ - no-cache="set-cookie"
23
+ content-type:
24
+ - text/plain; charset=utf-8
25
+ date:
26
+ - Fri, 02 Dec 2011 02:46:34 GMT
27
+ server:
28
+ - Apache/2.2.17
29
+ set-cookie:
30
+ - AWSELB=F7715B6B10BD5947A6E9DAE19D6F6DD7D91A271FB7C136A73584C8A9C7D99844891B9CE4098DC7B1C7896C3495C0321477EE3FFB6D062BCC604E27B432E4350AD26F2A097D;PATH=/;MAX-AGE=600
31
+ content-length:
32
+ - '113'
33
+ connection:
34
+ - Close
35
+ body: ! '{"ok":1,"level":13,"xp":60,"xp_max":7032,"currants":2541,"energy":298,"energy_max":300,"mood":295,"mood_max":300}'
36
+ http_version: '1.1'