diff options
| author | Luc Donnet | 2015-03-23 21:12:56 +0100 | 
|---|---|---|
| committer | Luc Donnet | 2015-03-23 21:12:56 +0100 | 
| commit | fa7e745459aefd64086869882fcca73f948b46fa (patch) | |
| tree | a5df17d4498b4ca612f6398156a667d2593cc76e /lib/ievkit | |
| parent | 0740decc6a2c5117d1dc89e3665774460626f86b (diff) | |
| download | chouette-core-fa7e745459aefd64086869882fcca73f948b46fa.tar.bz2 | |
Change ruby client for iev server
Diffstat (limited to 'lib/ievkit')
| -rw-r--r-- | lib/ievkit/arguments.rb | 14 | ||||
| -rw-r--r-- | lib/ievkit/authentication.rb | 70 | ||||
| -rw-r--r-- | lib/ievkit/client.rb | 307 | ||||
| -rw-r--r-- | lib/ievkit/client/jobs.rb | 49 | ||||
| -rw-r--r-- | lib/ievkit/configurable.rb | 85 | ||||
| -rw-r--r-- | lib/ievkit/default.rb | 149 | ||||
| -rw-r--r-- | lib/ievkit/error.rb | 241 | ||||
| -rw-r--r-- | lib/ievkit/response/raise_error.rb | 28 | ||||
| -rw-r--r-- | lib/ievkit/version.rb | 17 | 
9 files changed, 960 insertions, 0 deletions
| diff --git a/lib/ievkit/arguments.rb b/lib/ievkit/arguments.rb new file mode 100644 index 000000000..7f512eb25 --- /dev/null +++ b/lib/ievkit/arguments.rb @@ -0,0 +1,14 @@ +module Ievkit + +  # Extracts options from method arguments +  # @private +  class Arguments < Array +    attr_reader :options + +    def initialize(args) +      @options = args.last.is_a?(::Hash) ? args.pop : {} +      super(args) +    end + +  end +end diff --git a/lib/ievkit/authentication.rb b/lib/ievkit/authentication.rb new file mode 100644 index 000000000..666fcc8d8 --- /dev/null +++ b/lib/ievkit/authentication.rb @@ -0,0 +1,70 @@ +module Ievkit + +  # Authentication methods for {Octokit::Client} +  module Authentication + +    # Indicates if the client was supplied  Basic Auth +    # username and password +    # +    # @return [Boolean] +    def basic_authenticated? +      !!(@login && @password) +    end + +    # Indicates if the client was supplied an OAuth +    # access token +    # +    # @return [Boolean] +    def token_authenticated? +      !!@access_token +    end + +    # Indicates if the client was supplied an OAuth +    # access token or Basic Auth username and password +    # +    # @return [Boolean] +    def user_authenticated? +      basic_authenticated? || token_authenticated? +    end + +    # Indicates if the client has OAuth Application +    # client_id and secret credentials to make anonymous +    # requests at a higher rate limit +    # +    # @return Boolean +    def application_authenticated? +      !!application_authentication +    end + +    private + +    def application_authentication +      if @client_id && @client_secret +        { +          :client_id     => @client_id, +          :client_secret => @client_secret +        } +      end +    end + +    def login_from_netrc +      return unless netrc? + +      require 'netrc' +      info = Netrc.read netrc_file +      netrc_host = URI.parse(api_endpoint).host +      creds = info[netrc_host] +      if creds.nil? +        # creds will be nil if there is no netrc for this end point +        ievkit_warn "Error loading credentials from netrc file for #{api_endpoint}" +      else +        creds = creds.to_a +        self.login = creds.shift +        self.password = creds.shift +      end +    rescue LoadError +      ievkit_warn "Please install netrc gem for .netrc support" +    end + +  end +end diff --git a/lib/ievkit/client.rb b/lib/ievkit/client.rb new file mode 100644 index 000000000..53eb61074 --- /dev/null +++ b/lib/ievkit/client.rb @@ -0,0 +1,307 @@ +require 'sawyer' +require 'ievkit/arguments' +require 'ievkit/configurable' +require 'ievkit/client/jobs' + +module Ievkit + +  # Client for the Iev API   +  class Client +    +    include Ievkit::Configurable +    include Ievkit::Authentication +    include Ievkit::Client::Jobs + +    # Header keys that can be passed in options hash to {#get},{#head} +    CONVENIENCE_HEADERS = Set.new([:accept, :content_type]) + +    def initialize(options = {}) +      # Use options passed in, but fall back to module defaults +      Ievkit::Configurable.keys.each do |key| +        instance_variable_set(:"@#{key}", options[key] || Ievkit.instance_variable_get(:"@#{key}")) +      end +    end + +    # Compares client options to a Hash of requested options +    # +    # @param opts [Hash] Options to compare with current client options +    # @return [Boolean] +    def same_options?(opts) +      opts.hash == options.hash +    end + +    # Text representation of the client, masking tokens and passwords +    # +    # @return [String] +    def inspect +      inspected = super + +      # mask password +      inspected = inspected.gsub! @password, "*******" if @password +      # Only show last 4 of token, secret +      if @access_token +        inspected = inspected.gsub! @access_token, "#{'*'*36}#{@access_token[36..-1]}" +      end +      if @client_secret +        inspected = inspected.gsub! @client_secret, "#{'*'*36}#{@client_secret[36..-1]}" +      end + +      inspected +    end + +    # Make a HTTP GET request +    # +    # @param url [String] The path, relative to {#api_endpoint} +    # @param options [Hash] Query and header params for request +    # @return [Sawyer::Resource] +    def get(url, options = {}) +      request :get, url, parse_query_and_convenience_headers(options) +    end + +    # Make a HTTP POST request +    # +    # @param url [String] The path, relative to {#api_endpoint} +    # @param options [Hash] Body and header params for request +    # @return [Sawyer::Resource] +    def post(url, options = {}) +      request :post, url, options +    end + +    # Make a HTTP PUT request +    # +    # @param url [String] The path, relative to {#api_endpoint} +    # @param options [Hash] Body and header params for request +    # @return [Sawyer::Resource] +    def put(url, options = {}) +      request :put, url, options +    end + +    # Make a HTTP PATCH request +    # +    # @param url [String] The path, relative to {#api_endpoint} +    # @param options [Hash] Body and header params for request +    # @return [Sawyer::Resource] +    def patch(url, options = {}) +      request :patch, url, options +    end + +    # Make a HTTP DELETE request +    # +    # @param url [String] The path, relative to {#api_endpoint} +    # @param options [Hash] Query and header params for request +    # @return [Sawyer::Resource] +    def delete(url, options = {}) +      request :delete, url, options +    end + +    # Make a HTTP HEAD request +    # +    # @param url [String] The path, relative to {#api_endpoint} +    # @param options [Hash] Query and header params for request +    # @return [Sawyer::Resource] +    def head(url, options = {}) +      request :head, url, parse_query_and_convenience_headers(options) +    end + +    # Make one or more HTTP GET requests, optionally fetching +    # the next page of results from URL in Link response header based +    # on value in {#auto_paginate}. +    # +    # @param url [String] The path, relative to {#api_endpoint} +    # @param options [Hash] Query and header params for request +    # @param block [Block] Block to perform the data concatination of the +    #   multiple requests. The block is called with two parameters, the first +    #   contains the contents of the requests so far and the second parameter +    #   contains the latest response. +    # @return [Sawyer::Resource] +    def paginate(url, options = {}, &block) +      opts = parse_query_and_convenience_headers(options.dup) +      if @auto_paginate || @per_page +        opts[:query][:per_page] ||=  @per_page || (@auto_paginate ? 100 : nil) +      end + +      data = request(:get, url, opts) + +      if @auto_paginate +        while @last_response.rels[:next] && rate_limit.remaining > 0 +          @last_response = @last_response.rels[:next].get +          if block_given? +            yield(data, @last_response) +          else +            data.concat(@last_response.data) if @last_response.data.is_a?(Array) +          end +        end + +      end + +      data +    end + +    # Hypermedia agent for the Iev API +    # +    # @return [Sawyer::Agent] +    def agent +      @agent ||= Sawyer::Agent.new(api_endpoint, sawyer_options) do |http| +        http.headers[:accept] = default_media_type +        http.headers[:content_type] = "application/json" +        http.headers[:user_agent] = user_agent +         +        # Activate if authentication is needed +        # +        # if basic_authenticated? +        #   http.basic_auth(@login, @password) +        # elsif token_authenticated? +        #   http.authorization 'token', @access_token +        # elsif application_authenticated? +        #   http.params = http.params.merge application_authentication +        # end +      end +    end + +    # Fetch the root resource for the API +    # +    # @return [Sawyer::Resource] +    def root +      get "/" +    end + +    # Response for last HTTP request +    # +    # @return [Sawyer::Response] +    def last_response +      @last_response if defined? @last_response +    end + +    # Duplicate client using client_id and client_secret as +    # Basic Authentication credentials. +    # @example +    #   Ievkit.client_id = "foo" +    #   Ievkit.client_secret = "bar" +    # +    #   # GET https://api.github.com/?client_id=foo&client_secret=bar +    #   Ievkit.get "/" +    # +    #   Ievkit.client.as_app do |client| +    #     # GET https://foo:bar@api.github.com/ +    #     client.get "/" +    #   end +    def as_app(key = client_id, secret = client_secret, &block) +      if key.to_s.empty? || secret.to_s.empty? +        raise ApplicationCredentialsRequired, "client_id and client_secret required" +      end +      app_client = self.dup +      app_client.client_id = app_client.client_secret = nil +      app_client.login    = key +      app_client.password = secret + +      yield app_client if block_given? +    end + +    # Set username for authentication +    # +    # @param value [String] GitHub username +    def login=(value) +      reset_agent +      @login = value +    end + +    # Set password for authentication +    # +    # @param value [String] GitHub password +    def password=(value) +      reset_agent +      @password = value +    end + +    # Set OAuth access token for authentication +    # +    # @param value [String] 40 character GitHub OAuth access token +    def access_token=(value) +      reset_agent +      @access_token = value +    end + +    # Set OAuth app client_id +    # +    # @param value [String] 20 character GitHub OAuth app client_id +    def client_id=(value) +      reset_agent +      @client_id = value +    end + +    # Set OAuth app client_secret +    # +    # @param value [String] 40 character GitHub OAuth app client_secret +    def client_secret=(value) +      reset_agent +      @client_secret = value +    end + +    # Wrapper around Kernel#warn to print warnings unless +    # IEVKIT_SILENT is set to true. +    # +    # @return [nil] +    def ievkit_warn(*message) +      unless ENV['IEVKIT_SILENT'] +        warn message +      end +    end + +    private + +    def reset_agent +      @agent = nil +    end + +    def request(method, path, data, options = {}) +      if data.is_a?(Hash) +        options[:query]   = data.delete(:query) || {} +        options[:headers] = data.delete(:headers) || {} +        if accept = data.delete(:accept) +          options[:headers][:accept] = accept +        end +      end + +      @last_response = response = agent.call(method, URI::Parser.new.escape(path.to_s), data, options) +      response.data +    end + +    # Executes the request, checking if it was successful +    # +    # @return [Boolean] True on success, false otherwise +    def boolean_from_response(method, path, options = {}) +      request(method, path, options) +      @last_response.status == 204 +    rescue Ievkit::NotFound +      false +    end + + +    def sawyer_options +      opts = { +        :links_parser => Sawyer::LinkParsers::Hal.new +      } +      conn_opts = @connection_options +      conn_opts[:builder] = @middleware if @middleware +      conn_opts[:proxy] = @proxy if @proxy +      opts[:faraday] = Faraday.new(conn_opts) + +      opts +    end + +    def parse_query_and_convenience_headers(options) +      headers = options.fetch(:headers, {}) +      CONVENIENCE_HEADERS.each do |h| +        if header = options.delete(h) +          headers[h] = header +        end +      end +      query = options.delete(:query) +      opts = {:query => options} +      opts[:query].merge!(query) if query && query.is_a?(Hash) +      opts[:headers] = headers unless headers.empty? + +      opts +    end +  end +end diff --git a/lib/ievkit/client/jobs.rb b/lib/ievkit/client/jobs.rb new file mode 100644 index 000000000..a530b2527 --- /dev/null +++ b/lib/ievkit/client/jobs.rb @@ -0,0 +1,49 @@ +module Ievkit +  class Client +    module Jobs + +      # List jobs for a referential +      # +      # @param referential [String] Data referential name.  +      # @return [Array<Sawyer::Resource>] A list of jobs +      # @example Fetch all jobs for referential test +      #   client.jobs("test") +      def jobs(referential, options = {}) +        paginate "referentials/#{referential}/jobs", options +      end +       +      # Get scheduled job +      # +      # @param referential [String] Data referential name. +      # @param job_id [Integer] Id of the scheduled job. +      # @return [Sawyer::Resource] Hash representing scheduled job. +      # @example +      #   client.scheduled_job('test', 1451398) +      def scheduled_job(referential, job_id, options = {}) +        get "referentials/#{referential}/scheduled_jobs/#{job_id}", options +      end + +      # Get terminated job +      # +      # @param referential [String] Data referential name. +      # @param job_id [Integer] Id of the terminated job. +      # @return [Sawyer::Resource] Hash representing terminated job. +      # @example +      #   client.terminated_job('test', 1451399) +      def terminated_job(referential, job_id, options = {}) +        get "referentials/#{referential}/terminated_jobs/#{job_id}", options +      end + +      # Create job  +      # +      # @param referential [String] Data referential name. +      # @return [Sawyer::Resource] Hash representing the new job. +      # @example +      #   client.create_job("test",....) +      def create_job(referential, options = {}) +        post "jobs", options +      end +       +    end +  end +end diff --git a/lib/ievkit/configurable.rb b/lib/ievkit/configurable.rb new file mode 100644 index 000000000..fb1d9f787 --- /dev/null +++ b/lib/ievkit/configurable.rb @@ -0,0 +1,85 @@ +module Ievkit + +  # Configuration options for {Client}, defaulting to values +  # in {Default} +  module Configurable + +    attr_accessor :access_token, :auto_paginate, :client_id, +                  :client_secret, :default_media_type, :connection_options, +                  :middleware, :netrc, :netrc_file, +                  :per_page, :proxy, :user_agent +    attr_writer :password, :web_endpoint, :api_endpoint, :login + +    class << self + +      # List of configurable keys for {Octokit::Client} +      # @return [Array] of option keys +      def keys +        @keys ||= [ +          :access_token, +          :api_endpoint, +          :auto_paginate, +          :client_id, +          :client_secret, +          :connection_options, +          :default_media_type, +          :login, +          :middleware, +          :netrc, +          :netrc_file, +          :per_page, +          :password, +          :proxy, +          :user_agent, +          :web_endpoint +        ] +      end +    end + +    # Set configuration options using a block +    def configure +      yield self +    end + +    # Reset configuration options to default values +    def reset! +      Ievkit::Configurable.keys.each do |key| +        instance_variable_set(:"@#{key}", Ievkit::Default.options[key]) +      end +      self +    end +    alias setup reset! + +    def api_endpoint +      File.join(@api_endpoint, "") +    end + +    # Base URL for generated web URLs +    # +    # @return [String] Default: https://github.com/ +    def web_endpoint +      File.join(@web_endpoint, "") +    end + +    def login +      @login ||= begin +        user.login if token_authenticated? +      end +    end + +    def netrc? +      !!@netrc +    end + +    private + +    def options +      Hash[Ievkit::Configurable.keys.map{|key| [key, instance_variable_get(:"@#{key}")]}] +    end + +    def fetch_client_id_and_secret(overrides = {}) +      opts = options.merge(overrides) +      opts.values_at :client_id, :client_secret +    end +  end +end diff --git a/lib/ievkit/default.rb b/lib/ievkit/default.rb new file mode 100644 index 000000000..af0df422f --- /dev/null +++ b/lib/ievkit/default.rb @@ -0,0 +1,149 @@ +require 'ievkit/response/raise_error' +require 'ievkit/version' + +module Ievkit + +  # Default configuration options for {Client} +  module Default + +    # Default API endpoint +    API_ENDPOINT = "http://localhost:8080/chouette_iev/".freeze + +    # Default User Agent header string +    USER_AGENT   = "Ievkit Ruby Gem #{Ievkit::VERSION}".freeze + +    # Default media type +    MEDIA_TYPE   =  "" # "application/vnd.iev.v1.0+json".freeze + +    # Default WEB endpoint +    WEB_ENDPOINT = "http://localhost:3000".freeze + +    # Default page sie +    PER_PAGE = 12 + +    # In Faraday 0.9, Faraday::Builder was renamed to Faraday::RackBuilder +    RACK_BUILDER_CLASS = defined?(Faraday::RackBuilder) ? Faraday::RackBuilder : Faraday::Builder + +    # Default Faraday middleware stack +    MIDDLEWARE = RACK_BUILDER_CLASS.new do |builder| +      builder.use Ievkit::Response::RaiseError +      builder.use Faraday::Request::Multipart +      builder.use FaradayMiddleware::FollowRedirects +      builder.adapter Faraday.default_adapter +    end + +    class << self + +      # Configuration options +      # @return [Hash] +      def options +        Hash[Ievkit::Configurable.keys.map{|key| [key, send(key)]}] +      end + +      # Default access token from ENV +      # @return [String] +      def access_token +        ENV['IEVKIT_ACCESS_TOKEN'] +      end + +      # Default API endpoint from ENV or {API_ENDPOINT} +      # @return [String] +      def api_endpoint +        ENV['IEVKIT_API_ENDPOINT'] || API_ENDPOINT +      end + +      # Default pagination preference from ENV +      # @return [String] +      def auto_paginate +        ENV['IEVKIT_AUTO_PAGINATE'] +      end + +      # Default OAuth app key from ENV +      # @return [String] +      def client_id +        ENV['IEVKIT_CLIENT_ID'] +      end + +      # Default OAuth app secret from ENV +      # @return [String] +      def client_secret +        ENV['IEVKIT_SECRET'] +      end + +      # Default options for Faraday::Connection +      # @return [Hash] +      def connection_options +        { +          :headers => { +            :accept => default_media_type, +            :user_agent => user_agent +          } +        } +      end + +      # Default media type from ENV or {MEDIA_TYPE} +      # @return [String] +      def default_media_type +        ENV['IEVKIT_DEFAULT_MEDIA_TYPE'] || MEDIA_TYPE +      end + +      # Default Iev username for Basic Auth from ENV +      # @return [String] +      def login +        ENV['IEVKIT_LOGIN'] +      end + +      # Default middleware stack for Faraday::Connection +      # from {MIDDLEWARE} +      # @return [String] +      def middleware +        MIDDLEWARE +      end + +      # Default Iev password for Basic Auth from ENV +      # @return [String] +      def password +        ENV['IEVKIT_PASSWORD'] +      end + +      # Default pagination page size from ENV +      # @return [Fixnum] Page size +      def per_page +        page_size = ENV['IEVKIT_PER_PAGE'] || PER_PAGE + +        page_size.to_i if page_size +      end + +      # Default proxy server URI for Faraday connection from ENV +      # @return [String] +      def proxy +        ENV['IEVKIT_PROXY'] +      end + +      # Default User-Agent header string from ENV or {USER_AGENT} +      # @return [String] +      def user_agent +        ENV['IEVKIT_USER_AGENT'] || USER_AGENT +      end + +      # Default web endpoint from ENV or {WEB_ENDPOINT} +      # @return [String] +      def web_endpoint +        ENV['IEVKIT_WEB_ENDPOINT'] || WEB_ENDPOINT +      end + +      # Default behavior for reading .netrc file +      # @return [Boolean] +      def netrc +        ENV['IEVKIT_NETRC'] || false +      end + +      # Default path for .netrc file +      # @return [String] +      def netrc_file +        ENV['IEVKIT_NETRC_FILE'] || File.join(ENV['HOME'].to_s, '.netrc') +      end + +    end +  end +end diff --git a/lib/ievkit/error.rb b/lib/ievkit/error.rb new file mode 100644 index 000000000..593ed25a3 --- /dev/null +++ b/lib/ievkit/error.rb @@ -0,0 +1,241 @@ +module Ievkit +  # Custom error class for rescuing from all GitHub errors +  class Error < StandardError + +    # Returns the appropriate Ievkit::Error subclass based +    # on status and response message +    # +    # @param [Hash] response HTTP response +    # @return [Ievkit::Error] +    def self.from_response(response) +      status  = response[:status].to_i +      body    = response[:body].to_s +      headers = response[:response_headers] + +      if klass =  case status +                  when 400      then Ievkit::BadRequest +                  when 401      then error_for_401(headers) +                  when 403      then error_for_403(body) +                  when 404      then Ievkit::NotFound +                  when 405      then Ievkit::MethodNotAllowed +                  when 406      then Ievkit::NotAcceptable +                  when 409      then Ievkit::Conflict +                  when 415      then Ievkit::UnsupportedMediaType +                  when 422      then Ievkit::UnprocessableEntity +                  when 400..499 then Ievkit::ClientError +                  when 500      then Ievkit::InternalServerError +                  when 501      then Ievkit::NotImplemented +                  when 502      then Ievkit::BadGateway +                  when 503      then Ievkit::ServiceUnavailable +                  when 500..599 then Ievkit::ServerError +                  end +        klass.new(response) +      end +    end + +    def initialize(response=nil) +      @response = response +      super(build_error_message) +    end + +    # Documentation URL returned by the API for some errors +    # +    # @return [String] +    def documentation_url +      data[:documentation_url] if data.is_a? Hash +    end + +    # Returns most appropriate error for 401 HTTP status code +    # @private +    def self.error_for_401(headers) +      if Ievkit::OneTimePasswordRequired.required_header(headers) +        Ievkit::OneTimePasswordRequired +      else +        Ievkit::Unauthorized +      end +    end + +    # Returns most appropriate error for 403 HTTP status code +    # @private +    def self.error_for_403(body) +      if body =~ /rate limit exceeded/i +        Ievkit::TooManyRequests +      elsif body =~ /login attempts exceeded/i +        Ievkit::TooManyLoginAttempts +      elsif body =~ /abuse/i +        Ievkit::AbuseDetected +      elsif body =~ /repository access blocked/i +        Ievkit::RepositoryUnavailable +      else +        Ievkit::Forbidden +      end +    end + +    # Array of validation errors +    # @return [Array<Hash>] Error info +    def errors +      if data && data.is_a?(Hash) +        data[:errors] || [] +      else +        [] +      end +    end + +    private + +    def data +      @data ||= +        if (body = @response[:body]) && !body.empty? +          if body.is_a?(String) && +            @response[:response_headers] && +            @response[:response_headers][:content_type] =~ /json/ + +            Sawyer::Agent.serializer.decode(body) +          else +            body +          end +        else +          nil +        end +    end + +    def response_message +      case data +      when Hash +        data[:message] +      when String +        data +      end +    end + +    def response_error +      "Error: #{data[:error]}" if data.is_a?(Hash) && data[:error] +    end + +    def response_error_summary +      return nil unless data.is_a?(Hash) && !Array(data[:errors]).empty? + +      summary = "\nError summary:\n" +      summary << data[:errors].map do |hash| +        hash.map { |k,v| "  #{k}: #{v}" } +      end.join("\n") + +      summary +    end + +    def build_error_message +      return nil if @response.nil? + +      message =  "#{@response[:method].to_s.upcase} " +      message << redact_url(@response[:url].to_s) + ": " +      message << "#{@response[:status]} - " +      message << "#{response_message}" unless response_message.nil? +      message << "#{response_error}" unless response_error.nil? +      message << "#{response_error_summary}" unless response_error_summary.nil? +      message << " // See: #{documentation_url}" unless documentation_url.nil? +      message +    end + +    def redact_url(url_string) +      %w[client_secret access_token].each do |token| +        url_string.gsub!(/#{token}=\S+/, "#{token}=(redacted)") if url_string.include? token +      end +      url_string +    end +  end + +  # Raised on errors in the 400-499 range +  class ClientError < Error; end + +  # Raised when GitHub returns a 400 HTTP status code +  class BadRequest < ClientError; end + +  # Raised when GitHub returns a 401 HTTP status code +  class Unauthorized < ClientError; end + +  # Raised when GitHub returns a 401 HTTP status code +  # and headers include "X-GitHub-OTP" +  class OneTimePasswordRequired < ClientError +    #@private +    OTP_DELIVERY_PATTERN = /required; (\w+)/i + +    #@private +    def self.required_header(headers) +      OTP_DELIVERY_PATTERN.match headers['X-GitHub-OTP'].to_s +    end + +    # Delivery method for the user's OTP +    # +    # @return [String] +    def password_delivery +      @password_delivery ||= delivery_method_from_header +    end + +    private + +    def delivery_method_from_header +      if match = self.class.required_header(@response[:response_headers]) +        match[1] +      end +    end +  end + +  # Raised when GitHub returns a 403 HTTP status code +  class Forbidden < ClientError; end + +  # Raised when GitHub returns a 403 HTTP status code +  # and body matches 'rate limit exceeded' +  class TooManyRequests < Forbidden; end + +  # Raised when GitHub returns a 403 HTTP status code +  # and body matches 'login attempts exceeded' +  class TooManyLoginAttempts < Forbidden; end + +  # Raised when GitHub returns a 403 HTTP status code +  # and body matches 'abuse' +  class AbuseDetected < Forbidden; end + +  # Raised when GitHub returns a 403 HTTP status code +  # and body matches 'repository access blocked' +  class RepositoryUnavailable < Forbidden; end + +  # Raised when GitHub returns a 404 HTTP status code +  class NotFound < ClientError; end + +  # Raised when GitHub returns a 405 HTTP status code +  class MethodNotAllowed < ClientError; end + +  # Raised when GitHub returns a 406 HTTP status code +  class NotAcceptable < ClientError; end + +  # Raised when GitHub returns a 409 HTTP status code +  class Conflict < ClientError; end + +  # Raised when GitHub returns a 414 HTTP status code +  class UnsupportedMediaType < ClientError; end + +  # Raised when GitHub returns a 422 HTTP status code +  class UnprocessableEntity < ClientError; end + +  # Raised on errors in the 500-599 range +  class ServerError < Error; end + +  # Raised when GitHub returns a 500 HTTP status code +  class InternalServerError < ServerError; end + +  # Raised when GitHub returns a 501 HTTP status code +  class NotImplemented < ServerError; end + +  # Raised when GitHub returns a 502 HTTP status code +  class BadGateway < ServerError; end + +  # Raised when GitHub returns a 503 HTTP status code +  class ServiceUnavailable < ServerError; end + +  # Raised when client fails to provide valid Content-Type +  class MissingContentType < ArgumentError; end + +  # Raised when a method requires an application client_id +  # and secret but none is provided +  class ApplicationCredentialsRequired < StandardError; end +end diff --git a/lib/ievkit/response/raise_error.rb b/lib/ievkit/response/raise_error.rb new file mode 100644 index 000000000..9c466249e --- /dev/null +++ b/lib/ievkit/response/raise_error.rb @@ -0,0 +1,28 @@ +require 'faraday' +require 'ievkit/error' + +module Ievkit +  # Faraday response middleware +  module Response + +    # This class raises an Ievkit-flavored exception based +    # HTTP status codes returned by the API +    class RaiseError < Faraday::Response::Middleware + +      private + +      def on_complete(response) +        if error = Ievkit::Error.from_response(response) +          raise error +        end + +        # Big horrible hack to fix +        body = response[:body] +        if body["jobs"].present? +          response[:body] = body.gsub("{\"jobs\":", "").chomp("}") +        end +         +      end +    end +  end +end diff --git a/lib/ievkit/version.rb b/lib/ievkit/version.rb new file mode 100644 index 000000000..b2767952e --- /dev/null +++ b/lib/ievkit/version.rb @@ -0,0 +1,17 @@ +module Ievkit +  # Current major release. +  # @return [Integer] +  MAJOR = 0 + +  # Current minor release. +  # @return [Integer] +  MINOR = 1 + +  # Current patch level. +  # @return [Integer] +  PATCH = 0 + +  # Full release version. +  # @return [String] +  VERSION = [MAJOR, MINOR, PATCH].join('.').freeze +end | 
