Skip to content

Commit

Permalink
Merge e858d77 into 51b5a80
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaracco committed Feb 9, 2015
2 parents 51b5a80 + e858d77 commit 9c1d0bd
Show file tree
Hide file tree
Showing 7 changed files with 851 additions and 44 deletions.
6 changes: 6 additions & 0 deletions lib/lol/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,16 @@ def lol_status
@lol_status ||= LolStatusRequest.new(region, cache_store)
end

# @return [CurrentGameRequest]
def current_game
@current_game ||= CurrentGameRequest.new(api_key, region, cache_store)
end

# @return [FeaturedGamesRequest]
def featured_games
@featured_games ||= FeaturedGamesRequest.new(api_key, region, cache_store)
end

# Initializes a Lol::Client
# @param api_key [String]
# @param options [Hash]
Expand Down
85 changes: 43 additions & 42 deletions lib/lol/dynamic_model.rb
Original file line number Diff line number Diff line change
@@ -1,57 +1,58 @@
require 'ostruct'
require 'active_support/core_ext/string/inflections'

# DynamicModel extends OpenStruct adding the following features:
# - nested generation ({a: {}}) results in DynamicModel(a: DynamicModel)
# - parsing of date/time when property name ends with _at or _date and the value is a number
class DynamicModel < OpenStruct
def initialize(hash={})
raise ArgumentError, 'An hash is required as parameter' unless hash.is_a? Hash
@table = {}
@hash_table = {}

hash.each do |k,v|
key = k.to_s.underscore
set_property key, v
new_ostruct_member(key)
module Lol
# DynamicModel extends OpenStruct adding the following features:
# - nested generation ({a: {}}) results in DynamicModel(a: DynamicModel)
# - parsing of date/time when property name ends with _at or _date and the value is a number
class DynamicModel < OpenStruct
def initialize(hash={})
raise ArgumentError, 'An hash is required as parameter' unless hash.is_a? Hash
@table = {}
@hash_table = {}

hash.each do |k,v|
key = k.to_s.underscore
set_property key, v
new_ostruct_member(key)
end
end
end

def to_h
@hash_table
end
def to_h
@hash_table
end

def as_json opts={}
@table.as_json
end
def as_json opts={}
@table.as_json
end

private
private

def date_key? key
key.match(/^(.+_)?(at|date)$/)
end
def date_key? key
key.match(/^(.+_)?(at|date)$/)
end

def set_property key, v
if date_key?(key) && v.is_a?(Fixnum)
@table[key.to_sym] = @hash_table[key.to_sym] = value_to_date v
else
@table[key.to_sym] = convert_object v
@hash_table[key.to_sym] = v
def set_property key, v
if date_key?(key) && v.is_a?(Fixnum)
@table[key.to_sym] = @hash_table[key.to_sym] = value_to_date v
else
@table[key.to_sym] = convert_object v
@hash_table[key.to_sym] = v
end
end
end

def value_to_date v
Time.at(v / 1000)
end
def value_to_date v
Time.at(v / 1000)
end

def convert_object obj
if obj.is_a? Hash
self.class.new obj
elsif obj.respond_to?(:map)
obj.map { |o| convert_object o }
else
obj
def convert_object obj
if obj.is_a? Hash
self.class.new obj
elsif obj.respond_to?(:map)
obj.map { |o| convert_object o }
else
obj
end
end
end

end
15 changes: 15 additions & 0 deletions lib/lol/featured_games_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Lol
class FeaturedGamesRequest < Request
def self.api_version
"v1.0"
end

def api_url path, params = {}
"#{api_base_url}/observer-mode/rest/#{path}?#{api_query_string params}"
end

def get
DynamicModel.new perform_request api_url "featured"
end
end
end

0 comments on commit 9c1d0bd

Please sign in to comment.