rr_games_radar 1.0.2

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 +17 -0
  2. data/Gemfile +5 -0
  3. data/LICENSE.txt +22 -0
  4. data/README.md +104 -0
  5. data/Rakefile +9 -0
  6. data/games_radar_api.gemspec +28 -0
  7. data/lib/faraday/raise_http_exception.rb +51 -0
  8. data/lib/games_radar_api/api.rb +22 -0
  9. data/lib/games_radar_api/client/cheats.rb +18 -0
  10. data/lib/games_radar_api/client/developers.rb +9 -0
  11. data/lib/games_radar_api/client/franchises.rb +9 -0
  12. data/lib/games_radar_api/client/games.rb +61 -0
  13. data/lib/games_radar_api/client/genres.rb +9 -0
  14. data/lib/games_radar_api/client/guides.rb +18 -0
  15. data/lib/games_radar_api/client/news.rb +44 -0
  16. data/lib/games_radar_api/client/platforms.rb +9 -0
  17. data/lib/games_radar_api/client/publishers.rb +9 -0
  18. data/lib/games_radar_api/client/screenshots.rb +20 -0
  19. data/lib/games_radar_api/client/videos.rb +18 -0
  20. data/lib/games_radar_api/client.rb +18 -0
  21. data/lib/games_radar_api/configuration.rb +51 -0
  22. data/lib/games_radar_api/connection.rb +32 -0
  23. data/lib/games_radar_api/error.rb +19 -0
  24. data/lib/games_radar_api/request.rb +42 -0
  25. data/lib/games_radar_api/version.rb +3 -0
  26. data/lib/games_radar_api.rb +26 -0
  27. data/spec/lib/games_radar_api/client/cheats_spec.rb +33 -0
  28. data/spec/lib/games_radar_api/client/developers_spec.rb +22 -0
  29. data/spec/lib/games_radar_api/client/franchises_spec.rb +22 -0
  30. data/spec/lib/games_radar_api/client/games_spec.rb +68 -0
  31. data/spec/lib/games_radar_api/client/genres_spec.rb +21 -0
  32. data/spec/lib/games_radar_api/client/guides_spec.rb +31 -0
  33. data/spec/lib/games_radar_api/client/news_spec.rb +60 -0
  34. data/spec/lib/games_radar_api/client/platforms_spec.rb +22 -0
  35. data/spec/lib/games_radar_api/client/publishers_spec.rb +22 -0
  36. data/spec/lib/games_radar_api/client/screenshots_spec.rb +29 -0
  37. data/spec/lib/games_radar_api/client/videos_spec.rb +24 -0
  38. data/spec/lib/games_radar_api/client_spec.rb +59 -0
  39. data/spec/lib/games_radar_api/configuration_spec.rb +26 -0
  40. data/spec/lib/games_radar_api/games_radar_api_spec.rb +7 -0
  41. data/spec/spec_helper.rb +15 -0
  42. metadata +203 -0
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'pry'
4
+ # Specify your gem's dependencies in games_radar_api.gemspec
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Ash
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,104 @@
1
+ # GamesRadarApi
2
+
3
+ This gem is a fully featured API wrapper for the Games Radar API.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'games_radar_api'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install games_radar_api
18
+
19
+ ## Configuration
20
+
21
+ You can use a block configuration to specify the API key. e.g.
22
+
23
+ GamesRadarApi.configure do |config|
24
+ config.api_key = "YOUR_CLIENT_ID"
25
+ end
26
+
27
+
28
+ ## Usage
29
+
30
+ To start using the GamesRadarAPI just initialize the client and start requesting. e.g.
31
+
32
+ require 'games_radar_api'
33
+
34
+ client = GamesRadarApi.client(:api_key=>'<YOUR_API_KEY>')
35
+ # search for games called Darksiders on the XBOX 360
36
+ response = client.game_search('Darksiders','xbox360')
37
+
38
+ The following methods are available to you:
39
+
40
+ games(options={:platform=>'all',:genre=>'all',:page_num=>1,:page_size=>10,:sort=>'newest'})
41
+
42
+ game(id)
43
+
44
+ game_search(query,platform,options={:region=>'us'})
45
+
46
+ game_cheats(id,options={:region=>'us',:page_num=>1,:page_size=>10,:content=>'all_cheat'})
47
+
48
+ game_guides(id,options={:region=>'us',:page_num=>1,:page_size=>10})
49
+
50
+ game_news(id,options={:region=>'us',:page_num=>1,:page_size=>10})
51
+
52
+ game_features(id,options={:region=>'us',:page_num=>1,:page_size=>10})
53
+
54
+ game_previews(id,options={:region=>'us',:page_num=>1,:page_size=>10})
55
+
56
+ game_reviews(id,options={:region=>'us',:page_num=>1,:page_size=>10})
57
+
58
+ game_screenshots(id,options={:region=>'us',:page_num=>1,:page_size=>10})
59
+
60
+ game_videos(id,options={:region=>'us',:page_num=>1,:page_size=>10})
61
+
62
+ cheats(options={:region=>'us',:platform=>'all',:genre=>'all',:game_name=>'',:page_num=>1,:page_size=>10,:sort=>'newest'})
63
+
64
+ developers()
65
+
66
+ franchises()
67
+
68
+ genres()
69
+
70
+ guides(options={:region=>'us',:platform=>'all',:genre=>'all',:game_name=>'',:page_num=>1,:page_size=>10,:sort=>'newest'})
71
+
72
+ news(options={:region=>'us',:platform=>'all',:genre=>'all',:game_name=>'',:page_num=>1,:page_size=>10,:sort=>'newest'})
73
+
74
+ features(options={:region=>'us',:platform=>'all',:genre=>'all',:game_name=>'',:page_num=>1,:page_size=>10,:sort=>'newest'})
75
+
76
+ previews(options={:region=>'us',:platform=>'all',:genre=>'all',:game_name=>'',:page_num=>1,:page_size=>10,:sort=>'newest'})
77
+
78
+ reviews(options={:region=>'us',:platform=>'all',:genre=>'all',:game_name=>'',:page_num=>1,:page_size=>10,:sort=>'newest'})
79
+
80
+ platforms()
81
+
82
+ publishers()
83
+
84
+ screenshots(options={:region=>'us',:platform=>'all',:genre=>'all',:game_name=>'',:unique_game=>false,:page_num=>1,:page_size=>10,:sort=>'newest'})
85
+
86
+ videos(options={:region=>'us',:platform=>'all',:genre=>'all',:game_name=>'',:page_num=>1,:page_size=>10,:sort=>'newest'})
87
+
88
+ Each request returns an array of Hashie Mash objects which you can then manipulate in any way that you wish.
89
+
90
+ ### NOTE
91
+
92
+ Please note that during testing I found that elements of the Games Radar API don't work as expected. Here's a few of the issues that I found:
93
+
94
+ 1. news - this method returns total_rows but the not the articles themselves
95
+ 1. game_features, game_previews, game_reviews and game_news - returns total_rows but not the articles
96
+ 1. game - this method fetches information about the game, but I'm yet to find a game with a thumbnail image, and the empty_logo image they send returns a 404
97
+ 1. videos and game_videos - whilst the API call works, visiting the video URL in the URL attribute always goes to a 404 page
98
+ 1. screenshots and game_screenshots - no data is returned
99
+
100
+ It's also recommended that you look through the Games Radar API Documentation for a clearer idea of the parameters values which each method accepts http://dl.gamesradar.com/api/GamesRadar_Developer_API_Documentation.pdf
101
+
102
+ ## Credits
103
+
104
+ This gem is created by Cube Websites
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |test|
5
+ test.test_files = FileList['spec/lib/games_radar_api/*_spec.rb','spec/lib/games_radar_api/client/*_spec.rb']
6
+ test.verbose = true
7
+ end
8
+
9
+ task :default => :test
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'games_radar_api/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "rr_games_radar"
8
+ gem.version = GamesRadarApi::VERSION
9
+ gem.authors = ["Rodrigo Martins"]
10
+ gem.email = ["rodrigo@rrmartins.com"]
11
+ gem.description = "Gem for interaction with the API of GamesRadar."
12
+ gem.summary = "This gem is for interaction with the API of GamesRadar."
13
+ gem.homepage = "https://github.com/rrmartins/games_radar_api"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_runtime_dependency('faraday_middleware', '~> 0.8')
21
+ gem.add_runtime_dependency('faraday', ['>= 0.7', '< 0.9'])
22
+ gem.add_runtime_dependency('multi_xml', '>= 0.5.1')
23
+ gem.add_runtime_dependency('hashie', '>= 0.4.0')
24
+
25
+ gem.add_development_dependency('turn', '~> 0.9.6')
26
+ gem.add_development_dependency('rake', '~> 0.9.2.2')
27
+
28
+ end
@@ -0,0 +1,51 @@
1
+ require 'faraday'
2
+
3
+ # @private
4
+ module FaradayMiddleware
5
+ # @private
6
+ class RaiseHttpException < Faraday::Middleware
7
+ def call(env)
8
+ @app.call(env).on_complete do |response|
9
+ case response[:status].to_i
10
+ when 400
11
+ raise GamesRadarApi::BadRequest, error_message_400(response)
12
+ when 404
13
+ raise GamesRadarApi::NotFound, error_message_400(response)
14
+ when 500
15
+ raise GamesRadarApi::InternalServerError, error_message_500(response, "Something is technically wrong.")
16
+ when 503
17
+ end
18
+ end
19
+ end
20
+
21
+
22
+ def initialize(app)
23
+ super app
24
+ @parser = nil
25
+ end
26
+
27
+ private
28
+
29
+ def error_message_400(response)
30
+ "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:status]}#{error_body(response[:body])}"
31
+ end
32
+
33
+ def error_body(body)
34
+ # body gets passed as a string, not sure if it is passed as something else from other spots?
35
+ if not body.nil? and not body.empty? and body.kind_of?(String)
36
+ # removed multi_json thanks to wesnolte's commit
37
+ body = MultiXml.parse(body)
38
+ end
39
+
40
+ if body.nil?
41
+ nil
42
+ elsif body['error'] and body['error']['messages'] and body['error']['messages']['message'] and not body['error']['messages']['message'].empty?
43
+ ": #{body['error']['messages']['message']}"
44
+ end
45
+ end
46
+
47
+ def error_message_500(response, body=nil)
48
+ "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{[response[:status].to_s + ':', body].compact.join(' ')}"
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,22 @@
1
+ require File.expand_path('../connection', __FILE__)
2
+ require File.expand_path('../request', __FILE__)
3
+ require File.expand_path('../configuration', __FILE__)
4
+
5
+ module GamesRadarApi
6
+ # @private
7
+ class API
8
+ # @private
9
+ attr_accessor *Configuration::VALID_CONFIG_KEYS + [:total_rows]
10
+
11
+ # Creates a new API
12
+ def initialize(options={})
13
+ options = GamesRadarApi.options.merge(options)
14
+ Configuration::VALID_CONFIG_KEYS.each do |key|
15
+ send("#{key}=", options[key])
16
+ end
17
+ end
18
+
19
+ include Connection
20
+ include Request
21
+ end
22
+ end
@@ -0,0 +1,18 @@
1
+ module GamesRadarApi
2
+ class Client
3
+ module Cheats
4
+ def cheats(options={:region=>'us',:platform=>'all',:genre=>'all',:game_name=>'',:page_num=>1,:page_size=>10,:sort=>'newest'})
5
+ response = get('/cheats',options)
6
+ self.total_rows = response.cheats.total_rows.to_i
7
+ response.cheats.cheat
8
+ end
9
+
10
+ def game_cheats(id,options={:region=>'us',:page_num=>1,:page_size=>10,:content=>'all_cheat'})
11
+ response = get("/game/cheats/#{id}",options)
12
+ self.total_rows = response.cheats.total_rows.to_i
13
+ response.cheats.cheat
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,9 @@
1
+ module GamesRadarApi
2
+ class Client
3
+ module Developers
4
+ def developers
5
+ get('/developers').developers.company
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module GamesRadarApi
2
+ class Client
3
+ module Franchises
4
+ def franchises
5
+ get('/franchises').franchises.franchise
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,61 @@
1
+ module GamesRadarApi
2
+ class Client
3
+ module Games
4
+ def games(options={:platform=>'all',:genre=>'all',:page_num=>1,:page_size=>10,:sort=>'newest'})
5
+ response = get('/games',options)
6
+ self.total_rows = response.games.total_rows.to_i
7
+ unless response.game.nil? or response.game.id.nil?
8
+ if response.game.name.include?("us")
9
+ response.game.name = response.game.name.us
10
+ end
11
+ end
12
+ response.games.game
13
+ end
14
+
15
+ def game(id)
16
+ response = get("/game/#{id}")
17
+ self.total_rows = response.total_rows.to_i
18
+ unless response.game.nil? or response.game.id.nil?
19
+ if response.game.name.nil?
20
+ response.game.name = nil
21
+ elsif response.game.name.include?("us")
22
+ response.game.name = response.game.name.us
23
+ end
24
+
25
+ if response.game.release_date.nil?
26
+ response.game[:release_date] = nil
27
+ end
28
+
29
+ if response.game.platform.nil?
30
+ response.game[:platform] = nil
31
+ elsif response.game.platform.include?("name")
32
+ response.game[:platform] = response.game.platform.name
33
+ end
34
+
35
+ if response.game.genre.nil?
36
+ response.game[:genre] = nil
37
+ elsif response.game.genre.include?("name")
38
+ response.game[:genre] = response.game.genre.name
39
+ end
40
+
41
+ if response.game.publishers.nil?
42
+ response.game[:publishers] = nil
43
+ elsif response.game.publishers.include?("us")
44
+ response.game[:publishers] = response.game.publishers.us
45
+ end
46
+
47
+ end
48
+
49
+ return response.game unless response.game.nil? or response.game.id.nil?
50
+ return nil
51
+ end
52
+
53
+ def game_search(query,platform,options={:region=>'us'})
54
+ response = get("/search/gameName/#{platform}/#{query}",options)
55
+ self.total_rows = response.games.total_rows.to_i
56
+ response.games.game
57
+ end
58
+
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,9 @@
1
+ module GamesRadarApi
2
+ class Client
3
+ module Genres
4
+ def genres
5
+ get('/genres').genres.genre
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,18 @@
1
+ module GamesRadarApi
2
+ class Client
3
+ module Guides
4
+ def guides(options={:region=>'us',:platform=>'all',:genre=>'all',:game_name=>'',:page_num=>1,:page_size=>10,:sort=>'newest'})
5
+ response = get('/guidesandfaqs',options)
6
+ self.total_rows = response.cheats.total_rows.to_i
7
+ response.cheats.cheat
8
+ end
9
+
10
+ def game_guides(id,options={:region=>'us',:page_num=>1,:page_size=>10})
11
+ response = get("/game/guidesandfaqs/#{id}",options)
12
+ self.total_rows = response.cheats.total_rows.to_i
13
+ response.cheats.cheat
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,44 @@
1
+ module GamesRadarApi
2
+ class Client
3
+ module News
4
+ puts "NOTE: Tested the News API on 26 September 2012 and it wasn't returning any articles, just a number of rows"
5
+ puts "This module will therefore appear in this gem, but please don't be suprised if it doesn't work"
6
+ def news(options={:region=>'us',:platform=>'all',:genre=>'all',:game_name=>'',:page_num=>1,:page_size=>10,:sort=>'newest'})
7
+ response = get('/news',options)
8
+ self.total_rows = response.articles.total_rows.to_i
9
+ response.articles.article
10
+ end
11
+
12
+ def features(options={:region=>'us',:platform=>'all',:genre=>'all',:game_name=>'',:page_num=>1,:page_size=>10,:sort=>'newest'})
13
+ response = get('/features',options)
14
+ self.total_rows = response.articles.total_rows.to_i
15
+ response.articles.article
16
+ end
17
+
18
+ def game_news(id,options={:region=>'us',:page_num=>1,:page_size=>10})
19
+ response = get("/game/news/#{id}",options)
20
+ self.total_rows = response.articles.total_rows.to_i
21
+ response.articles.article
22
+ end
23
+
24
+ def game_features(id,options={:region=>'us',:page_num=>1,:page_size=>10})
25
+ response = get("/game/features/#{id}",options)
26
+ self.total_rows = response.articles.total_rows.to_i
27
+ response.articles.article
28
+ end
29
+
30
+ def game_previews(id,options={:region=>'us',:page_num=>1,:page_size=>10})
31
+ response = get("/game/previews/#{id}",options)
32
+ self.total_rows = response.articles.total_rows.to_i
33
+ response.articles.article
34
+ end
35
+
36
+ def game_reviews(id,options={:region=>'us',:page_num=>1,:page_size=>10})
37
+ response = get("/game/reviews/#{id}",options)
38
+ self.total_rows = response.articles.total_rows.to_i
39
+ response.articles.article
40
+ end
41
+
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,9 @@
1
+ module GamesRadarApi
2
+ class Client
3
+ module Platforms
4
+ def platforms
5
+ get('/platforms').platforms.platform
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module GamesRadarApi
2
+ class Client
3
+ module Publishers
4
+ def publishers
5
+ get('/publishers').publishers.company
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,20 @@
1
+ module GamesRadarApi
2
+ class Client
3
+ module Screenshots
4
+ def screenshots(options={:region=>'us',:platform=>'all',:genre=>'all',:game_name=>'',:unique_game=>false,:page_num=>1,:page_size=>10,:sort=>'newest'})
5
+ puts "NOTE: Tested the Screenshot API on 26 September 2012 and it wasn't returning any rows"
6
+ puts "This module will therefore appear in this gem, but please don't be suprised if it doesn't work"
7
+ response = get('/screenshots',options)
8
+ self.total_rows = response.screenshots.total_rows.to_i
9
+ response.screenshots.screenshot
10
+ end
11
+
12
+ def game_screenshots(id,options={:region=>'us',:page_num=>1,:page_size=>10})
13
+ response = get("/game/screenshots/#{id}",options)
14
+ self.total_rows = response.screenshots.total_rows.to_i
15
+ response.screenshots.screenshot
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,18 @@
1
+ module GamesRadarApi
2
+ class Client
3
+ module Videos
4
+ def videos(options={:region=>'us',:platform=>'all',:genre=>'all',:game_name=>'',:page_num=>1,:page_size=>10,:sort=>'newest'})
5
+ response = get('/videos',options)
6
+ self.total_rows = response.videos.total_rows.to_i
7
+ response.videos.video
8
+ end
9
+
10
+ def game_videos(id,options={:region=>'us',:page_num=>1,:page_size=>10})
11
+ response = get("/game/videos/#{id}",options)
12
+ self.total_rows = response.videos.total_rows.to_i
13
+ response.videos.video
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module GamesRadarApi
2
+ class Client < API
3
+ Dir[File.expand_path('../client/*.rb',__FILE__)].each{ |f| require f }
4
+
5
+ include GamesRadarApi::Client::Cheats
6
+ include GamesRadarApi::Client::Developers
7
+ include GamesRadarApi::Client::Franchises
8
+ include GamesRadarApi::Client::Games
9
+ include GamesRadarApi::Client::Genres
10
+ include GamesRadarApi::Client::Guides
11
+ include GamesRadarApi::Client::News
12
+ include GamesRadarApi::Client::Platforms
13
+ include GamesRadarApi::Client::Publishers
14
+ include GamesRadarApi::Client::Screenshots
15
+ include GamesRadarApi::Client::Videos
16
+
17
+ end # Client
18
+ end
@@ -0,0 +1,51 @@
1
+ require 'faraday'
2
+ require File.expand_path('../version', __FILE__)
3
+
4
+ module GamesRadarApi
5
+ module Configuration
6
+ VALID_CONNECTION_KEYS = [:adapter,:endpoint,:user_agent,:method,:proxy].freeze
7
+ VALID_OPTIONS_KEYS = [:api_key,:format].freeze
8
+ VALID_CONFIG_KEYS = VALID_CONNECTION_KEYS + VALID_OPTIONS_KEYS
9
+
10
+ DEFAULT_ADAPTER = Faraday.default_adapter.freeze
11
+ DEFAULT_ENDPOINT = 'http://api.gamesradar.com'.freeze
12
+ DEFAULT_METHOD = 'get'.freeze
13
+ DEFAULT_USER_AGENT = "Games Radar API Gem #{GamesRadarApi::VERSION}".freeze
14
+
15
+ DEFAULT_API_KEY = nil
16
+ DEFAULT_FORMAT = :xml
17
+ DEFAULT_PROXY = nil
18
+
19
+ # Build accessor methods for every config options so we can do this, for example:
20
+ # GamesRadarApi.format = :xml
21
+ attr_accessor *VALID_CONFIG_KEYS
22
+
23
+ # Make sure we have the default values set when we get 'extended'
24
+ def self.extended(base)
25
+ base.reset
26
+ end
27
+
28
+ # Convenience method to allow configuration options to be set in a block
29
+ def configure
30
+ yield self
31
+ end
32
+
33
+ # Create a has of options and their values
34
+ def options
35
+ Hash[* VALID_CONFIG_KEYS.map { |key| [key,send(key)] }.flatten ]
36
+ end
37
+
38
+ # Reset all configuration options to defaults
39
+ def reset
40
+ self.adapter = DEFAULT_ADAPTER
41
+ self.endpoint = DEFAULT_ENDPOINT
42
+ self.method = DEFAULT_METHOD
43
+ self.user_agent = DEFAULT_USER_AGENT
44
+
45
+ self.api_key = DEFAULT_API_KEY
46
+ self.format = DEFAULT_FORMAT
47
+ self.proxy = DEFAULT_PROXY
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,32 @@
1
+ require 'faraday_middleware'
2
+ Dir[File.expand_path('../../faraday/*.rb', __FILE__)].each{|f| require f}
3
+
4
+ module GamesRadarApi
5
+ # @private
6
+ module Connection
7
+ private
8
+
9
+ def connection(raw=false)
10
+ options = {
11
+ :headers => {'Accept' => "application/#{format}; charset=utf-8", 'User-Agent' => user_agent},
12
+ :proxy => proxy,
13
+ :ssl => {:verify => false},
14
+ :url => endpoint,
15
+ }
16
+
17
+ Faraday::Connection.new(options) do |connection|
18
+ #connection.use FaradayMiddleware::OAuth2, client_id, access_token
19
+ connection.use Faraday::Request::UrlEncoded
20
+ connection.use FaradayMiddleware::Mashify unless raw
21
+ unless raw
22
+ case format.to_s.downcase
23
+ when 'json' then connection.use Faraday::Response::ParseJson
24
+ when 'xml' then connection.use Faraday::Response::ParseXml
25
+ end
26
+ end
27
+ connection.use FaradayMiddleware::RaiseHttpException
28
+ connection.adapter(adapter)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,19 @@
1
+ module GamesRadarApi
2
+ # Custom error class for rescuing from all Instagram errors
3
+ class Error < StandardError; end
4
+
5
+ # Raised when Instagram returns the HTTP status code 400
6
+ class BadRequest < Error; end
7
+
8
+ # Raised when Instagram returns the HTTP status code 404
9
+ class NotFound < Error; end
10
+
11
+ # Raised when Instagram returns the HTTP status code 500
12
+ class InternalServerError < Error; end
13
+
14
+ # Raised when Instagram returns the HTTP status code 503
15
+ class ServiceUnavailable < Error; end
16
+
17
+ # Raised when a subscription payload hash is invalid
18
+ class InvalidSignature < Error; end
19
+ end
@@ -0,0 +1,42 @@
1
+ module GamesRadarApi
2
+ # Defines HTTP request methods
3
+ module Request
4
+ # Perform an HTTP GET request
5
+ def get(path, options={}, raw=false, unformatted=false)
6
+ request(:get, path, options, raw, unformatted)
7
+ end
8
+
9
+ # Perform an HTTP POST request
10
+ def post(path, options={}, raw=false, unformatted=false)
11
+ request(:post, path, options, raw, unformatted)
12
+ end
13
+
14
+ # Perform an HTTP PUT request
15
+ def put(path, options={}, raw=false, unformatted=false)
16
+ request(:put, path, options, raw, unformatted)
17
+ end
18
+
19
+ # Perform an HTTP DELETE request
20
+ def delete(path, options={}, raw=false, unformatted=false)
21
+ request(:delete, path, options, raw, unformatted)
22
+ end
23
+
24
+ private
25
+
26
+ # Perform an HTTP request
27
+ def request(method, path, options, raw=false, unformatted=false)
28
+ options = options.merge(:api_key=>api_key)
29
+ options.delete_if { |k, v| v.nil? or v.eql?'' }
30
+ response = connection(raw).send(method) do |request|
31
+ case method
32
+ when :get, :delete
33
+ request.url(path, options)
34
+ when :post, :put
35
+ request.path = path
36
+ request.body = options unless options.empty?
37
+ end
38
+ end
39
+ raw ? response : response.body
40
+ end
41
+ end
42
+ end