<commit_before>require 'httparty'

module Rush
  class Client
    OAUTH_URI = 'https://login.uber.com/oauth/v2/token'
    attr_accessor *Configuration::VALID_CONNECTION_KEYS

    def initialize(options={})
      merged_options = Rush.options.merge(options)

      # Copy the merged values to this client and ignore those
      # not part of our configuration
      Configuration::VALID_CONNECTION_KEYS.each do |key|
        send("#{key}=", merged_options[key])
      end

      get_access_token
    end

    def api_uri
      sandbox ? Configuration::API_SANDBOX_URI : Configuration::API_PRODUCTION_URI
    end

    def get_access_token
      return self if access_token
      data = {client_secret: client_secret,
              client_id: client_id,
              grant_type: 'client_credentials',
              scope: sandbox ? 'delivery_sandbox' : 'delivery'
      }
      response = HTTParty.post(OAUTH_URI, body: data)
      if response.success?
        self.access_token = JSON.parse(response.body)["access_token"]
        return self.access_token
      else
        raise "Could not retrieve access token: #{response.body}"
      end

    end

    def create_delivery(items, pickup, dropoff, quote_id=nil)
      body = {items: items, pickup: pickup.to_json, dropoff: dropoff.to_json}.to_json
      response = HTTParty.post(api_uri + 'deliveries', body: body, headers: { "Authorization" => "Bearer #{access_token}",
                                                                              'Content-Type' => 'application/json'})
      if response.success?
        return response.parsed_response
      else
        raise(response.parsed_response)
      end
    end

    def get_quotes(pickup, dropoff)
      body = {pickup: pickup.to_json, dropoff: dropoff.to_json}.to_json

      HTTParty.post(api_uri + 'deliveries/quote', body: body, headers: { "Authorization" => "Bearer #{access_token}", "Content-Type" => "application/json"})
    end

    def fetch_deliveries(offset=0)
      response = HTTParty.get(api_uri + 'deliveries?offset=' + offset.to_s, headers: { "Authorization" => "Bearer #{access_token}"})
      # Transforms json into a hash
      result = response.parsed_response["deliveries"].map {|p| p.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}}

      return result.map{|s| Delivery.new(s)}
    end

    def find_delivery(id)
      response = HTTParty.get(api_uri + 'deliveries/' + id.to_s, headers: { "Authorization" => "Bearer #{access_token}"})
      # Transforms json into a hash
      result = response.parsed_response.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}

      return Delivery.new(result)
    end

    def cancel_delivery(id)
      response = HTTParty.post(api_uri + "deliveries/#{id}/cancel", headers: { "Authorization" => "Bearer #{access_token}"})

      return response.success?
    end
  end
end
<commit_msg>handle request if wrong delivery id<commit_after>require 'httparty'

module Rush
  class Client
    OAUTH_URI = 'https://login.uber.com/oauth/v2/token'
    attr_accessor *Configuration::VALID_CONNECTION_KEYS

    def initialize(options={})
      merged_options = Rush.options.merge(options)

      # Copy the merged values to this client and ignore those
      # not part of our configuration
      Configuration::VALID_CONNECTION_KEYS.each do |key|
        send("#{key}=", merged_options[key])
      end

      get_access_token
    end

    def api_uri
      sandbox ? Configuration::API_SANDBOX_URI : Configuration::API_PRODUCTION_URI
    end

    def get_access_token
      return self if access_token
      data = {client_secret: client_secret,
              client_id: client_id,
              grant_type: 'client_credentials',
              scope: sandbox ? 'delivery_sandbox' : 'delivery'
      }
      response = HTTParty.post(OAUTH_URI, body: data)
      if response.success?
        self.access_token = JSON.parse(response.body)["access_token"]
        return self.access_token
      else
        raise "Could not retrieve access token: #{response.body}"
      end

    end

    def create_delivery(items, pickup, dropoff, quote_id=nil)
      body = {items: items, pickup: pickup.to_json, dropoff: dropoff.to_json}.to_json
      response = HTTParty.post(api_uri + 'deliveries', body: body, headers: { "Authorization" => "Bearer #{access_token}",
                                                                              'Content-Type' => 'application/json'})
      if response.success?
        return response.parsed_response
      else
        raise(response.parsed_response)
      end
    end

    def get_quotes(pickup, dropoff)
      body = {pickup: pickup.to_json, dropoff: dropoff.to_json}.to_json

      HTTParty.post(api_uri + 'deliveries/quote', body: body, headers: { "Authorization" => "Bearer #{access_token}", "Content-Type" => "application/json"})
    end

    def fetch_deliveries(offset=0)
      response = HTTParty.get(api_uri + 'deliveries?offset=' + offset.to_s, headers: { "Authorization" => "Bearer #{access_token}"})
      # Transforms json into a hash
      result = response.parsed_response["deliveries"].map {|p| p.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}}

      return result.map{|s| Delivery.new(s)}
    end

    def find_delivery(id)
      response = HTTParty.get(api_uri + 'deliveries/' + id.to_s, headers: { "Authorization" => "Bearer #{access_token}"})
      
      raise(response.parsed_response) unless response.success?
      
      # Transforms json into a hash
      result = response.parsed_response.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}

      return Delivery.new(result)
    end

    def cancel_delivery(id)
      response = HTTParty.post(api_uri + "deliveries/#{id}/cancel", headers: { "Authorization" => "Bearer #{access_token}"})

      return response.success?
    end
  end
end

{'fraction_non_alphanumeric': 0.11672683513838748, 'fraction_numerical': 0.0006876396768093519, 'mean_word_length': 2.8812541694462976, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5678679', 'n_tokens_mistral': 1823, 'n_tokens_neox': 1775, 'n_words': 394}