class Meli

This class is the actual library

Constants

API_ROOT_URL
AUTH_URL
OAUTH_URL
SDK_VERSION

Attributes

access_token[RW]
app_id[R]
https[R]
refresh_token[RW]
secret[R]

Public Class Methods

new( app_id = nil, secret = nil, access_token = nil, refresh_token = nil ) click to toggle source

constructor

# File lib/meli.rb, line 25
def initialize(
  app_id = nil,
  secret = nil,
  access_token = nil,
  refresh_token = nil
)
  @access_token = access_token
  @refresh_token = refresh_token
  @app_id = app_id
  @secret = secret
  api_url = URI.parse API_ROOT_URL
  @https = Net::HTTP.new(api_url.host, api_url.port)
  @https.use_ssl = true
  @https.verify_mode = OpenSSL::SSL::VERIFY_PEER
  @https.ssl_version = :TLSv1
end

Public Instance Methods

auth_url(redirect_uri) click to toggle source

AUTH METHODS

# File lib/meli.rb, line 43
def auth_url(redirect_uri)
  params = {
    client_id: @app_id,
    response_type: 'code',
    redirect_uri: redirect_uri
  }

  url = "#{AUTH_URL}?#{to_url_params(params)}"
end
authorize(code, redirect_uri) click to toggle source
# File lib/meli.rb, line 53
def authorize(code, redirect_uri)
  params = {
    grant_type: 'authorization_code',
    client_id: @app_id,
    client_secret: @secret,
    code: code,
    redirect_uri: redirect_uri
  }

  uri = make_path(OAUTH_URL, params)

  req = Net::HTTP::Post.new(uri.path)
  req['Accept'] = 'application/json'
  req['User-Agent'] = SDK_VERSION
  req['Content-Type'] = 'application/x-www-form-urlencoded'
  req.set_form_data(params)
  response = @https.request(req)

  case response
  when Net::HTTPSuccess
    response_info = JSON.parse response.body
    # convert hash keys to symbol
    response_info = Hash[response_info.map { |k, v| [k.to_sym, v] }]

    @access_token = response_info[:access_token]
    @refresh_token = if response_info.key?(:refresh_token)
                       response_info[:refresh_token]
                     else
                       '' # offline_access not set up
                     end
    @access_token
  else
    # response code isn't a 200; raise an exception
    response.error!
  end
end
delete(path, params = {}) click to toggle source
# File lib/meli.rb, line 156
def delete(path, params = {})
  uri = make_path(path, params)
  req = Net::HTTP::Delete.new("#{uri.path}?#{uri.query}")
  execute req
end
execute(req) click to toggle source

REQUEST METHODS

# File lib/meli.rb, line 127
def execute(req)
  req['Accept'] = 'application/json'
  req['User-Agent'] = SDK_VERSION
  req['Content-Type'] = 'application/json'
  response = @https.request(req)
end
get(path, params = {}) click to toggle source
# File lib/meli.rb, line 134
def get(path, params = {})
  uri = make_path(path, params)
  req = Net::HTTP::Get.new("#{uri.path}?#{uri.query}")
  execute req
end
options(path, params = {}) click to toggle source
# File lib/meli.rb, line 162
def options(path, params = {})
  uri = make_path(path, params)
  req = Net::HTTP::Options.new("#{uri.path}?#{uri.query}")
  execute req
end
post(path, body, params = {}) click to toggle source
# File lib/meli.rb, line 140
def post(path, body, params = {})
  uri = make_path(path, params)
  req = Net::HTTP::Post.new("#{uri.path}?#{uri.query}")
  req.set_form_data(params)
  req.body = body.to_json unless body.nil?
  execute req
end
put(path, body, params = {}) click to toggle source
# File lib/meli.rb, line 148
def put(path, body, params = {})
  uri = make_path(path, params)
  req = Net::HTTP::Put.new("#{uri.path}?#{uri.query}")
  req.set_form_data(params)
  req.body = body.to_json unless body.nil?
  execute req
end

Private Instance Methods

make_path(path, params = {}) click to toggle source
# File lib/meli.rb, line 174
def make_path(path, params = {})
  # Making Path and add a leading / if not exist
  unless path =~ /^http/
    path = "/#{path}" unless path =~ %r{ /^\// }
    path = "#{API_ROOT_URL}#{path}"
  end
  path = "#{path}?#{to_url_params(params)}" unless params.keys.empty?
  uri = URI.parse path
end
to_url_params(params) click to toggle source
# File lib/meli.rb, line 170
def to_url_params(params)
  URI.escape(params.collect { |k, v| "#{k}=#{v}" }.join('&'))
end