Skip to content

Commit

Permalink
Improve API translation
Browse files Browse the repository at this point in the history
  • Loading branch information
Stan Hu committed Dec 2, 2015
1 parent 6650421 commit 1b8361e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 18 deletions.
1 change: 1 addition & 0 deletions lib/ghee.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Empty file added lib/ghee/api/gitlab/issues.rb
Empty file.
6 changes: 2 additions & 4 deletions lib/ghee/api/gitlab/repos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/ghee/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
31 changes: 18 additions & 13 deletions lib/ghee/resource_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -100,7 +105,7 @@ def paginate(options)

parse_link_header response.headers.delete("link")

return self
return self
end

def all
Expand 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
Expand All @@ -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
Expand Down

0 comments on commit 1b8361e

Please sign in to comment.