lib/storenvy/client.rb in storenvy-0.1.1 vs lib/storenvy/client.rb in storenvy-0.1.3

- old
+ new

@@ -6,11 +6,11 @@ class Client include HTTParty headers 'Content-Type' => 'application/json' - attr_reader :account, :host + attr_reader :account, :host, :maintenance_mode def initialize(host) host.gsub!(/https?:\/\//, '') # remove http(s):// @account = host.include?('.') ? host.match(/^\w*/i).to_s : host @host = "#{@account}.storenvy.com" # extend url to storenvy.com if no host is given @@ -21,14 +21,15 @@ def self.fetch(path) response = get(path) case response.code when 200 Hashie::Mash.new(response) - when 503 - {:error => "#{response.code} - Maintenance Mode"} + when 503 + @maitenance_mode = true + Hashie::Mash.new({:error => "#{response.code} - Maintenance Mode", :maintenance_mode => true}) else - {:error => response.code} + Hashie::Mash.new({:error => response.code}) end end # GET when response expects multiple resources @@ -37,23 +38,29 @@ case response.code when 200 response.map { |c| Hashie::Mash.new(c) } when 503 - {:error => "#{response.code} - Maintenance Mode"} + # {:error => "#{response.code} - Maintenance Mode"} + @maitenance_mode = true + [] else {:error => response.code} end - end + end # GET store resource def store(opts={}) opts = { :show_products => true }.merge opts data = self.class.fetch("http://#{@host}/store.json") - data.products = opts[:show_products] ? products(true) : {} + unless @maintenance_mode + data.products = opts[:show_products] ? products(true) : {} + else + data.products = nil + end data end # GET Products resources, all products or single page (max 50 products per page is API limit) @@ -85,14 +92,14 @@ def all_products page = 1 products = [] while true data = self.class.list("http://#{@host}/products.json", {:page => page}) - products = (products+data).group_by{|h| h[:id]}.map{ |k,v| v.reduce(:merge)} + products = (products+data).group_by{|h| h[:id]}.map{ |k,v| v.reduce(:merge)} unless @maintenance_mode page = page + 1 break if data.count < 50 end products - end + end end end