Class: Storenvy::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/storenvy/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
# File 'lib/storenvy/client.rb', line 13

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       
end

Instance Attribute Details

#accountObject (readonly)

Returns the value of attribute account.



11
12
13
# File 'lib/storenvy/client.rb', line 11

def 
  @account
end

#hostObject (readonly)

Returns the value of attribute host.



11
12
13
# File 'lib/storenvy/client.rb', line 11

def host
  @host
end

#maintenance_modeObject (readonly)

Returns the value of attribute maintenance_mode.



11
12
13
# File 'lib/storenvy/client.rb', line 11

def maintenance_mode
  @maintenance_mode
end

Class Method Details

.fetch(path) ⇒ Object

GET for single resource



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/storenvy/client.rb', line 21

def self.fetch(path)
  response = get(path)     
  case response.code
    when 200
      Hashie::Mash.new(response) 
    when 503        
      @maitenance_mode = true
      Hashie::Mash.new({:error => "#{response.code} - Maintenance Mode", :maintenance_mode => true})
    else
      Hashie::Mash.new({:error => response.code})
  end        
end

.list(path, opts = {}) ⇒ Object

GET when response expects multiple resources



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/storenvy/client.rb', line 36

def self.list(path, opts={})      
  response = get(path, :query =>  {'page' => opts[:page]})           

  case response.code
    when 200
      response.map { |c| Hashie::Mash.new(c) }
    when 503
      # {:error => "#{response.code} - Maintenance Mode"}          
      @maitenance_mode = true
      []
    else
      {:error => response.code}
  end      
end

Instance Method Details

#collection(id) ⇒ Object

GET Collection resource



82
83
84
# File 'lib/storenvy/client.rb', line 82

def collection(id)
  self.class.fetch("http://#{@host}/collections/#{id}.json")
end

#collectionsObject

GET Collections resources



77
78
79
# File 'lib/storenvy/client.rb', line 77

def collections
  self.class.fetch("http://#{@host}/collections.json").collections
end

#product(id) ⇒ Object



72
73
74
# File 'lib/storenvy/client.rb', line 72

def product(id)
  self.class.fetch("http://#{@host}/products/#{id}.json")
end

#products(display_all_products = false, page = 1) ⇒ Object

GET Products resources, all products or single page (max 50 products per page is API limit)



67
68
69
70
# File 'lib/storenvy/client.rb', line 67

def products(display_all_products = false, page = 1)
  return all_products() if display_all_products
  return self.class.list("http://#{@host}/products.json", {:page => page})
end

#store(opts = {}) ⇒ Object

GET store resource



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/storenvy/client.rb', line 53

def store(opts={})
 opts = { :show_products => true }.merge opts
 
  data = self.class.fetch("http://#{@host}/store.json")
  unless @maintenance_mode
    data.products = opts[:show_products] ?  products(true) : {} 
  else 
    data.products = nil
  end
  data
end