Module: BlommingApi::PublicHelpers

Included in:
Client
Defined in:
lib/blomming_api/public_helpers.rb

Class Method Summary collapse

Instance Method Summary collapse

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



100
101
102
103
# File 'lib/blomming_api/public_helpers.rb', line 100

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)



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/blomming_api/public_helpers.rb', line 83

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

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 }



29
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
# File 'lib/blomming_api/public_helpers.rb', line 29

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



69
70
71
72
# File 'lib/blomming_api/public_helpers.rb', line 69

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



60
61
62
63
64
65
66
67
# File 'lib/blomming_api/public_helpers.rb', line 60

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