githubris 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.travis.yml CHANGED
@@ -1,4 +1,5 @@
1
1
  language: ruby
2
+ script: 'rake all'
2
3
  rvm:
3
4
  - 1.9.2
4
5
  - 1.9.3
data/Rakefile CHANGED
@@ -6,8 +6,15 @@ RSpec::Core::RakeTask.new :spec do |t|
6
6
  t.rspec_opts = '--color --format=documentation'
7
7
  end
8
8
 
9
+ namespace :features do
10
+ Cucumber::Rake::Task.new :wip do |t|
11
+ t.cucumber_opts = '--tags @wip --tags ~@backlog'
12
+ end
13
+ end
14
+
9
15
  Cucumber::Rake::Task.new :features do |t|
10
16
  t.cucumber_opts = '--tags ~@wip --tags ~@backlog'
11
17
  end
12
18
 
19
+ task :all => [:spec, :features]
13
20
  task :default => :spec
@@ -0,0 +1,8 @@
1
+ Feature: Get a user
2
+ In order to retrieve data about a user
3
+ As a developer
4
+ I want to be able to get a user's data
5
+
6
+ Scenario: Getting a user
7
+ When I access "@githubris.find_user('GithubrisTestUser')"
8
+ Then I have that user
@@ -5,8 +5,8 @@ Feature: List of Public Gists
5
5
 
6
6
  Scenario: Asking for public gists
7
7
  When I access "@githubris.public_gists"
8
- Then I should have a default number of gists
8
+ Then I have the default number of gists
9
9
 
10
- Scenario: Asking for a certain quantity of public gists
11
- When I access "@githubris.public_gists(quantity: 47)"
12
- Then I should have 47 gists
10
+ Scenario: Asking for a certain page of public gists
11
+ When I access "@githubris.public_gists(page: 60)"
12
+ Then I have the default number of gists
@@ -1,4 +1,3 @@
1
- @backlog
2
1
  Feature: List of user's public gists
3
2
  In order to view all of the public gists another user has made,
4
3
  As a user of the Githubris GitHub API,
@@ -6,9 +5,9 @@ Feature: List of user's public gists
6
5
 
7
6
  Scenario: via Githubris
8
7
  When I access "@githubris.public_gists(user: 'GithubrisTestUser')"
9
- Then I have GithubrisTestUser's public gists
8
+ Then I have 2 gists
10
9
 
11
10
  Scenario: via the User
12
11
  Given @user is GithubrisTestUser
13
12
  When I access "@user.public_gists"
14
- Then I have GithubrisTestUser's public gists
13
+ Then I have 2 gists
@@ -6,15 +6,14 @@ When /^I access "([^"]*)"$/ do |api_code|
6
6
  @actual = binding.eval api_code
7
7
  end
8
8
 
9
- Then /^I have GithubrisTestUser's public gists$/ do
10
- @actual.should eql test_public_gists
11
- end
12
-
13
- Then /^I should have a default number of gists$/ do
9
+ Then /^I have the default number of gists$/ do
14
10
  @actual.count.should eql 30
15
11
  end
16
12
 
17
- Then /^I should have (\d+) gists$/ do |count|
13
+ Then /^I have (\d+) gists$/ do |count|
18
14
  @actual.count.should eql Integer(count)
19
15
  end
20
16
 
17
+ Then /^I have that user$/ do
18
+ @actual.should == test_user
19
+ end
@@ -1,6 +1,6 @@
1
- $:.push '../../support'
2
1
 
3
2
  require 'githubris'
3
+
4
4
  Before do
5
5
  @githubris = Githubris.new
6
6
  end
@@ -3,4 +3,8 @@ require 'fakeweb'
3
3
  FakeWeb.allow_net_connect = false
4
4
  FakeWeb.register_uri(:get, 'https://api.github.com/', :body => '{}')
5
5
  FakeWeb.register_uri(:get, /gists\/public(?:[^?]*\?page=(\d+))?/,
6
- :body => File.open("features/support/public_gists_page_#{$1 || 1}.json", 'r') {|f| f.read })
6
+ :body => File.open("spec/support/public_gists_page_#{$1 || 1}.json", 'r') {|f| f.read })
7
+ FakeWeb.register_uri(:get, /users\/([^\/]*)\/gists\z/,
8
+ :body => File.open("spec/support/user_public_gists.json"){|f| f.read })
9
+ FakeWeb.register_uri(:get, /users\/\w+\z/,
10
+ :body => File.open("spec/support/user.json"){|f| f.read })
@@ -1,10 +1,34 @@
1
1
  module Fixtures
