Module: BlommingApi::PublicHelpers
- Included in:
- Client
- Defined in:
- lib/blomming_api/public_helpers.rb
Class Method Summary collapse
-
.collect_key_values(array, key_name) ⇒ Object
collect key values associated to a key in
array
of hashes return nil ifarray
doesn’t exist return array containing a values list associated tokey_name
. -
.id_from_name(name, data) ⇒ Object
search in
data
hash fieldname
and get value of corresponding ‘id’ (per endpoint categories, collections). -
.to_eurolocal(iso8601_timestamp) ⇒ Object
Convert a timestamp in ISO 8601 format to Time.
-
.to_eurolocal_timestamp(time) ⇒ Object
Print timestamp from Time in UTC to local time in “European” timestamp format.
-
.to_time(iso8601_timestamp) ⇒ Object
Convert a timestamp in ISO 8601 format to Time.
Instance Method Summary collapse
-
#all_pages(verbose = :quite, per_page = 16, &endpoint_call_block) ⇒ Object
all_pages It’s a Ruby block iterator that retrieve all items of all pages of any API endpoint.
- #dump_pretty(json_data) ⇒ Object
- #puts_response_header(method, data) ⇒ Object
Class Method Details
.collect_key_values(array, key_name) ⇒ Object
collect key values associated to a key in array
of hashes return nil if array
doesn’t exist return array containing a values list associated to key_name
101 102 103 104 |
# File 'lib/blomming_api/public_helpers.rb', line 101 def self.collect_key_values (array, key_name) return nil if array.nil? return array.collect { |item| item[key_name] } end |
.id_from_name(name, data) ⇒ Object
search in data
hash field name
and get value of corresponding ‘id’ (per endpoint categories, collections)
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/blomming_api/public_helpers.rb', line 84 def self.id_from_name (name, data) id = nil data.each { |item| if name == item["name"] # estrae l'id dal campo: items_url. id = item["items_url"].split('/')[-2] # scan( /\d+/ ).last break end } id end |
.to_eurolocal(iso8601_timestamp) ⇒ Object
Convert a timestamp in ISO 8601 format to Time
example:
= '2014-01-14T15:17:42Z'
to_eurolocal => '14-01-2014 16:17:42'
145 146 147 |
# File 'lib/blomming_api/public_helpers.rb', line 145 def self.to_eurolocal () to_time end |
.to_eurolocal_timestamp(time) ⇒ Object
Print timestamp from Time in UTC to local time in “European” timestamp format
example:
time = 2014-01-14 15:17:42 UTC
time => => '14-01-2014 16:17:42'
131 132 133 134 |
# File 'lib/blomming_api/public_helpers.rb', line 131 def self. (time) time.localtime if time.utc? time.strftime "%d-%m-%Y %H:%M:%S" end |
.to_time(iso8601_timestamp) ⇒ Object
Convert a timestamp in ISO 8601 format to Time
example:
= '2014-01-14T15:17:42Z'
to_time => 2014-01-14 15:17:42 UTC
118 119 120 |
# File 'lib/blomming_api/public_helpers.rb', line 118 def self.to_time () Time.iso8601() end |
Instance Method Details
#all_pages(verbose = :quite, per_page = 16, &endpoint_call_block) ⇒ Object
all_pages It’s a Ruby block iterator that retrieve all items of all pages of any API endpoint.
arguments
verbose
: :quite (silent mode)
:stdout (verbose mode: stdout puts)
per_page
: :number of items returned from a single page
examples
all_pages(:stdout, 64) do |page, per_page|
client.shop_items(shop_id, {:page => page, :per_page => per_page})
end
all_pages { |page, per_page| c.sell_shop_items page: page, per_page: per_page }
30 31 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 59 |
# File 'lib/blomming_api/public_helpers.rb', line 30 def all_pages (verbose=:quite, per_page=16, &endpoint_call_block) page = 1 data = [] unless block_given? raise 'method require a block! usage: #{__method__} '\ '{ |page, per_page| endpoint_method(..., {page: page, per_page: per_page}) }' end print 'collecting all items from de-pagination ' if verbose==:stdout loop do print "." if verbose==:stdout # run block passing local variables: page, per_page. data_single_page = endpoint_call_block.call page, per_page # debug # data_single_page.each_with_index { |item, index| # puts "#{index+1}: title: #{item["title"]}, id: #{item["id"]}, shop: #{item["shop"]["id"]}" } data.concat data_single_page break if (data_single_page.size < per_page) || data_single_page.empty? page += 1 end print "\n" if verbose==:stdout data end |
#dump_pretty(json_data) ⇒ Object
70 71 72 73 |
# File 'lib/blomming_api/public_helpers.rb', line 70 def dump_pretty (json_data) # JSON.pretty_generate(JSON.parse(json_data)) puts MultiJson.dump json_data, :pretty => true end |
#puts_response_header(method, data) ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/blomming_api/public_helpers.rb', line 61 def puts_response_header(method, data) puts "#{method.to_s} response header_params:" puts data.header_params.to_s puts puts "#{method.to_s} response data:" puts data puts end |