Module: Mercadolibre::Core::Questions

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

Instance Method Summary collapse

Instance Method Details

#add_user_to_questions_blacklist(seller_id, user_id) ⇒ Object


83
84
85
86
87
88
89
90
91
# File 'lib/mercadolibre/core/questions.rb', line 83

def add_user_to_questions_blacklist(seller_id, user_id)
  payload = { user_id: user_id }.to_json

  headers = { content_type: :json }

  url = "/users/#{seller_id}/questions_blacklist?access_token=#{@access_token}"

  post_request(url, payload, headers)[:status_code] == 200
end

#answer_question(question_id, text) ⇒ Object


68
69
70
71
72
73
74
75
76
# File 'lib/mercadolibre/core/questions.rb', line 68

def answer_question(question_id, text)
  payload = { question_id: question_id, text: text }.to_json

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

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

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

#ask_question(item_id, text) ⇒ Object


58
59
60
61
62
63
64
65
66
# File 'lib/mercadolibre/core/questions.rb', line 58

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

  headers = { content_type: :json }

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

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

#delete_question(question_id) ⇒ Object


52
53
54
55
56
# File 'lib/mercadolibre/core/questions.rb', line 52

def delete_question(question_id)
  result = delete_request("/questions/#{question_id}?access_token=#{@access_token}")

  result
end

#get_my_questions(filters = {}) ⇒ Object


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

def get_my_questions(filters={})
  filters.merge!({ access_token: @access_token })

  response = get_request('/my/received_questions/search', filters)[:body]

  {
    results: response['questions'].map { |r| Mercadolibre::Entity::Question.new(r) },
    paging: { total: response['total'], limit: response['limit'] }
  }
end

#get_my_questions_count(filters = {}) ⇒ Object


15
16
17
18
19
# File 'lib/mercadolibre/core/questions.rb', line 15

def get_my_questions_count(filters={})
  filters.merge!({ access_token: @access_token, limit: 1, offset: 0 })

  get_request('/my/received_questions/search', filters)[:body]['total'].to_i
end

#get_question(question_id) ⇒ Object


44
45
46
47
48
49
50
# File 'lib/mercadolibre/core/questions.rb', line 44

def get_question(question_id)
  filters = { access_token: @access_token }

  result = get_request("/questions/#{question_id}", filters)[:body]

  Mercadolibre::Entity::Question.new(result)
end

#get_questions(filters = {}) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mercadolibre/core/questions.rb', line 21

def get_questions(filters={})
  if @access_token.present?
    filters.merge!({ access_token: @access_token })
  end

  response = get_request('/questions/search', filters)[:body]

  {
    results: response['questions'].map { |r| Mercadolibre::Entity::Question.new(r) },
    paging: { total: response['total'], limit: response['limit'] }
  }
end

#get_questions_blacklist(seller_id) ⇒ Object


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

def get_questions_blacklist(seller_id)
  result = get_request("/users/#{seller_id}/questions_blacklist?access_token=#{@access_token}")
  result[:body].map { |r| r['user']['id'] }
end

#get_questions_count(filters = {}) ⇒ Object


34
35
36
37
38
39
40
41
42
# File 'lib/mercadolibre/core/questions.rb', line 34

def get_questions_count(filters={})
  if @access_token.present?
    filters.merge!({ access_token: @access_token })
  end

  filters.merge!({ limit: 1, offset: 0 })

  get_request('/questions/search', filters)[:body]['total'].to_i
end

#remove_user_from_questions_blacklist(seller_id, user_id) ⇒ Object


93
94
95
96
97
# File 'lib/mercadolibre/core/questions.rb', line 93

def remove_user_from_questions_blacklist(seller_id, user_id)
  url = "/users/#{seller_id}/questions_blacklist/#{user_id}?access_token=#{@access_token}"

  delete_request(url)[:status_code] == 200
end