grepolis-api 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .rvmrc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in grepolis-api.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Dan Matthews
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.
@@ -0,0 +1,29 @@
1
+ # Grepolis::Api
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'grepolis-api'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install grepolis-api
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'grepolis/api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "grepolis-api"
8
+ spec.version = Grepolis::Api::VERSION
9
+ spec.authors = ["Dan Matthews"]
10
+ spec.email = ["dan@bluefoc.us"]
11
+ spec.description = %q{Wraps the data provided by InnoGames for Grepolis worlds}
12
+ spec.summary = %q{}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "debugger"
23
+ spec.add_development_dependency "rake"
24
+
25
+ spec.add_dependency "faraday"
26
+ spec.add_dependency "multi_json"
27
+ spec.add_dependency "yajl-ruby"
28
+ spec.add_dependency "activesupport"
29
+ end
@@ -0,0 +1,18 @@
1
+ require 'uri'
2
+ require "active_support/inflector"
3
+ require "grepolis/api/version"
4
+ require "grepolis/api/connection"
5
+ require "grepolis/api/base"
6
+
7
+ [:alliance_kills_all, :alliance_kills_att, :alliance_kills_def, :alliances, :conquers,
8
+ :islands, :player_kills_all, :player_kills_att, :player_kills_def, :players, :towns].each do |i|
9
+ require "grepolis/api/#{i}"
10
+ end
11
+
12
+ module Grepolis
13
+ module Api
14
+ def self.method_missing(meth, *args)
15
+ Connection.new(world: meth)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,7 @@
1
+ module Grepolis
2
+ module Api
3
+ class AllianceKillsAll < Base
4
+ MAPPING = [:rank, :id, :points]
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ module Grepolis
2
+ module Api
3
+ class AllianceKillsAtt < Base
4
+ MAPPING = [:rank, :id, :points]
5
+ end
6
+ end
7
+ end
8
+
@@ -0,0 +1,8 @@
1
+ module Grepolis
2
+ module Api
3
+ class AllianceKillsDef < Base
4
+ MAPPING = [:rank, :id, :points]
5
+ end
6
+ end
7
+ end
8
+
@@ -0,0 +1,8 @@
1
+ module Grepolis
2
+ module Api
3
+ class Alliances < Base
4
+ MAPPING = [:id, :name, :points, :towns, :members, :rank]
5
+ end
6
+ end
7
+ end
8
+
@@ -0,0 +1,43 @@
1
+ module Grepolis
2
+ module Api
3
+ class Base
4
+ attr_reader :conn
5
+
6
+ def initialize(conn)
7
+ @conn = conn
8
+ @index = {}.tap {|index| self.class::MAPPING.each{|column| index[column] = {}}}
9
+ end
10
+
11
+ def all
12
+ data
13
+ end
14
+
15
+ def method_missing(meth, *args)
16
+ meth = meth.to_s
17
+ regex = /find_by_/
18
+ column = meth.gsub(regex, '').to_sym
19
+ if meth =~ regex and data and @index[column]
20
+ @index[column][args[0]]
21
+ else
22
+ super
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def data
29
+ @data ||= fetch
30
+ end
31
+
32
+ def fetch
33
+ endpoint = self.class.name.split("::").last.underscore
34
+ data = conn.request(endpoint, self.class::MAPPING)
35
+ data.each do |datum|
36
+ self.class::MAPPING.each do |key|
37
+ @index[key][datum[key]] = datum
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,58 @@
1
+ require 'faraday'
2
+
3
+ module Grepolis
4
+ module Api
5
+ class Connection
6
+ attr_accessor :world, :protocol
7
+
8
+ def initialize(options = {})
9
+ @world = options[:world]
10
+ @protocol = options[:protocol] || 'http'
11
+ end
12
+
13
+ def uri
14
+ "#{protocol}://#{world}.grepolis.com"
15
+ end
16
+
17
+ def request(type, mapping, response_handler = method(:default_response_handler))
18
+ conn = Faraday.new(:url => uri) do |faraday|
19
+ faraday.response :logger # log requests to STDOUT
20
+ faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
21
+ end
22
+
23
+ response = conn.get("/data/#{type}.txt")
24
+
25
+ response_handler.call response, mapping
26
+ end
27
+
28
+ def method_missing(meth, *args)
29
+ begin
30
+ "Grepolis::Api::#{meth.to_s.camelize}".constantize.new(self)
31
+ rescue
32
+ super
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def default_response_handler(response, mapping)
39
+ results = []
40
+ data = StringIO.new response.body
41
+ data.each_line do |datum|
42
+ results << {}.tap do |store|
43
+ datum.chomp.split(',').each_with_index do |part, i|
44
+ store[mapping[i]] = if mapping[i].to_s =~ /id$/
45
+ part.to_i
46
+ elsif mapping[i] == :name
47
+ part.gsub('+', ' ')
48
+ else
49
+ part
50
+ end
51
+ end
52
+ end
53
+ end
54
+ return results
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,7 @@
1
+ module Grepolis
2
+ module Api
3
+ class Conquers < Base
4
+ MAPPING = [:town_id, :time, :new_player_id, :old_player_id, :new_ally_id, :old_ally_id]
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Grepolis
2
+ module Api
3
+ class Islands < Base
4
+ MAPPING = [:id, :x, :y, :island_type, :available_towns]
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ module Grepolis
2
+ module Api
3
+ class PlayerKillsAll < Base
4
+ MAPPING = [:rank, :id, :points]
5
+ end
6
+ end
7
+ end
8
+
@@ -0,0 +1,7 @@
1
+ module Grepolis
2
+ module Api
3
+ class PlayerKillsAtt < Base
4
+ MAPPING = [:rank, :id, :points]
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Grepolis
2
+ module Api
3
+ class PlayerKillsDef < Base
4
+ MAPPING = [:rank, :id, :points]
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Grepolis
2
+ module Api
3
+ class Players < Base
4
+ MAPPING = [:id, :name, :alliance_id, :points, :rank, :towns]
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ module Grepolis
2
+ module Api
3
+ class Towns < Base
4
+ MAPPING = [:id, :player_id, :name, :island_x, :island_y, :number_on_island, :points]
5
+ end
6
+ end
7
+ end
8
+
9
+
@@ -0,0 +1,5 @@
1
+ module Grepolis
2
+ module Api
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,180 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grepolis-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dan Matthews
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: debugger
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: faraday
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: multi_json
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: yajl-ruby
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: activesupport
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ description: Wraps the data provided by InnoGames for Grepolis worlds
127
+ email:
128
+ - dan@bluefoc.us
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - .gitignore
134
+ - .rvmrc
135
+ - Gemfile
136
+ - LICENSE.txt
137
+ - README.md
138
+ - Rakefile
139
+ - grepolis-api.gemspec
140
+ - lib/grepolis/api.rb
141
+ - lib/grepolis/api/alliance_kills_all.rb
142
+ - lib/grepolis/api/alliance_kills_att.rb
143
+ - lib/grepolis/api/alliance_kills_def.rb
144
+ - lib/grepolis/api/alliances.rb
145
+ - lib/grepolis/api/base.rb
146
+ - lib/grepolis/api/connection.rb
147
+ - lib/grepolis/api/conquers.rb
148
+ - lib/grepolis/api/islands.rb
149
+ - lib/grepolis/api/player_kills_all.rb
150
+ - lib/grepolis/api/player_kills_att.rb
151
+ - lib/grepolis/api/player_kills_def.rb
152
+ - lib/grepolis/api/players.rb
153
+ - lib/grepolis/api/towns.rb
154
+ - lib/grepolis/api/version.rb
155
+ homepage: ''
156
+ licenses:
157
+ - MIT
158
+ post_install_message:
159
+ rdoc_options: []
160
+ require_paths:
161
+ - lib
162
+ required_ruby_version: !ruby/object:Gem::Requirement
163
+ none: false
164
+ requirements:
165
+ - - ! '>='
166
+ - !ruby/object:Gem::Version
167
+ version: '0'
168
+ required_rubygems_version: !ruby/object:Gem::Requirement
169
+ none: false
170
+ requirements:
171
+ - - ! '>='
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ requirements: []
175
+ rubyforge_project:
176
+ rubygems_version: 1.8.25
177
+ signing_key:
178
+ specification_version: 3
179
+ summary: ''
180
+ test_files: []