2
2
  def test_public_gists
3
- Githubris::Builder.new.build get_public_gists_data
3
+ build_gists get_public_gists_data
4
+ end
5
+
6
+ def test_user_public_gists
7
+ build_gists get_user_public_gists_data
8
+ end
9
+
10
+ def test_user
11
+ build_user get_user_data
12
+ end
13
+
14
+ def build_gists data
15
+ Githubris::Builder.new.build_gists data
16
+ end
17
+
18
+ def build_user data
19
+ Githubris::Builder.new.build_user data
20
+ end
21
+
22
+ def get_user_public_gists_data
23
+ MultiJson.decode(File.open("spec/support/user_public_gists.json") {|f| f.read})
4
24
  end
5
25
 
6
26
  def get_public_gists_data(page=1)
7
- MultiJson.decode(File.open("features/support/public_gists_page_#{page}.json") {|f| f.read })
27
+ MultiJson.decode(File.open("spec/support/public_gists_page_#{page}.json") {|f| f.read })
28
+ end
29
+
30
+ def get_user_data
31
+ MultiJson.decode(File.open('spec/support/user.json') {|f| f.read })
8
32
  end
9
33
  end
10
34
 
@@ -0,0 +1,31 @@
1
+ require 'githubris/builder/gist'
2
+
3
+ module Githubris::API::Gist
4
+ PUBLIC_GISTS_PATH = '/gists/public'
5
+
6
+ def get_user_public_gists(login)
7
+ data = self.class.get user_gists_path(login)
8
+ gists = @builder.build_gists(data)
9
+ gists.map do |gist|
10
+ gist.set_attribute(:user, get_user(login))
11
+ gist
12
+ end
13
+ end
14
+
15
+ def get_public_gists(options={})
16
+ login = options.delete(:user)
17
+ return get_user_public_gists(login) if login
18
+
19
+ data = self.class.get public_gists_path_for_page(options[:page])
20
+ @builder.build_gists data
21
+ end
22
+
23
+ private
24
+ def public_gists_path_for_page page_number=1
25
+ "#{PUBLIC_GISTS_PATH}?page=#{page_number}"
26
+ end
27
+
28
+ def user_gists_path(login)
29
+ "/users/#{login}/gists"
30
+ end
31
+ end
@@ -0,0 +1,15 @@
1
+ require 'githubris/builder/user'
2
+
3
+ module Githubris::API::User
4
+ USERS_PATH = '/users'
5
+
6
+ def get_user(login)
7
+ data = self.class.get user_path(login)
8
+ @builder.build_user data
9
+ end
10
+
11
+ private
12
+ def user_path(login)
13
+ "#{USERS_PATH}/#{login}"
14
+ end
15
+ end
data/lib/githubris/api.rb CHANGED
@@ -2,12 +2,16 @@ require 'httparty'
2
2
 
3
3
  class Githubris::API
4
4
  include HTTParty
5
+
6
+ require_relative 'api/gist'
7
+ include Githubris::API::Gist
8
+
9
+ require_relative 'api/user'
10
+ include Githubris::API::User
11
+
5
12
  base_uri 'https://api.github.com'
6
13
  format :json
7
14
 
8
- GISTS_PER_PAGE = 30
9
- PUBLIC_GISTS_PATH = '/gists/public'
10
-
11
15
  def initialize
12
16
  @builder = Githubris::Builder.new
13
17
  end
@@ -15,30 +19,4 @@ class Githubris::API
15
19
  def authenticate!(options={})
16
20
  Githubris::API.basic_auth options[:login], options[:password]
17
21
  end
