Class: Pobo::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/pobo/client.rb

Constant Summary collapse

DEFAULT_BASE_URL =
"https://api.pobo.space"
MAX_BULK_ITEMS =
100
DEFAULT_TIMEOUT =
30

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_token:, base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT) ⇒ Client

Returns a new instance of Client.



14
15
16
17
18
# File 'lib/pobo/client.rb', line 14

def initialize(api_token:, base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT)
  @api_token = api_token
  @base_url = base_url
  @timeout = timeout
end

Instance Attribute Details

#api_token ⇒ Object (readonly)

Returns the value of attribute api_token.



12
13
14
# File 'lib/pobo/client.rb', line 12

def api_token
  @api_token
end

#base_url ⇒ Object (readonly)

Returns the value of attribute base_url.



12
13
14
# File 'lib/pobo/client.rb', line 12

def base_url
  @base_url
end

#timeout ⇒ Object (readonly)

Returns the value of attribute timeout.



12
13
14
# File 'lib/pobo/client.rb', line 12

def timeout
  @timeout
end

Instance Method Details

#each_blog(last_update_from: nil, is_edited: nil, &block) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/pobo/client.rb', line 98

def each_blog(last_update_from: nil, is_edited: nil, &block)
  return enum_for(:each_blog, last_update_from: last_update_from, is_edited: is_edited) unless block_given?

  page = 1
  loop do
    response = get_blogs(page: page, per_page: MAX_BULK_ITEMS, last_update_from: last_update_from, is_edited: is_edited)
    response.data.each(&block)
    break unless response.more_pages?

    page += 1
  end
end

#each_category(last_update_from: nil, is_edited: nil, &block) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/pobo/client.rb', line 85

def each_category(last_update_from: nil, is_edited: nil, &block)
  return enum_for(:each_category, last_update_from: last_update_from, is_edited: is_edited) unless block_given?

  page = 1
  loop do
    response = get_categories(page: page, per_page: MAX_BULK_ITEMS, last_update_from: last_update_from, is_edited: is_edited)
    response.data.each(&block)
    break unless response.more_pages?

    page += 1
  end
end

#each_product(last_update_from: nil, is_edited: nil, &block) ⇒ Object

Iterator methods



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/pobo/client.rb', line 72

def each_product(last_update_from: nil, is_edited: nil, &block)
  return enum_for(:each_product, last_update_from: last_update_from, is_edited: is_edited) unless block_given?

  page = 1
  loop do
    response = get_products(page: page, per_page: MAX_BULK_ITEMS, last_update_from: last_update_from, is_edited: is_edited)
    response.data.each(&block)
    break unless response.more_pages?

    page += 1
  end
end

#get_blogs(page: nil, per_page: nil, last_update_from: nil, is_edited: nil) ⇒ Object



64
65
66
67
68
# File 'lib/pobo/client.rb', line 64

def get_blogs(page: nil, per_page: nil, last_update_from: nil, is_edited: nil)
  query = build_query_params(page, per_page, last_update_from, is_edited)
  response = request(:get, "/api/v2/rest/blogs#{query}")
  DTO::PaginatedResponse.from_hash(response, DTO::Blog)
end

#get_categories(page: nil, per_page: nil, last_update_from: nil, is_edited: nil) ⇒ Object



58
59
60
61
62
# File 'lib/pobo/client.rb', line 58

def get_categories(page: nil, per_page: nil, last_update_from: nil, is_edited: nil)
  query = build_query_params(page, per_page, last_update_from, is_edited)
  response = request(:get, "/api/v2/rest/categories#{query}")
  DTO::PaginatedResponse.from_hash(response, DTO::Category)
end

#get_products(page: nil, per_page: nil, last_update_from: nil, is_edited: nil) ⇒ Object

Export methods



52
53
54
55
56
# File 'lib/pobo/client.rb', line 52

def get_products(page: nil, per_page: nil, last_update_from: nil, is_edited: nil)
  query = build_query_params(page, per_page, last_update_from, is_edited)
  response = request(:get, "/api/v2/rest/products#{query}")
  DTO::PaginatedResponse.from_hash(response, DTO::Product)
end

#import_blogs(blogs) ⇒ Object



43
44
45
46
47
48
# File 'lib/pobo/client.rb', line 43

def import_blogs(blogs)
  validate_bulk_size!(blogs)
  payload = blogs.map { |b| b.respond_to?(:to_hash) ? b.to_hash : b }
  response = request(:post, "/api/v2/rest/blogs", payload)
  DTO::ImportResult.from_hash(response)
end

#import_categories(categories) ⇒ Object



29
30
31
32
33
34
# File 'lib/pobo/client.rb', line 29

def import_categories(categories)
  validate_bulk_size!(categories)
  payload = categories.map { |c| c.respond_to?(:to_hash) ? c.to_hash : c }
  response = request(:post, "/api/v2/rest/categories", payload)
  DTO::ImportResult.from_hash(response)
end

#import_parameters(parameters) ⇒ Object



36
37
38
39
40
41
# File 'lib/pobo/client.rb', line 36

def import_parameters(parameters)
  validate_bulk_size!(parameters)
  payload = parameters.map { |p| p.respond_to?(:to_hash) ? p.to_hash : p }
  response = request(:post, "/api/v2/rest/parameters", payload)
  DTO::ImportResult.from_hash(response)
end

#import_products(products) ⇒ Object

Import methods



22
23
24
25
26
27
# File 'lib/pobo/client.rb', line 22

def import_products(products)
  validate_bulk_size!(products)
  payload = products.map { |p| p.respond_to?(:to_hash) ? p.to_hash : p }
  response = request(:post, "/api/v2/rest/products", payload)
  DTO::ImportResult.from_hash(response)
end