Module: Epages::Utils
- Included in:
- Address, BasePrice, Cart, Category, ContentPageSummary, CustomAttribute, Customer, CustomersResponse, DeliveryWeightQuantity, Image, ImageSize, LineItemContainer, Link, MinimumCart, Newsletter, Order, OrdersResponse, PaymentData, PaymentMethodInfo, Price, PriceInfo, Product, ProductLineItem, ProductsResponse, Quantity, REST::Legal, REST::Miscellaneous, REST::Request, REST::ShippingMethods, Sales, SalesSummary, ShippingData, ShippingMethod, ShippingMethodInfo, Subscriber, TaxClass, TaxClassInfo, Variation, VariationAttribute, VariationIdentifier, Webhook, WebhookResponse, WebhookResponsePagination
- Defined in:
- lib/epages/utils.rb
Class Method Summary collapse
-
.build_shop_from(shop) ⇒ Object
returns a shop.
-
.camelize_keys(hash) ⇒ Object
return the hash with the keys converted to lower camelcase.
-
.camelize_words(string) ⇒ Object
return the string passed as a parameter with all the words camelized.
-
.options_to_multipart_request(options) ⇒ Object
returns the json body for the patch calls.
-
.options_to_patch_request(options) ⇒ Object
returns the json body for the patch calls.
-
.parse_attribute_as(name, data, klass = NilClass) ⇒ Object
creates a instance variable (underscore) from the [data] if [klass] is passed, creates a object of [klass] initialized with data.
-
.parse_attribute_as_array_of(name, data, klass) ⇒ Object
creates a instance variable (underscore).
-
.parse_attributes(data, klass = nil) ⇒ Object
creates instance variables from the data if klass is passed, create a object initializing with data.
-
.symbolize_keys!(object) ⇒ Object
returns the object replacing all the keys as symbols.
-
.to_query_options(options) ⇒ Object
return a string that acts as a query parameters (for POST calls).
-
.underscorize_keys(hash) ⇒ Object
return the hash with the keys converted to underscore.
Class Method Details
.build_shop_from(shop) ⇒ Object
returns a shop. If [shop] is not a Epages::REST::Shop create one calling the [shop] method shop_mame
77 78 79 80 |
# File 'lib/epages/utils.rb', line 77 def build_shop_from(shop) return shop if shop.is_a? Epages::REST::Shop Epages::REST::Shop.new(shop.host, shop.shop_name) end |
.camelize_keys(hash) ⇒ Object
return the hash with the keys converted to lower camelcase
53 54 55 56 57 58 59 60 61 |
# File 'lib/epages/utils.rb', line 53 def camelize_keys(hash) return unless hash.is_a?(Hash) other = {} hash.keys.each do |k| key = k.to_s.camelize(:lower).to_sym other[key] = hash[k] end other end |
.camelize_words(string) ⇒ Object
return the string passed as a parameter with all the words camelized
116 117 118 |
# File 'lib/epages/utils.rb', line 116 def camelize_words(string) string.to_s.gsub(/(\w+)/) { |s| s.camelize(:lower) } end |
.options_to_multipart_request(options) ⇒ Object
returns the json body for the patch calls
107 108 109 110 111 |
# File 'lib/epages/utils.rb', line 107 def () file = [:file] @request_method = :post {image: HTTP::FormData::File.new(file, filename: File.basename(file), mime_type: mime_type(File.basename(file)))} end |
.options_to_patch_request(options) ⇒ Object
returns the json body for the patch calls
97 98 99 100 101 102 |
# File 'lib/epages/utils.rb', line 97 def () json = [] Array[.delete(:remove)].flatten.compact.each { |i| json << {'op' => 'remove', 'path' => "/#{camelize_words(i)}"} } .each { |k, v| json << {'op' => 'add', 'path' => "/#{camelize_words(k)}", 'value' => v} } json.collect { |i| Hash[i.each_pair.to_a] } end |
.parse_attribute_as(name, data, klass = NilClass) ⇒ Object
creates a instance variable (underscore) from the [data] if [klass] is passed, creates a object of [klass] initialized with data
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/epages/utils.rb', line 16 def parse_attribute_as(name, data, klass = NilClass) return if data.nil? value = case klass.name when 'NilClass' then data when 'DateTime' then klass.parse(data) else klass.new(data) end instance_variable_set("@#{name.to_s.underscore}", value) end |
.parse_attribute_as_array_of(name, data, klass) ⇒ Object
creates a instance variable (underscore). This variable contains an array of <klass> or empty array
32 33 34 35 |
# File 'lib/epages/utils.rb', line 32 def parse_attribute_as_array_of(name, data, klass) value = data.class != Array || data.empty? ? [] : data.collect { |i| klass.new(i) } instance_variable_set("@#{name.to_s.underscore}", value) end |
.parse_attributes(data, klass = nil) ⇒ Object
creates instance variables from the data if klass is passed, create a object initializing with data
42 43 44 45 46 47 48 |
# File 'lib/epages/utils.rb', line 42 def parse_attributes(data, klass = nil) data.keys.each do |key| next if data[key.to_sym].nil? value = klass.nil? ? data[key.to_sym] : klass.new(data[key.to_sym]) instance_variable_set("@#{key.to_s.underscore}", value) end end |
.symbolize_keys!(object) ⇒ Object
returns the object replacing all the keys as symbols
85 86 87 88 89 90 91 92 |
# File 'lib/epages/utils.rb', line 85 def symbolize_keys!(object) if object.is_a?(Array) object.each_with_index { |val, index| object[index] = symbolize_keys!(val) } elsif object.is_a?(Hash) object.keys.each { |key| object[key.to_sym] = symbolize_keys!(object.delete(key)) } end object end |
.to_query_options(options) ⇒ Object
return a string that acts as a query parameters (for POST calls)
123 124 125 126 |
# File 'lib/epages/utils.rb', line 123 def () return '' if !.is_a?(Hash) || .empty? "?#{.map{ |k,v| "#{k}=#{v}" }.join('&')}" end |
.underscorize_keys(hash) ⇒ Object
return the hash with the keys converted to underscore
66 67 68 69 70 71 72 73 74 |
# File 'lib/epages/utils.rb', line 66 def underscorize_keys(hash) return unless hash.is_a?(Hash) hash.keys.each do |k| key = k.to_s.underscore.to_sym hash[key] = hash[k] hash.delete(k) if key != k end hash end |