Class: ProductWars::Client
- Inherits:
-
Object
- Object
- ProductWars::Client
- Includes:
- HTTParty
- Defined in:
- lib/productwars/client.rb
Class Method Summary collapse
- .all_wars(params = "") ⇒ Object
- .dispatch(method, *args, &block) ⇒ Object
- .global_leaders(params = "") ⇒ Object
- .handle(response) ⇒ Object
- .leaders_in_war(war_id, params = "") ⇒ Object
- .new_product_wars_object(info) ⇒ Object
- .parse_params(params_hash) ⇒ Object
- .product(product_id, params = "") ⇒ Object
- .product_stats(product_id, params = "") ⇒ Object
- .product_wars_type?(obj) ⇒ Boolean
-
.products(params = "") ⇒ Object
API Calls ###########.
- .products_in_war(war_id, params = "") ⇒ Object
- .war(war_id, params = "") ⇒ Object
- .wars_containing_product(product_id, params = "") ⇒ Object
- .wrap(response) ⇒ Object
Class Method Details
.all_wars(params = "") ⇒ Object
44 45 46 |
# File 'lib/productwars/client.rb', line 44 def self.all_wars(params="") get("/wars.json#{params}") end |
.dispatch(method, *args, &block) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/productwars/client.rb', line 17 def self.dispatch(method, *args, &block) base_uri "#{ProductWars.domain}/api/v1" if args.count == 1 and args[0].class == Hash args[0] = parse_params(args[0]) elsif args.count == 2 and args[1].class == Hash args[1] = parse_params(args[1]) end response = self.send(method, *args, &block) self.handle(response) end |
.global_leaders(params = "") ⇒ Object
60 61 62 |
# File 'lib/productwars/client.rb', line 60 def self.global_leaders(params="") get("/leaders.json#{params}") end |
.handle(response) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/productwars/client.rb', line 69 def self.handle(response) if response.response.code.to_i == 404 raise ProductWars::Errors::NotFound else self.wrap(response) end end |
.leaders_in_war(war_id, params = "") ⇒ Object
64 65 66 |
# File 'lib/productwars/client.rb', line 64 def self.leaders_in_war(war_id, params="") get("/wars/#{war_id}/leaders.json#{params}") end |
.new_product_wars_object(info) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/productwars/client.rb', line 136 def self.new_product_wars_object(info) type = product_wars_type?(info) if type == ProductWars::Product ProductWars::Product.new(info) elsif type == ProductWars::War ProductWars::War.new(info) elsif type == ProductWars::Stats ProductWars::Stats.new(info) else nil end end |
.parse_params(params_hash) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/productwars/client.rb', line 150 def self.parse_params(params_hash) if not params_hash.class == Hash return "" end param_string = "?" for k, v in params_hash param_string = param_string + "#{k.to_s}=#{v}&" end return param_string end |
.product(product_id, params = "") ⇒ Object
36 37 38 |
# File 'lib/productwars/client.rb', line 36 def self.product(product_id, params="") get("/products/#{product_id}.json#{params}") end |
.product_stats(product_id, params = "") ⇒ Object
48 49 50 |
# File 'lib/productwars/client.rb', line 48 def self.product_stats(product_id, params="") get("/stats/products/#{product_id}.json#{params}") end |
.product_wars_type?(obj) ⇒ Boolean
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/productwars/client.rb', line 118 def self.product_wars_type?(obj) if obj.nil? return nil end if obj.class == Hash if obj.has_key?("dp_id") if obj.has_key?("win_rate") ProductWars::Stats else ProductWars::Product end else ProductWars::War end end end |
.products(params = "") ⇒ Object
API Calls ###########
32 33 34 |
# File 'lib/productwars/client.rb', line 32 def self.products(params="") get("/products.json#{params}") end |
.products_in_war(war_id, params = "") ⇒ Object
56 57 58 |
# File 'lib/productwars/client.rb', line 56 def self.products_in_war(war_id, params="") get("/wars/#{war_id}/products.json#{params}") end |
.war(war_id, params = "") ⇒ Object
52 53 54 |
# File 'lib/productwars/client.rb', line 52 def self.war(war_id, params="") get("/wars/#{war_id}.json#{params}") end |
.wars_containing_product(product_id, params = "") ⇒ Object
40 41 42 |
# File 'lib/productwars/client.rb', line 40 def self.wars_containing_product(product_id, params="") get("/products/#{product_id}/wars.json#{params}") end |
.wrap(response) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/productwars/client.rb', line 77 def self.wrap(response) info = response.parsed_response items = [] sym = nil if info.has_key?("products") sym = "products" items = info[sym] elsif info.has_key?("wars") sym = "wars" items = info[sym] end if items.count > 0 a = [] for hash in items a.push(new_product_wars_object(hash)) end info[sym] = a return info elsif info.class == Hash return new_product_wars_object(info) end #if info.class == Array # a = [] # for hash in info # a.push(new_product_wars_object(hash)) # end # return a #elsif info.class == Hash # new_product_wars_object(info) #end end |