18
-
19
- def get_public_gists(options={})
20
- quantity = options.delete(:quantity) || 30
21
- number_of_overflow_gists = quantity % GISTS_PER_PAGE
22
- if quantity > GISTS_PER_PAGE
23
- max_page_number = (quantity / GISTS_PER_PAGE) +
24
- (number_of_overflow_gists > 0 ? 1 : 0)
25
-
26
- data = Githubris::API.get public_gists_path_for_page(max_page_number)
27
- gists = @builder.build(data).
28
- concat(get_public_gists(quantity: quantity - GISTS_PER_PAGE))
29
- else
30
- url = PUBLIC_GISTS_PATH
31
- data = Githubris::API.get(url)
32
- gists = @builder.build(data)
33
- end
34
- gists.shift(quantity)
35
- end
36
-
37
- def get_user(options={})
38
- Githubris::User.new
39
- end
40
-
41
- def public_gists_path_for_page page_number
42
- "#{PUBLIC_GISTS_PATH}?page=#{page_number}"
43
- end
44
22
  end
@@ -0,0 +1,16 @@
1
+ class Githubris::Builder::Gist
2
+ def build data
3
+ gist_data = {}
4
+ gist_data[:id] = Integer(data.delete('id'))
5
+ gist_data[:public] = data.delete('public')
6
+ gist_data[:description] = data.delete('description')
7
+ gist_data[:files] = data.delete('files').values
8
+ gist_data[:url] = URI.parse data.delete('url')
9
+ gist_data[:created_at] = DateTime.parse data.delete('created_at')
10
+ gist_data[:updated_at] = DateTime.parse data.delete('updated_at')
11
+ tmp_arr = []
12
+ data.delete('comments').times { tmp_arr << Githubris::Comment.new }
13
+ gist_data[:comments] = tmp_arr
14
+ Githubris::Gist.new gist_data
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ require 'githubris/builder'
2
+ require 'githubris/user'
3
+
4
+ class Githubris::Builder::User
5
+ def build data
6
+ user_attributes = {
7
+ :login => data.delete('login'),
8
+ :id => Integer(data.delete('id')),
9
+ :avatar_url => URI.parse(data.delete('avatar_url')),
10
+ :url => URI.parse(data.delete('url')),
11
+ :gravatar_id => data.delete('gravatar_id')
12
+ }
13
+ Githubris::User.new user_attributes
14
+ end
15
+ end
@@ -1,35 +1,15 @@
1
- class Githubris
2
- class Builder
3
- def build data
4
- if data.instance_of? Array
5
- build_gists data
6
- else
7
- build_gist data
8
- end
9
- end
10
-
11
- def build_gists(data)
12
- data.map do |gist|
13
- build_gist gist
14
- end
15
- end
1
+ class Githubris::Builder
2
+ autoload :Gist, 'githubris/builder/gist'
3
+ autoload :User, 'githubris/builder/user'
16
4
 
17
- def build_gist(data)
18
- data[:user] = build_user(data.delete 'user' )
19
- data[:public] = data.delete('public')
20
- data[:description] = data.delete('description')
21
- data[:files] = data.delete('files').values
22
- data[:url] = URI.parse data.delete('url')
23
- data[:created_at] = DateTime.parse data.delete('created_at')
24
- data[:updated_at] = DateTime.parse data.delete('updated_at')
25
- tmp_arr = []
26
- data.delete('comments').times { tmp_arr << Githubris::Comment.new }
27
- data[:comments] = tmp_arr
28
- Githubris::Gist.new data
5
+ def build_gists(data)
6
+ data = [data].flatten
7
+ data.map do |gist|
8
+ Githubris::Builder::Gist.new.build gist
29
9
  end
10
+ end
30
11
 
31
- def build_user data
32
- Githubris::User.new
33
- end
12
+ def build_user(data)
13
+ Githubris::Builder::User.new.build(data)
34
14
  end
35
15
  end
@@ -1,57 +1,68 @@
1
- class Githubris
2
- class Gist
3
- autoload :File, 'githubris/gist/file'
4
-
5
- class << self
6
- def default_options
7
- {
8
- files: [],
9
- created_at: DateTime.now,
10
- updated_at: DateTime.now
11
- }
1
+ class Githubris::Gist
2
+ autoload :File, 'githubris/gist/file'
3
+
4
+ def self.default_attributes
5
+ {
6
+ files: [],
7
+ created_at: DateTime.now,
8
+ updated_at: DateTime.now
9
+ }
10
+ end
11
+
12
+ def initialize attributes={}
13
+ attributes.merge! Githubris::Gist.default_attributes do |given_key, given_value|
14
+ if Githubris::Gist.default_attributes.has_key? given_key
15
+ given_value
12
16
  end
13
17
  end
14
18
 
