Permalink
Browse files

Initial commit, version 0.0.1

  • Loading branch information...
0 parents commit ff790afb6fa770183540507a4f2521a20b9ff137 @lucashungaro committed Sep 10, 2011
@@ -0,0 +1,5 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
+vendor
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in giant_bomb.gemspec
+gemspec
@@ -0,0 +1,20 @@
+Copyright (c) 2011 Lucas Húngaro
+
+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,88 @@
+h1. giant_bomb
+
+A simple wrapper for the awesome GiantBomb.com API.
+
+Does giant_bomb helps your daily work with Ruby? So, "please recommend me in Work With Rails":http://workingwithrails.com/recommendation/new/person/9370-lucas-h-ngaro and thanks for your kindness! :)
+
+h2. How?
+
+First, install the gem:
+
+<pre>
+$ [sudo] gem install giant_bomb
+</pre>
+
+Then, add it as a dependency of your code using your favorite way (a simple require or mechanisms like the Bundler gem).
+
+Using it is really simple.
+
+First, you need an API key. Then, configure giant_bomb to use it:
+
+<pre>
+GiantBomb::Configuration.api_key = "my api key"
+</pre>
+
+Now to the real data consumption. There are two kinds of entities: a Resource (single entity) and a ResourceList (list of entities).
+
+An example of Resource is a Game. To get data about a particular game you can do the following:
+
+<pre>
+>> GiantBomb::Game.details(17280)
+=> <#Hashie::Mash name="BioShock" ...>
+</pre>
+
+An example of a ResourceList is a GameList. To get a list of games you can do the following:
+
+<pre>
+>> GiantBomb::GameList.fetch
+=> [<#Hashie::Mash aliases="BioShock" ...>, <#Hashie::Mash aliases="Civilization V" ...>, ...]
+</pre>
+
+As you can see, the gem makes use of "Hashie's":https://github.com/intridea/hashie Mash, which basically is a Hash with attribute accessors for all keys, meaning you can use the dot notation to access values:
+
+<pre>
+>> game = GiantBomb::Game.details(17280)
+=> <#Hashie::Mash name="BioShock" ...>
+>> game.name
+=> "BioShock"
+</pre>
+
+For a Resource, the return will always be a Mash instance. For a ResourceList, it will be an Array of Mash instances.
+
+h3. Available endpoints
+
+Resources:
+
+* Company
+* Game
+* Genre
+* Platform
+* Theme
+
+Resource lists:
+
+* Companies
+* Games
+* Genres
+* Platforms
+* Themes
+
+h3. Additional configuration
+
+Besides the API key, the Configuration class allows you to configure an adapter to make HTTP requests (you can use the included "CurbAdapter":https://github.com/lucashungaro/giant_bomb/blob/master/lib/curb_adapter.rb. and its specs as an example of implementation) and a logger (STDOUT by default).
+
+h2. Note on Patches/Pull Requests
+
+* Fork the project.
+* Make your feature addition or bug fix.
+* Add tests for it. This is important so I don't break it in a
+ future version unintentionally.
+* Commit, do not mess with rakefile, version, or history.
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
+* Send me a pull request. Bonus points for topic branches.
+
+
+h3. License
+
+sentinel is released under the MIT license. See "MIT LICENSE":https://github.com/lucashungaro/giant_bomb/blob/master/MIT-LICENSE.
+
@@ -0,0 +1 @@
+require "bundler/gem_tasks"
@@ -0,0 +1,27 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "giant_bomb/version"
+
+Gem::Specification.new do |s|
+ s.name = "giant_bomb"
+ s.version = GiantBomb::VERSION
+ s.authors = ["Lucas Húngaro"]
+ s.email = ["lucashungaro@gmail.com"]
+ s.homepage = ""
+ s.summary = %q{A simple wrapper for the awesome GiantBomb.com API}
+ s.description = %q{A simple wrapper for the awesome GiantBomb.com API}
+
+ s.rubyforge_project = "giant_bomb"
+
+ 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"]
+
+ s.add_development_dependency "rspec"
+ s.add_development_dependency "webmock"
+
+ s.add_runtime_dependency "curb"
+ s.add_runtime_dependency "hashie"
+ s.add_runtime_dependency "yajl-ruby"
+end
@@ -0,0 +1,26 @@
+# -*- encoding: utf-8 -*-
+require "giant_bomb/version"
+
+require "giant_bomb/curb_adapter"
+require "giant_bomb/errors"
+require "giant_bomb/request"
+require "giant_bomb/response_handler"
+require "giant_bomb/resource"
+require "giant_bomb/resource_list"
+
+require "giant_bomb/company"
+require "giant_bomb/company_list"
+require "giant_bomb/configuration"
+require "giant_bomb/game"
+require "giant_bomb/game_list"
+require "giant_bomb/genre"
+require "giant_bomb/genre_list"
+require "giant_bomb/platform"
+require "giant_bomb/platform_list"
+require "giant_bomb/theme"
+require "giant_bomb/theme_list"
+
+require "curb"
+require "hashie"
+require "logger"
+require "yajl"
@@ -0,0 +1,8 @@
+# -*- encoding: utf-8 -*-
+module GiantBomb
+ class Company
+ extend Resource
+
+ @resource = "company"
+ end
+end
@@ -0,0 +1,8 @@
+# -*- encoding: utf-8 -*-
+module GiantBomb
+ class CompanyList
+ extend ResourceList
+
+ @resource = "companies"
+ end
+end
@@ -0,0 +1,15 @@
+# -*- encoding: utf-8 -*-
+require "logger"
+
+module GiantBomb
+ class Configuration
+ BASE_URL = "http://api.giantbomb.com"
+
+ @http_library_adapter = CurbAdapter
+ @logger = Logger.new(STDOUT)
+
+ class << self
+ attr_accessor :api_key, :http_library_adapter, :logger
+ end
+ end
+end
@@ -0,0 +1,18 @@
+# -*- encoding : utf-8 -*-
+require "ostruct"
+
+module GiantBomb
+ class CurbAdapter
+ def self.http_get(url, headers = {})
+ begin
+ response = Curl::Easy.http_get(url) {|config| config.headers = headers }
+
+ if response
+ OpenStruct.new(:body => response.body_str, :code => response.response_code, :headers => response.headers)
+ end
+ rescue StandardError => e
+ raise Errors::WebServiceNotAvailableError, e.message
+ end
+ end
+ end
+end
@@ -0,0 +1,9 @@
+# -*- encoding: utf-8 -*-
+module GiantBomb
+ module Errors
+ class InvalidRequestError < StandardError; end
+ class RequestFailedError < StandardError; end
+ class UnrecognizedResponseError < StandardError; end
+ class WebServiceNotAvailableError < StandardError; end
+ end
+end
@@ -0,0 +1,8 @@
+# -*- encoding: utf-8 -*-
+module GiantBomb
+ class Game
+ extend Resource
+
+ @resource = "game"
+ end
+end
@@ -0,0 +1,17 @@
+# -*- encoding: utf-8 -*-
+module GiantBomb
+ class GameList
+ extend ResourceList
+
+ @resource = "games"
+
+ class << self
+ attr_accessor :platforms
+ end
+
+ def self.include_additional_parameters
+ self.platforms ||= []
+ params[:platforms] = platforms.join(",") unless platforms.empty?
+ end
+ end
+end
@@ -0,0 +1,8 @@
+# -*- encoding: utf-8 -*-
+module GiantBomb
+ class Genre
+ extend Resource
+
+ @resource = "genre"
+ end
+end
@@ -0,0 +1,8 @@
+# -*- encoding: utf-8 -*-
+module GiantBomb
+ class GenreList
+ extend ResourceList
+
+ @resource = "genres"
+ end
+end
@@ -0,0 +1,8 @@
+# -*- encoding: utf-8 -*-
+module GiantBomb
+ class Platform
+ extend Resource
+
+ @resource = "platform"
+ end
+end
@@ -0,0 +1,8 @@
+# -*- encoding: utf-8 -*-
+module GiantBomb
+ class PlatformList
+ extend ResourceList
+
+ @resource = "platforms"
+ end
+end
@@ -0,0 +1,43 @@
+# -*- encoding: utf-8 -*-
+module GiantBomb
+ class Request
+ def initialize(resource, params = {})
+ self.request_url = "#{Configuration::BASE_URL}/#{resource}/"
+ self.request_params = build_params(params)
+ self.logger = Configuration.logger
+ self.http_client = Configuration.http_library_adapter
+ end
+
+ def get(response_handler = ResponseHandler.new)
+ url = build_url
+ logger.info("GET on #{url}")
+
+ response = http_client.http_get(url)
+ response_handler.process(response)
+ end
+
+ private
+ attr_accessor :http_client, :logger, :request_url, :request_params
+
+ def build_params(params)
+ building_params = {:format => "json", :api_key => Configuration.api_key}
+
+ if limit = params.delete(:per_page)
+ building_params[:limit] = limit
+ building_params[:offset] = (params.delete(:page_number) - 1) * limit
+ end
+
+ building_params.merge!(params)
+ building_params
+ end
+
+ def build_url
+ params = request_params.inject([]) do |memo, param_pair|
+ memo << "#{param_pair[0]}=#{param_pair[1]}"
+ end
+
+ building_url = request_url.dup
+ building_url << "?#{params.join('&')}"
+ end
+ end
+end
@@ -0,0 +1,12 @@
+# -*- encoding: utf-8 -*-
+module GiantBomb
+ module Resource
+ def details(resource_id)
+ request = Request.new("#{resource}/#{resource_id}")
+ request.get
+ end
+
+ private
+ attr_accessor :resource
+ end
+end
@@ -0,0 +1,15 @@
+# -*- encoding: utf-8 -*-
+module GiantBomb
+ module ResourceList
+ def fetch(page_number = 1, per_page = 100)
+ self.params = {:page_number => page_number, :per_page => per_page}
+ include_additional_parameters
+
+ request = Request.new(resource, params)
+ request.get
+ end
+
+ private
+ attr_accessor :params, :resource
+ end
+end
Oops, something went wrong.

0 comments on commit ff790af

Please sign in to comment.