Module: Mercadolibre::Core::ItemsAndSearches

Included in:
Api
Defined in:
lib/mercadolibre/core/items_and_searches.rb

Instance Method Summary collapse

Instance Method Details

#add_item_description(item_id, text) ⇒ Object


148
149
150
151
152
153
154
# File 'lib/mercadolibre/core/items_and_searches.rb', line 148

def add_item_description(item_id, text)
  payload = { text: text }.to_json

  headers = { content_type: :json, accept: :json }

  post_request("/items/#{item_id}/descriptions?access_token=#{@access_token}", payload, headers)[:body]
end

#create_item(attribs) ⇒ Object


112
113
114
115
116
117
118
119
120
# File 'lib/mercadolibre/core/items_and_searches.rb', line 112

def create_item(attribs)
  payload = attribs.to_json

  headers = { content_type: :json }

  result = post_request("/items?access_token=#{@access_token}", payload, headers)

  Mercadolibre::Entity::Item.new(result[:body])
end

#delete_item_attributes(item_id, attr_list) ⇒ Object


165
166
167
168
169
170
171
# File 'lib/mercadolibre/core/items_and_searches.rb', line 165

def delete_item_attributes(item_id, attr_list)
  payload = attr_list.map { |x| { id: x } }.to_json

  headers = { content_type: :json, accept: :json }

  put_request("/items/#{item_id}?access_token=#{@access_token}", payload, headers)[:status_code] == 200
end

84
85
86
87
88
# File 'lib/mercadolibre/core/items_and_searches.rb', line 84

def get_featured_items(site_id, pool_id)
  results = get_request("/sites/#{site_id}/featured_items/#{pool_id}")

  results[:body].map { |r| Mercadolibre::Entity::FeaturedItem.new(r) }
end

#get_hot_items(site_id, category_id, limit = 15) ⇒ Object


78
79
80
81
82
# File 'lib/mercadolibre/core/items_and_searches.rb', line 78

def get_hot_items(site_id, category_id, limit=15)
  results = get_request("/sites/#{site_id}/hot_items/search?category=#{category_id}&limit=#{limit}")

  results[:body].map { |r| Mercadolibre::Entity::Item.new(r) }
end

#get_item(item_id) ⇒ Object


35
36
37
38
39
# File 'lib/mercadolibre/core/items_and_searches.rb', line 35

def get_item(item_id)
  result = get_request("/items/#{item_id}")

  Mercadolibre::Entity::Item.new(result[:body])
end

#get_item_available_upgrades(item_id) ⇒ Object


67
68
69
70
71
# File 'lib/mercadolibre/core/items_and_searches.rb', line 67

def get_item_available_upgrades(item_id)
  results = get_request("/items/#{item_id}/[email protected]_token")

  results[:body].map { |r| Mercadolibre::Entity::ListingType.new(r) }
end

#get_item_description(item_id) ⇒ Object


55
56
57
58
59
# File 'lib/mercadolibre/core/items_and_searches.rb', line 55

def get_item_description(item_id)
  result = get_request("/items/#{item_id}/description")

  Mercadolibre::Entity::ItemDescription.new(result[:body])
end

#get_item_descriptions(item_id) ⇒ Object


61
62
63
64
65
# File 'lib/mercadolibre/core/items_and_searches.rb', line 61

def get_item_descriptions(item_id)
  results = get_request("/items/#{item_id}/descriptions")

  results[:body].map { |r| Mercadolibre::Entity::ItemDescription.new(r) }
end

#get_item_identifiers(item_id) ⇒ Object


181
182
183
184
185
# File 'lib/mercadolibre/core/items_and_searches.rb', line 181

def get_item_identifiers(item_id)
  result = get_request("/items/#{item_id}/product_identifiers")

  Mercadolibre::Entity::ProductIdentifier.new(result[:body])
end

#get_item_ids(filters = {}) ⇒ Object


17
18
19
20
21
22
23
24
# File 'lib/mercadolibre/core/items_and_searches.rb', line 17

def get_item_ids(filters={})
  response = get_request("/sites/#{@site}/search", filters)[:body]

  {
    results: response['results'].map { |r| r['id'] },
    paging: response['paging']
  }
end

#get_item_visits(item_ids) ⇒ Object


41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mercadolibre/core/items_and_searches.rb', line 41