15
- def initialize options={}
16
- options.merge! Githubris::Gist.default_options do |given_key, given_value|
17
- if Githubris::Gist.default_options.has_key? given_key
18
- given_value
19
- end
20
- end
19
+ @attributes = attributes
20
+ end
21
21
 
22
- @options = options
23
- end
22
+ def id
23
+ @attributes[:id]
24
+ end
24
25
 
25
- def user
26
- @options[:user]
27
- end
26
+ def user
27
+ @attributes[:user]
28
+ end
28
29
 
29
- def created_at
30
- @options[:created_at]
31
- end
30
+ def created_at
31
+ @attributes[:created_at]
32
+ end
32
33
 
33
- def updated_at
34
- @options[:updated_at]
35
- end
34
+ def updated_at
35
+ @attributes[:updated_at]
36
+ end
36
37
 
37
- def description
38
- @options[:description]
39
- end
38
+ def description
39
+ @attributes[:description]
40
+ end
40
41
 
41
- def files
42
- @options[:files]
43
- end
42
+ def files
43
+ @attributes[:files]
44
+ end
44
45
 
45
- def url
46
- @options[:url]
47
- end
46
+ def url
47
+ @attributes[:url]
48
+ end
48
49
 
49
- def public?
50
- @options[:public]
51
- end
50
+ def public?
51
+ @attributes[:public]
52
+ end
52
53
 
53
- def comments
54
- @options[:comments]
55
- end
54
+ def comments
55
+ @attributes[:comments]
56
+ end
57
+
58
+ def ==(other)
59
+ self.class == other.class &&
60
+ self.created_at == other.created_at &&
61
+ self.files == other.files &&
62
+ self.user == other.user
63
+ end
64
+
65
+ def set_attribute(attribute, value)
66
+ @attributes[attribute] = value
56
67
  end
57
68
  end
@@ -1,10 +1,36 @@
1
- class Githubris
2
- class User
3
- def initialize data={}
4
- end
1
+ class Githubris::User
2
+ def initialize attributes={}
3
+ @attributes = attributes
4
+ end
5
5
 
6
- def public_gists
7
- end
6
+ def public_gists
7
+ Githubris::API.new.get_user_public_gists(@attributes[:login])
8
+ end
9
+
10
+ def login
11
+ @attributes[:login]
12
+ end
13
+
14
+ def id
15
+ @attributes[:id]
16
+ end
8
17
 
18
+ def avatar_url
19
+ @attributes[:avatar_url]
20
+ end
21
+
22
+ def url
23
+ @attributes[:url]
24
+ end
25
+
26
+ def gravatar_id
27
+ @attributes[:gravatar_id]
28
+ end
29
+
30
+ def == other
31
+ other_attrs = other.instance_variable_get(:@attributes)
32
+ if other_attrs
33
+ @attributes == other_attrs
34
+ end
9
35
  end
10
36
  end
@@ -1,3 +1,3 @@
1
1
  class Githubris
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/githubris.rb CHANGED
@@ -24,13 +24,13 @@ class Githubris
24
24
  @api = Githubris::API.new
25
25
  end
26
26
 
27
- def authenticate(username, password)
28
- @api.authenticate! login: username, password: password
27
+ def authenticate(login, password)
28
+ @api.authenticate! login: login, password: password
29
29
  @authenticated_user = Githubris::User.new
30
30
  end
31
31
 
32
32
  def find_user(login)
33
- @api.get_user(login: login)
33
+ @api.get_user(login)
34
34
  end
35
35
 
