Sha256: f55d38d488c498095bedcb129ad688e110ccbbdef16a9fbdf785a3171a07c0f5

Contents?: true

Size: 1.01 KB

Versions: 4

Compression:

Stored size: 1.01 KB

Contents

module Moip2
  class OrderApi
    attr_reader :client

    def initialize(client)
      @client = client
    end

    def base_path
      "/v2/orders"
    end

    def create(order)
      Resource::Order.new client, client.post(base_path, order)
    end

    def show(id)
      Resource::Order.new client, client.get("#{base_path}/#{id}")
    end

    def find_all(limit: nil, offset: nil, q: nil, filters: nil)
      encoded_filters = Moip2::Util::FiltersEncoder.encode(filters)

      # `URI.encode...` will accept nil params, but they will pollute the URI
      params = {
        limit: limit,
        offset: offset,
        q: q,
        filters: encoded_filters,
      }.reject { |_, value| value.nil? }

      query_string = URI.encode_www_form(params)
      path = "#{base_path}?#{query_string}"
      response = client.get(path)

      # We need to transform raw JSON in Order objects
      response.orders.collect! { |order| Resource::Order.new client, order }
      Resource::Order.new client, response
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
moip2-1.3.1 lib/moip2/order_api.rb
moip2-1.2.1 lib/moip2/order_api.rb
moip2-1.2.0 lib/moip2/order_api.rb
moip2-1.1.0 lib/moip2/order_api.rb