def get_item_visits(item_ids)
  if item_ids.is_a? Array
    result = { }

    item_ids.each_slice(50) do |ids_group|
      result.merge!(get_request("/visits/items?ids=#{ids_group.join(',')}")[:body])
    end

    result
  else
    get_request("/visits/items?ids=#{item_ids}")[:body][item_ids]
  end
end

#get_my_item_ids(filters = {}) ⇒ Object


4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/mercadolibre/core/items_and_searches.rb', line 4

def get_my_item_ids(filters={})
  user_id = get_my_user.id

  filters.merge!({ access_token: @access_token, limit: 50, offset: 0 })

  response = get_request("/users/#{user_id}/items/search", filters)[:body]

  {
    results: response['results'],
    paging: response['paging']
  }
end

#get_search_url(site_id, q) ⇒ Object


73
74
75
76
# File 'lib/mercadolibre/core/items_and_searches.rb', line 73

def get_search_url(site_id, q)
  result = get_request("/sites/#{site_id}/searchUrl?q=#{q}")
  result[:body]['url']
end

90
91
92
93
94
95
96
97
98
99
100
# File 'lib/mercadolibre/core/items_and_searches.rb', line 90

def get_site_trends(site_id, category_id=nil)
  if category_id.present?
    params = { category: category_id }
  else
    params = { }
  end

  results = get_request("/sites/#{site_id}/trends/search", params)

  results[:body].map { |r| Mercadolibre::Entity::SiteTrend.new(r) }
end

#item_valid?(attribs) ⇒ Boolean

Returns:

  • (Boolean)

102
103
104
105
106
107
108
109
110
# File 'lib/mercadolibre/core/items_and_searches.rb', line 102

def item_valid?(attribs)
  payload = attribs.to_json

  headers = { content_type: :json }

  result = post_request("/items/validate?access_token=#{@access_token}", payload, headers)

  (result[:status_code].to_s == '204')
end

#relist_item(item_id, price, quantity, listing_type_id) ⇒ Object


122
123
124
125
126
127
128
129
130
# File 'lib/mercadolibre/core/items_and_searches.rb', line 122

def relist_item(item_id, price, quantity, listing_type_id)
  payload = { price: price, quantity: quantity, listing_type_id: listing_type_id }.to_json

  headers = { content_type: :json, accept: :json }

  result = post_request("/items/#{item_id}/relist?access_token=#{@access_token}", payload, headers)

  Mercadolibre::Entity::Item.new(result[:body])
end

#search_items(filters = {}) ⇒ Object


26
27
28
29
30
31
32
33
# File 'lib/mercadolibre/core/items_and_searches.rb', line 26

def search_items(filters={})
  response = get_request("/sites/#{@site}/search", filters)[:body]

  {
    results: response['results'].map { |x| Mercadolibre::Entity::Item.new(x) },
    paging: response['paging']
  }
end

#update_item_attributes(item_id, attrs) ⇒ Object


156
157
158
159
160
161
162
163
# File 'lib/mercadolibre/core/items_and_searches.rb', line 156

def update_item_attributes(item_id, attrs)
  payload = attrs.to_json

  headers = { content_type: :json, accept: :json }

  put_request("/items/#{item_id}?access_token=#{@access_token}", payload, headers)[:status_code] == 200

end

#update_item_fields(item_id, attribs) ⇒ Object


132
133
134
135
136
137
138
# File 'lib/mercadolibre/core/items_and_searches.rb', line 132

def update_item_fields(item_id, attribs)
  payload = attribs.to_json

  headers = { content_type: :json, accept: :json }

  put_request("/items/#{item_id}?access_token=#{@access_token}", payload, headers)[:body]
end

#update_item_identifiers(item_id, attrs) ⇒ Object


173
174
175
176
177
178
179
# File 'lib/mercadolibre/core/items_and_searches.rb', line 173

def update_item_identifiers(item_id, attrs)
  payload = attrs.to_json

  headers = { content_type: :json }

  put_request("/items/#{item_id}/product_identifiers?access_token=#{@access_token}", payload, headers)[:status_code] == 200
end

#update_item_listing_type(item_id, listing_type_id) ⇒ Object


140
141
142
143
144
145
146
# File 'lib/mercadolibre/core/items_and_searches.rb', line 140

def update_item_listing_type(item_id, listing_type_id)
  payload = { id: listing_type_id }.to_json

  headers = { content_type: :json, accept: :json }

  post_request("/items/#{item_id}/listing_type?access_token=#{@access_token}", payload, headers)[:body]
end