36
36
  def public_gists(options={})
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ describe Githubris::API::Gist do
4
+ subject do
5
+ Githubris::API.new
6
+ end
7
+
8
+ describe '#get_user_public_gists' do
9
+ let(:login) {'GithubrisTestUser'}
10
+
11
+ it 'gets /users/:username/gists' do
12
+ subject.stub(:user_gists_path => '/users/GithubrisTestUser/gists')
13
+ user_public_gists = subject.get_user_public_gists(login)
14
+ subject.should have_received(:user_gists_path).with(login)
15
+ end
16
+
17
+ it 'retrieves the full user' do
18
+ subject.stub(:get_user)
19
+ subject.get_public_gists(user: login)
20
+ subject.should have_received(:get_user).with(login)
21
+ end
22
+
23
+ context 'given a user with 2 gists' do
24
+ it 'returns 2 gists by the given user' do
25
+ user_public_gists = subject.get_public_gists(user: login)
26
+ user_public_gists.should have(2).items
27
+ user_public_gists.each do |gist|
28
+ gist.should be_instance_of Githubris::Gist
29
+ end
30
+ end
31
+ end
32
+
33
+ end
34
+
35
+ describe '#get_public_gists' do
36
+ it 'returns an array of gists' do
37
+ subject.get_public_gists.should be_instance_of Array
38
+ subject.get_public_gists.each do |gist|
39
+ gist.should be_instance_of Githubris::Gist
40
+ end
41
+ end
42
+
43
+ it 'accepts an options hash' do
44
+ lambda do
45
+ subject.get_public_gists({foo: 'bar'})
46
+ end.should_not raise_error
47
+ end
48
+
49
+ context 'given a page option' do
50
+ it 'returns an array of 30 gists from that page' do
51
+ page_one_gists = subject.get_public_gists(page: 1)
52
+ page_two_gists = subject.get_public_gists(page: 2)
53
+ page_one_gists.should have(30).items
54
+ page_two_gists.should have(30).items
55
+ end
56
+ end
57
+
58
+ context 'given a user option' do
59
+ it 'delegates to #get_user_public_gists' do
60
+ login = 'GithubrisTestUser'
61
+ subject.should_receive(:get_user_public_gists).with(login)
62
+ subject.get_public_gists(user: login)
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe Githubris::API::User do
4
+ let(:api) { Githubris::API.new }
5
+
6
+ describe '#get_user' do
7
+ subject { api.get_user('login') }
8
+ it 'takes a login' do
9
+ lambda do
10
+ api.get_user('login')
11
+ end.should_not raise_error
12
+ end
13
+
14
+ it 'returns a user object' do
15
+ subject.should be_instance_of Githubris::User
16
+ end
17
+ end
18
+ end
@@ -1,28 +1,4 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Githubris::API do
4
- describe '#get_public_gists' do
5
- it 'returns an array of gists' do
6
- subject.get_public_gists.should be_instance_of Array
7
- subject.get_public_gists.each do |gist|
8
- gist.should be_instance_of Githubris::Gist
9
- end
10
- end
11
-
12
- it 'accepts an options hash' do
13
- lambda do
14
- subject.get_public_gists({foo: 'bar'})
15
- end.should_not raise_error
16
- end
17
-
18
- context 'given a quantity option' do
19
- it 'returns an array of gists with that quantity' do
20
- subject.get_public_gists(quantity: 10).should have(10).items
21
- subject.get_public_gists(quantity: 30).should have(30).items
22
- subject.get_public_gists(quantity: 31).should have(31).items
23
- subject.get_public_gists(quantity: 45).should have(45).items
24
- subject.get_public_gists(quantity: 61).should have(61).items
25
- end
26
- end
27
- end
28
4
  end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe Githubris::Builder::Gist do
4
+ subject { Githubris::Builder::Gist.new.build gist_data }
5
+ let(:gist_data) { Githubris::SpecHelper.gist_data }
6
+
7
+ context 'schema' do
8
+
9
+ it { should be_instance_of Githubris::Gist }
10
+ its(:url) { should be_kind_of URI }
11
+ its(:id) { should be_instance_of Fixnum }
12
+ its(:description) { should be_instance_of String }
13
+ it { should be_public }
14
+ its(:created_at) { should be_instance_of DateTime }
15
+ its(:updated_at) { should be_instance_of DateTime }
16
+ its(:comments) { should be_instance_of Array }
17
+ its(:files) { should be_instance_of Array }
18
+ end
19
+
20
+ context 'when passed a specific gist' do
21
+
22
+ it 'is public' do
23
+ subject.should be_public
24
+ end
25
+
26
+ its(:id) { should eql 1 }
27
+
28
+ its(:description) { should eql 'the meaning of gist' }
29
+
30
+ it 'was created at 6:17:13 on July 15, 2008' do
31
+ subject.created_at.should eql DateTime.new(2008, 7, 15, 18, 17, 13)
32
+ end
33
+
34
+ it 'was updated at 2:58:22 on Feburary 22, 2011' do
35
+ subject.updated_at.should eql DateTime.new(2011, 2, 22, 2, 58, 22)
36
+ end
37
+ end
38
+ end