module OBarc::Api

Constants

DEFAULT_TIMEOUT
VALID_ACTIONS

Public Instance Methods

get_chat_messages(chat_messages, options = {}) click to toggle source

GET api/v1/get_chat_messages

# File lib/obarc/api.rb, line 126
def get_chat_messages(chat_messages, options = {})
  guid = chat_messages[:guid]
  
  if guid.nil? || guid.size != 40
    raise Utils::Exceptions::InvalidArgumentError, guid: "must be present, 40 characters (was: #{guid.inspect})"
  end
  
  url = "#{build_base_url(options)}/get_chat_messages"
  execute method: :get, url: url,
    headers: build_headers(options).merge(params: chat_messages.compact),
    verify_ssl: build_verify_ssl(options)
end
get_contracts(contracts, options = {}) click to toggle source

GET api/v1/contracts

# File lib/obarc/api.rb, line 109
def get_contracts(contracts, options = {})
  id = contracts[:id]
  guid = contracts[:guid]
  
  if !!id && id.size != 40
    raise Utils::Exceptions::InvalidArgumentError, id: "must be 40 characters, if present (was: #{id.size})"
  elsif !!guid && guid.size != 40
    raise Utils::Exceptions::InvalidArgumentError, guid: "must be 40 characters, if present (was: #{guid.size})"
  end
  
  url = "#{build_base_url(options)}/contracts"
  execute method: :get, url: url,
    headers: build_headers(options).merge(params: contracts.compact),
    verify_ssl: build_verify_ssl(options)
end
get_order(order, options = {}) click to toggle source

GET api/v1/get_order

# File lib/obarc/api.rb, line 140
def get_order(order, options = {})
  order_id = order[:order_id]
  
  if order_id.nil?
    raise Utils::Exceptions::InvalidArgumentError, order_id: 'must be present'
  end
  
  url = "#{build_base_url(options)}/get_order"
  execute method: :get, url: url,
    headers: build_headers(options).merge(params: order.compact),
    verify_ssl: build_verify_ssl(options)
end
method_missing(m, *args, &block) click to toggle source
Calls superclass method
# File lib/obarc/api.rb, line 55
def method_missing(m, *args, &block)
  super unless respond_to_missing?(m)
  
  # Many of the calls to restapi.py are uniform enough for DRY code, but the
  # ones that aren't are mapped here.
  
  rest_method, endpoint, params, options = case m
  when :get_image then [:get, 'get_image', args[0], args[1]]
  when :get_listings then [:get, 'get_listings', args[0], args[1]]
  when :get_followers then [:get, 'get_followers', args[0], args[1]]
  when :get_following then [:get, 'get_following', args[0], args[1]]
  when :post_contract then [:post, 'contracts', args[0], args[1]]
  when :delete_contract then [:delete, 'contracts', args[0], args[1]]
  when :get_notifications then [:get, 'get_notifications', nil, args[0]]
  when :get_chat_conversations then [:get, 'get_chat_conversations', nil, args[0]]
  when :get_sales then [:get, 'get_sales', nil, args[0]]
  when :get_purchases then [:get, 'get_purchases', nil, args[0]]
  when :get_cases then [:get, 'get_cases', nil, args[0]]
  when :get_ratings then [:get, 'get_ratings', args[0], args[1]]
  else
    verb = m.to_s.split('_')
    a = [verb[0].to_sym, verb[1..-1].join('_')]
    a << if args.size == 1
      nil
    else
      args[0]
    end
    a << args[args.size - 1]
  end
  
  url = "#{build_base_url(options)}/#{endpoint}" if !!endpoint && !!options
  if !!rest_method && !!url && !!options
    headers = build_headers(options)
    headers = headers.merge(params: params.compact) if !!params
    return execute method: rest_method, url: url, headers: headers,
      verify_ssl: build_verify_ssl(options)
  end
      
  raise Utils::Exceptions::OBarcError, "Did not handle #{m} as expected, arguments: #{args}"
