Skip to content

Commit f9b408a

Browse files
committed
initial import
0 parents  commit f9b408a

File tree

11 files changed

+136
-0
lines changed

11 files changed

+136
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.gem
2+
.bundle
3+
Gemfile.lock
4+
pkg/*

Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source "http://rubygems.org"
2+
3+
# Specify your gem's dependencies in gg.gemspec
4+
gemspec

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
### GG!
2+
3+
This gem will include both esdb and ggtracker API implementations. We could separate the two, but for now we'll keep it inside a single gem and see if it's beneficial to separate it later on.
4+
5+
(And even though it's unlikely we will have the gem around in esdb, I might want to not use the ESDB namespace ..)

Rakefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require 'bundler'
2+
Bundler::GemHelper.install_tasks

gg.gemspec

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- encoding: utf-8 -*-
2+
$:.push File.expand_path("../lib", __FILE__)
3+
require "gg/version"
4+
5+
Gem::Specification.new do |s|
6+
s.name = "gg"
7+
s.version = GG::VERSION
8+
s.platform = Gem::Platform::RUBY
9+
s.authors = ["Marian Rudzynski"]
10+
s.email = ["[email protected]"]
11+
s.homepage = ""
12+
s.summary = ""
13+
s.description = ""
14+
15+
s.add_dependency('rest-client')
16+
s.add_dependency('yajl-ruby')
17+
18+
# Because I just don't want to live without some of it..
19+
s.add_dependency('active_support')
20+
21+
s.files = `git ls-files`.split("\n")
22+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
23+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
24+
s.require_paths = ["lib"]
25+
end

lib/esdb.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module ESDB
2+
# Simply transforms a stats hash into a StatsParam string
3+
class StatsParam
4+
def initialize(stats)
5+
@array = []
6+
@stats = stats
7+
stats.each do |key, value|
8+
if value.is_a?(Array)
9+
values = value.join(',')
10+
elsif value === true
11+
values = 'avg'
12+
else
13+
values = value
14+
end
15+
16+
@array << "#{key}(#{values})"
17+
end
18+
end
19+
20+
def to_s
21+
@array.join(',')
22+
end
23+
end
24+
end
25+
26+
require 'lib/esdb/resource'
27+
require 'lib/esdb/identity'
28+
require 'lib/esdb/stat'

lib/esdb/identity.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module ESDB
2+
class Identity < ESDB::Resource
3+
end
4+
end

lib/esdb/resource.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
module ESDB
2+
class Resource < RestClient::Resource
3+
attr_accessor :params
4+
5+
def initialize(url=nil, options={}, *args)
6+
options[:headers] ||= {}
7+
8+
# Lets you specify options, finder-like, as the first argument:
9+
# Resource.new(:id => 123)
10+
if url.is_a?(Hash)
11+
options[:headers][:params] ||= {}
12+
options[:headers][:params].merge!(url)
13+
url = nil
14+
end
15+
16+
# REST is DRY! Let's just guess our url..
17+
klass_ep = self.class.to_s.scan(/::(.*?)$/).flatten[0].pluralize.underscore
18+
@url ||= "http://localhost:9292/api/v1/#{klass_ep}"
19+
20+
# For now, various params will trigger a change in the URI
21+
# might want to do it more like Rails in esdb ..not sure yet
22+
@params = options[:headers][:params]
23+
if @params.any?
24+
@url += "/#{@params.delete(:id)}" if @params[:id]
25+
options[:headers][:params][:stats] = StatsParam.new(options[:headers][:params][:stats]) if @params[:stats]
26+
end
27+
28+
# And we also default to JSON
29+
options[:headers][:accept] ||= 'application/json'
30+
31+
@options = options
32+
# Screw super, there's nothing useful in it anyway.
33+
# https://github.com/archiloque/rest-client/blob/master/lib/restclient/resource.rb#L39
34+
# super(url, *args)
35+
end
36+
37+
# Retrieves the resource and unlike RestClient, stores the result in the
38+
# instance instead of returning it (but also returns it)
39+
def get!(*args)
40+
@response = get(*args)
41+
end
42+
43+
def json
44+
get! unless @response
45+
JSON.parse(@response)
46+
end
47+
end
48+
end

lib/esdb/stat.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module ESDB
2+
class Stat < ESDB::Resource
3+
end
4+
end

lib/gg.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module GG
2+
end
3+
4+
require 'rest_client'
5+
require 'active_support/inflector'
6+
require 'yajl'
7+
require 'yajl/json_gem'
8+
9+
require 'esdb'

0 commit comments

Comments
 (0)