Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit fc1615cd05854cc3f2f09e6ed7f628f832eba3be @jbrowning committed Nov 27, 2011
Showing with 245 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +4 −0 Gemfile
  3. +10 −0 Guardfile
  4. +7 −0 LICENSE
  5. +3 −0 README.textile
  6. +6 −0 Rakefile
  7. +6 −0 lib/snafu.rb
  8. +50 −0 lib/snafu/client.rb
  9. +49 −0 lib/snafu/locations.rb
  10. +3 −0 lib/snafu/version.rb
  11. +27 −0 snafu.gemspec
  12. +25 −0 spec/snafu/client_spec.rb
  13. +50 −0 spec/snafu/locations_spec.rb
  14. +1 −0 spec/spec_helper.rb
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in snafu.gemspec
+gemspec
@@ -0,0 +1,10 @@
+# A sample Guardfile
+# More info at https://github.com/guard/guard#readme
+
+guard 'rspec', :version => 2 do
+ watch(%r{^spec/.+_spec\.rb$})
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
+ watch(%r{^lib/snafu/(.+)\.rb$}) { |m| "spec/snafu/#{m[1]}_spec.rb" }
+ watch('spec/spec_helper.rb') { "spec" }
+end
+
@@ -0,0 +1,7 @@
+Copyright (c) 2011 Jeff Browning
+
+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:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+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,3 @@
+h1. Snafu
+
+Snafu is a Ruby implementation of the "Glitch API":http://api.glitch.com. "Glitch":http://glitch.com is a online multiplayer game created by Tiny Speck.
@@ -0,0 +1,6 @@
+require "bundler/gem_tasks"
+require 'rspec/core/rake_task'
+
+RSpec::Core::RakeTask.new(:spec)
+
+task :default => :spec
@@ -0,0 +1,6 @@
+require "snafu/version"
+require "snafu/locations"
+require "snafu/client"
+
+module Snafu
+end
@@ -0,0 +1,50 @@
+require 'httparty'
+
+module Snafu
+ API_URL = 'http://api.glitch.com/simple'
+
+ def self.new(api_key="")
+ if api_key.is_a?(String) && !api_key.empty?
+ Snafu::Client.new(api_key)
+ else
+ raise ArgumentError.new("API key is required")
+ end
+ end
+
+ class Client
+ include HTTParty
+ base_uri API_URL
+
+ # include other classes & modules so they can be called on the returned client object
+ include Snafu::Locations
+
+ attr_accessor :api_key
+
+ def initialize(api_key)
+ @api_key = api_key
+ end
+
+ def call(method, query_parameters={})
+ if method.is_a? String
+ options = { :format => :json }
+ unless query_parameters.empty?
+ options[:query] = query_parameters
+ end
+ request_uri = "/#{method}"
+ parse_response(self.class.get(request_uri, options))
+ else
+ raise ArgumentError.new("Method argument must be a string")
+ end
+ end
+
+ def parse_response(response)
+ if response["ok"] == 0
+ raise GlitchAPIError.new(response["error"])
+ else
+ response
+ end
+ end
+ end
+
+ class GlitchAPIError < StandardError;end
+end
@@ -0,0 +1,49 @@
+module Snafu
+ module Locations
+ def get_hubs
+ hubs = []
+ response = self.call("locations.getHubs")
+ response["hubs"].each do |key,value|
+ hubs << {:id => key, :name => value["name"]}
+ end
+ return hubs
+ end
+
+ def get_hub(hub_id)
+ response = self.call("locations.getStreets", :hub_id => hub_id)
+ hub = Hub.new(:id => response["hub_id"], :name => response["name"])
+ streets = []
+ response["streets"].each do |k,v|
+ streets << {:id => k, :name => v[:name]}
+ end
+ hub.streets = streets
+ return hub
+ end
+
+ # Models
+
+ class Hub
+ attr_accessor :id, :name, :streets
+ def initialize(options = {})
+ @id = options[:id]
+ @name = options[:name]
+ @streets = options[:streets]
+ end
+
+ def to_s
+ "#{@id} - #{@name}"
+ end
+ end
+
+ class Location
+ attr_accessor :id, :name, :hub, :connections
+ def initialize(options = {})
+ if options
+ @id = options[:id]
+ @name = options[:name]
+ @hub = options[:name]
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,3 @@
+module Snafu
+ VERSION = "0.0.1alpha"
+end
@@ -0,0 +1,27 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "snafu/version"
+
+Gem::Specification.new do |s|
+ s.name = "snafu"
+ s.version = Snafu::VERSION
+ s.authors = ["Jeff Browning"]
+ s.email = ["jeff@jkbrowning.com"]
+ s.homepage = "http://jbrowning.org"
+ s.summary = %q{A Ruby library for the Glitch API}
+ s.description = %q{This gem provides a convenient way to consume the Glitch
+ API located at http://api.glitch.com}
+
+ s.rubyforge_project = "snafu"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+
+ # specify any dependencies here; for example:
+ s.add_development_dependency 'rspec'
+ s.add_development_dependency 'guard-rspec'
+ s.add_runtime_dependency 'json'
+ s.add_runtime_dependency 'httparty'
+end
@@ -0,0 +1,25 @@
+require 'spec_helper'
+
+describe Snafu::Client do
+ before(:each) do
+ @snafu = Snafu.new("testkey")
+ end
+
+ describe 'call' do
+ it 'should receive a successful response from the Glitch API if given an valid method' do
+ result = @snafu.call("calendar.getHolidays")
+ result['ok'].should eql(1)
+ end
+
+ it 'should raise a GlitchAPIError if receiving an unsucessful response from the Glitch API' do
+ expect { @snafu.call("badmethod") }.to raise_error(Snafu::GlitchAPIError)
+ end
+
+ it 'should accept a hash of POST arguments' do
+ hub_id = "27"
+ hub_name = "Ix"
+ result = @snafu.call("locations.getStreets", {:hub_id => "27"})
+ result["name"].should eql(hub_name)
+ end
+ end
+end
@@ -0,0 +1,50 @@
+require 'spec_helper'
+
+describe Snafu::Locations do
+ before(:each) do
+ @snafu = Snafu.new("test-key")
+ @test_hub_id = 27
+ @test_hub_name = "Ix"
+ end
+ context "get_hubs" do
+ it "should return an array of all hubs" do
+ hubs = @snafu.get_hubs
+ hubs.length.should be > 0
+ end
+
+ it "should return valid hub names and IDs" do
+ hubs = @snafu.get_hubs
+ first_hub = hubs.first
+ first_hub[:id].should_not be_nil
+ first_hub[:name].should_not be_nil
+ end
+ end
+
+ context "get_hub" do
+
+ it "should return a hub if given a valid Hub ID" do
+ hub = @snafu.get_hub(@test_hub_id)
+ hub.name.should eql(@test_hub_name)
+ end
+
+ it "should return an array of all locations if given a valid Hub object" do
+ hub = Hub.new(:id => @test_hub_id, :name => @test_hub_name)
+ hub
+ end
+ end
+
+ describe Snafu::Locations::Hub do
+ Hub = Snafu::Locations::Hub
+ it "should retain the ID & Name of the hub if given a valid " do
+ id = "27"
+ name = "Ix"
+ hub = Hub.new(:id => id, :name => name)
+ hub.id.should eql(id)
+ hub.name.should eql(name)
+ end
+ end
+
+ describe Snafu::Locations::Location do
+ Location = Snafu::Locations::Location
+ end
+end
@@ -0,0 +1 @@
+require 'snafu'

0 comments on commit fc1615c

Please sign in to comment.