Class: ShipStationRuby::Collection
- Inherits:
-
Object
- Object
- ShipStationRuby::Collection
- Defined in:
- lib/shipstation_ruby/collection.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#resource ⇒ Object
Returns the value of attribute resource.
Instance Method Summary collapse
- #all ⇒ Object
- #find(id) ⇒ Object
-
#initialize(client, resource) ⇒ Collection
constructor
A new instance of Collection.
- #where(filters = {}) ⇒ Object
Constructor Details
#initialize(client, resource) ⇒ Collection
Returns a new instance of Collection.
6 7 8 9 |
# File 'lib/shipstation_ruby/collection.rb', line 6 def initialize(client, resource) @client = client @resource = resource end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
4 5 6 |
# File 'lib/shipstation_ruby/collection.rb', line 4 def client @client end |
#resource ⇒ Object
Returns the value of attribute resource.
4 5 6 |
# File 'lib/shipstation_ruby/collection.rb', line 4 def resource @resource end |
Instance Method Details
#all ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/shipstation_ruby/collection.rb', line 20 def all @client.send("#{@resource}") results = @client.execute formatted_results = [] results.each do |result| result_hash = JSON.parse(result.to_json) result_rash = Hashie::Rash.new(result_hash) formatted_results.push(result_rash) end return formatted_results end |
#find(id) ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/shipstation_ruby/collection.rb', line 11 def find(id) @client.send("#{@resource}",id) result = @client.execute single_result = result.first json_hash = JSON.parse(single_result.to_json) json_rash = Hashie::Rash.new(json_hash) return json_rash end |
#where(filters = {}) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/shipstation_ruby/collection.rb', line 32 def where(filters={}) final_string = "" final_string_array = [] filters.each do |attribute, value| shipstation_style_attribute = attribute.to_s.classify.gsub(/Id/, 'ID') puts shipstation_style_attribute puts value if value.is_a?(Integer) filter_string = "#{shipstation_style_attribute} eq #{value}" else filter_string = "#{shipstation_style_attribute} eq '#{value}'" end final_string_array << filter_string end final_string = final_string_array.join(' and ') puts final_string @client.send("#{@resource}").filter("#{final_string}") results = @client.execute formatted_results = [] results.each do |result| result_hash = JSON.parse(result.to_json) result_rash = Hashie::Rash.new(result_hash) formatted_results.push(result_rash) end return formatted_results end |