end
ping(options = {}) click to toggle source
# File lib/obarc/api.rb, line 38
def ping(options = {})
  execute(method: :get,
    url: "#{build_base_url(options)}/connected_peers?_=#{Time.now.to_i}",
    headers: build_headers(options), verify_ssl: build_verify_ssl(options))
end
post_login(options = {}) click to toggle source

POST api/v1/login

# File lib/obarc/api.rb, line 28
def post_login(options = {})
  url = "#{build_base_url(options)}/login"
  auth = build_authentication(options)
  
  raise Utils::Exceptions::MissingArgumentError, [:username, :password] if auth.empty?

  execute method: :post, url: url, headers: {params: auth},
    verify_ssl: build_verify_ssl(options)
end
post_upload_image(options = {}) click to toggle source

POST api/v1/upload_image

# File lib/obarc/api.rb, line 97
def post_upload_image(options = {})
  elements = [:image, :avatar, :header]
  params = options.slice(*elements)
  options = options.delete_if { |k, v| elements.include? k }
  
  url = "#{build_base_url(options)}/upload_images"
  execute method: :post, url: url,
    headers: build_headers(options).merge(params: params.compact),
    verify_ssl: build_verify_ssl(options)
end
respond_to_missing?(m, include_private = false) click to toggle source
# File lib/obarc/api.rb, line 44
def respond_to_missing?(m, include_private = false)
  verb = m.to_s.split('_')
  rest_method = verb[0].to_sym
  return false unless VALID_ACTIONS.keys.include?(rest_method)
  
  endpoint = verb[1..-1].join('_')
  return false if endpoint.nil?
  
  VALID_ACTIONS[rest_method].include?(endpoint.to_sym)
end

Private Instance Methods

build_authentication(options = {}) click to toggle source
# File lib/obarc/api.rb, line 186
def build_authentication(options = {})
  if options.kind_of? Session
    {username: options.username, password: options.password}
  else
    options.slice(:username, :password)
  end.compact
end
build_base_url(options = {}) click to toggle source
# File lib/obarc/api.rb, line 194
def build_base_url(options = {})
  if options.kind_of? Session
    options.base_url
  elsif options.kind_of? Hash
    options[:base_url]
  else
    raise Utils::Exceptions::OBarcError, "Unable to build base URL using: #{options.inspect}, expected a OBarc::Session or Hash."
  end
end
build_headers(options = {}) click to toggle source
# File lib/obarc/api.rb, line 204
def build_headers(options = {})
  if options.kind_of? Session
    {cookies: options.cookies}
  else
    options.slice(:cookies)
  end
end
build_verify_ssl(options = {}) click to toggle source
# File lib/obarc/api.rb, line 212
def build_verify_ssl(options = {})
  if options.kind_of? Session
    !!options.verify_ssl
  else
    !!options[:verify_ssl]
  end
end
execute(options = {}) click to toggle source
# File lib/obarc/api.rb, line 153
def execute(options = {})
  if options[:method] == :post
    options[:headers][:content_type] = 'application/x-www-form-urlencoded'
  end
    
  if !!options[:headers][:params]
    params = options[:headers].delete(:params)
    
    if params.values.map(&:class).include? Array
      # Dropping to a lower level for parameters since they're more
      # complicated due to the presense of an Array.  Note that these
      # parameters go # outside the header.
      options[:url] += "?#{URI::encode_www_form params}"
    elsif params.values.map(&:class).any? { |c| [Tempfile, File, StringIO].include?(c) }
      # Handling (possibly) large files.
      options[:payload] = {multipart: true}
      params.each do |k, v|
        if v.respond_to? :read
          options[:payload][k] = Base64.strict_encode64(v.read)
        else
          options[:payload][k] = v
          # FIXME Might want to warn that we are possibly mixing multipart
          # with simple payload.
        end
      end
    else
      options[:headers][:params] = params
    end
  end
  
  RestClient::Request.execute(options.merge(timeout: DEFAULT_TIMEOUT))
end