Sha256: b0612b53c4defd5081d93a591aeae7fe6ffd2c71e9a4f3002a5a75aeaea62c7d

Contents?: true

Size: 1.56 KB

Versions: 5

Compression:

Stored size: 1.56 KB

Contents

module Base
  module Apis
    class Search
      attr_reader   :client_secret
      attr_accessor :request_parameters, :uri

      def initialize(client_secret)
        @client_secret = client_secret
      end

      def request(query, &block)
        send(:request_parameters=, query, &block)
        send(:uri=, request_parameters)
        request = Net::HTTP::Get.new(uri)

        Net::HTTP.start(
          uri.hostname,
          uri.port,
          use_ssl: uri.scheme == 'https') do |http|
            http.request(request)
          end
      end

      private

      def request_parameters=(query)
        # https://github.com/baseinc/api-docs/blob/master/base_api_v1_search.md
        #
        # { client_id:     'client id for search',
        #   client_secret: 'client secret for search',
        #   q:             'search words delimited by space',
        #   sort:          'order_count desc, stock desc, price asc',
        #   start:         0,
        #   size:          10,
        #   fields:        'shop_name, title, detail',
        #   shop_id:       '' }
        given_parameters = block_given? ? yield : {}
        default =
          { client_id:     @client_secret.search_client_id,
            client_secret: @client_secret.search_client_secret }

        @request_parameters =
          default.merge(given_parameters).merge(q: query)
      end

      def uri=(request_parameters)
        @uri =
          URI([
            API_HOST,
            "/#{API_VERSION}/search?",
            URI.encode_www_form(request_parameters)].join)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
base-api-client-0.2.4.beta lib/base/apis/search.rb
base-api-client-0.2.3.beta lib/base/apis/search.rb
base-api-client-0.2.2.beta lib/base/apis/search.rb
base-api-client-0.2.1.beta lib/base/apis/search.rb
base-api-client-0.1.beta lib/base/apis/search.rb