diff --git a/lib/ghee.rb b/lib/ghee.rb index db485e6..d769718 100644 --- a/lib/ghee.rb +++ b/lib/ghee.rb @@ -73,6 +73,7 @@ def self.access_token(token, api_url = nil) def self.private_token(token, api_url = nil) options = { :private_token => token } + options[:enable_url_escape] = false options[:api_url] = api_url if api_url Ghee.new options end diff --git a/lib/ghee/api/gitlab/issues.rb b/lib/ghee/api/gitlab/issues.rb new file mode 100644 index 0000000..e69de29 diff --git a/lib/ghee/api/gitlab/repos.rb b/lib/ghee/api/gitlab/repos.rb index 74f154a..4e29733 100644 --- a/lib/ghee/api/gitlab/repos.rb +++ b/lib/ghee/api/gitlab/repos.rb @@ -27,13 +27,12 @@ class Proxy < ::Ghee::ResourceProxy def repos(login, name = nil) repo = name.nil? ? '' : "#{login}%2F#{name}" path_prefix = "./projects/#{repo}" - proxy = Proxy.new(connection, path_prefix, { escape_path: false } ) + proxy = Proxy.new(connection, path_prefix, self) proxy.repo_name = repo - translate_data(proxy) + proxy end def translate_data(data) - puts data.inspect data['full_name'] = data['path_with_namespace'] data['private'] = !data['public'] data['ssh_url'] = data['ssh_url_to_repo'] @@ -55,7 +54,6 @@ def translate_data(data) trees_url updated_at url watchers watchers_count) data.select { |key| allowed_keys.include? key } end - end end end diff --git a/lib/ghee/connection.rb b/lib/ghee/connection.rb index 7164445..fbbb409 100644 --- a/lib/ghee/connection.rb +++ b/lib/ghee/connection.rb @@ -4,7 +4,7 @@ class Ghee class Connection < Faraday::Connection - attr_reader :hash + attr_reader :hash, :enable_url_escape def parallel_connection(adapter=:typhoeus) conn = self.class.new @hash @@ -22,6 +22,7 @@ def parallel_connection(adapter=:typhoeus) # :basic_auth => {:user_name => "octocat", :password => "secret"} def initialize(hash={}) @hash = hash + @enable_url_escape = hash.fetch(:enable_url_escape, true) private_token = hash[:private_token] if hash.has_key?:private_token access_token = hash[:access_token] if hash.has_key?:access_token basic_auth = hash[:basic_auth] if hash.has_key?:basic_auth diff --git a/lib/ghee/resource_proxy.rb b/lib/ghee/resource_proxy.rb index 8a06a96..48c1c27 100644 --- a/lib/ghee/resource_proxy.rb +++ b/lib/ghee/resource_proxy.rb @@ -13,7 +13,7 @@ class ResourceProxy include Ghee::CUD # Make connection and path_prefix readable - attr_reader :connection, :path_prefix, :params, :id + attr_reader :connection, :path_prefix, :api_translator, :params, :id # Expose pagination data attr_reader :current_page, :total, :pagination @@ -30,15 +30,14 @@ def self.accept_header(header) # connection - Ghee::Connection object # path_prefix - String # - def initialize(connection, path_prefix, params = {}, &block) + def initialize(connection, path_prefix, api_translator = nil, params = {}, &block) if !params.is_a?Hash @id = params params = {} end - escape_path = params.fetch(:escape_path, true) - @connection, @path_prefix, @params = connection, path_prefix, params - @path_prefix = URI.escape(@path_prefix) if escape_path + @connection, @path_prefix, @api_translator, @params = connection, path_prefix, api_translator, params + @path_prefix = URI.escape(@path_prefix) if connection.enable_url_escape @block = block if block subject if block end @@ -68,18 +67,24 @@ def raw # Returns json # def subject - @subject ||= connection.get(path_prefix) do |req| - req.params.merge!params + @subject ||= connection.get(path_prefix) do |req| + req.params.merge!params req.headers["Accept"] = accept_type if self.respond_to? :accept_type @block.call(req)if @block - end.body + end + + if @api_translator + @api_translator.translate_data(@subject.body) + else + @subject.body + end end # Paginate is a helper method to handle # request pagination to the github api # # options - Hash containing pagination params - # eg; + # eg; # :per_page => 100, :page => 1 # # Returns self @@ -100,7 +105,7 @@ def paginate(options) parse_link_header response.headers.delete("link") - return self + return self end def all @@ -125,7 +130,7 @@ def all_parallel end end end - requests.inject([]) do |results, page| + requests.inject([]) do |results, page| results.concat(page.body) end end @@ -138,11 +143,11 @@ def all_parallel end def build_prefix(first_argument, endpoint) - (!first_argument.is_a?(Hash) && !first_argument.nil?) ? + (!first_argument.is_a?(Hash) && !first_argument.nil?) ? File.join(path_prefix, "/#{endpoint}/#{first_argument}") : File.join(path_prefix, "/#{endpoint}") end - private + private def pagination_data(header) parse_link_header header