-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
851 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.