githubris 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. data/.travis.yml +5 -5
  2. data/Rakefile +11 -2
  3. data/githubris.gemspec +3 -2
  4. data/lib/githubris.rb +1 -11
  5. data/lib/githubris/api.rb +61 -40
  6. data/lib/githubris/api/gist.rb +40 -3
  7. data/lib/githubris/api/http.rb +13 -4
  8. data/lib/githubris/api/json.rb +16 -0
  9. data/lib/githubris/base.rb +2 -64
  10. data/lib/githubris/custom_attributes.rb +40 -0
  11. data/lib/githubris/gist.rb +53 -7
  12. data/lib/githubris/oauth.rb +23 -18
  13. data/lib/githubris/publicity.rb +2 -2
  14. data/lib/githubris/version.rb +1 -1
  15. data/spec/githubris/api/gist_spec.rb +2 -6
  16. data/spec/githubris/api_spec.rb +3 -22
  17. data/spec/githubris/base_spec.rb +1 -1
  18. data/spec/githubris/gist_spec.rb +117 -26
  19. data/spec/githubris/oauth_spec.rb +12 -0
  20. data/spec/githubris/publicity_spec.rb +4 -4
  21. data/spec/githubris/user_spec.rb +2 -10
  22. data/spec/githubris_spec.rb +2 -5
  23. data/spec/support/fixtures/vcr_cassettes/Githubris.yml +137 -0
  24. data/spec/support/fixtures/vcr_cassettes/Githubris_API.yml +118 -0
  25. data/spec/support/fixtures/vcr_cassettes/Githubris_API/_authenticated_/when_authenticated_with_good_credentials.yml +6 -6
  26. data/spec/support/fixtures/vcr_cassettes/Githubris_API_Gist.yml +197 -0
  27. data/spec/support/fixtures/vcr_cassettes/Githubris_API_User/_get_authenticated_user/without_credentials.yml +3 -3
  28. data/spec/support/fixtures/vcr_cassettes/Githubris_API_User/_get_user.yml +5 -5
  29. data/spec/support/fixtures/vcr_cassettes/Githubris_Gist.yml +1009 -0
  30. data/spec/support/fixtures/vcr_cassettes/Githubris_User.yml +196 -0
  31. data/spec/support/vcr.rb +1 -0
  32. metadata +78 -39
  33. data/lib/githubris/comment.rb +0 -1
  34. data/lib/githubris/gist/file.rb +0 -2
  35. data/lib/githubris/issue.rb +0 -6
  36. data/lib/githubris/organization.rb +0 -5
  37. data/lib/githubris/repository.rb +0 -7
  38. data/spec/support/fixtures/vcr_cassettes/Githubris/_find_gist.yml +0 -44
  39. data/spec/support/fixtures/vcr_cassettes/Githubris/_find_user.yml +0 -40
  40. data/spec/support/fixtures/vcr_cassettes/Githubris/_public_gists.yml +0 -55
  41. data/spec/support/fixtures/vcr_cassettes/Githubris_API/_authenticated_/when_authenticated_with_bad_credentials.yml +0 -40
  42. data/spec/support/fixtures/vcr_cassettes/Githubris_API/_authenticated_/when_there_are_no_credentials.yml +0 -42
  43. data/spec/support/fixtures/vcr_cassettes/Githubris_API_Gist/_get_gist.yml +0 -44
  44. data/spec/support/fixtures/vcr_cassettes/Githubris_API_Gist/_get_public_gists.yml +0 -107
  45. data/spec/support/fixtures/vcr_cassettes/Githubris_API_Gist/_get_user_gists.yml +0 -43
  46. data/spec/support/fixtures/vcr_cassettes/Githubris_Gist/_save.yml +0 -42
  47. data/spec/support/fixtures/vcr_cassettes/Githubris_Gist/_save/when_authenticated.yml +0 -44
  48. data/spec/support/fixtures/vcr_cassettes/Githubris_Gist/any_gist.yml +0 -44
  49. data/spec/support/fixtures/vcr_cassettes/Githubris_Gist/given_a_gist_full_of_data.yml +0 -44
  50. data/spec/support/fixtures/vcr_cassettes/Githubris_Gist/on_a_gist_with_an_id.yml +0 -44
  51. data/spec/support/fixtures/vcr_cassettes/Githubris_Gist/on_a_gist_with_an_id/_reload.yml +0 -44
  52. data/spec/support/fixtures/vcr_cassettes/Githubris_User/_gists.yml +0 -43
  53. data/spec/support/fixtures/vcr_cassettes/Githubris_User/_reload.yml +0 -40
  54. data/spec/support/fixtures/vcr_cassettes/Githubris_User/_reload/on_a_user_with_a_login.yml +0 -40
  55. data/spec/support/fixtures/vcr_cassettes/Githubris_User/_reload/on_a_user_without_a_login.yml +0 -40
  56. data/spec/support/fixtures/vcr_cassettes/Githubris_User/_starred_gists/on_an_authenticated_user.yml +0 -82
@@ -7,8 +7,8 @@ rvm:
7
7
  - ruby-head
8
8
  - jruby-head
9
9
  # Breaks on the JSON stuff
10
- # - 1.8.7
11
- # - jruby-18mode
12
- # - jruby-19mode
13
- # - rbx-18mode
14
- # - ree
10
+ - 1.8.7
11
+ - jruby-18mode
12
+ - jruby-19mode
13
+ - rbx-18mode
14
+ - ree
data/Rakefile CHANGED
@@ -1,7 +1,16 @@
1
1
  require 'bundler/gem_tasks'
2
+ require 'bundler/setup'
2
3
  require 'rspec/core/rake_task'
3
4
  require 'flay_task'
4
5
  require 'flog_task'
6
+ require 'pry'
7
+
8
+ Bundler.setup
9
+
10
+ task :console do
11
+ Bundler.require
12
+ pry
13
+ end
5
14
 
6
15
  RSpec::Core::RakeTask.new :spec do |t|
7
16
  t.rspec_opts = '--color --format=documentation'
@@ -20,5 +29,5 @@ FlayTask.new &FLAY_FLOG_OPTS_BLOCK
20
29
  FlogTask.new &FLAY_FLOG_OPTS_BLOCK
21
30
 
22
31
  task :ci => [:spec]
23
- task :all => [:cov, :spec, :flay, :flog]
24
- task :default => [:cov, :spec]
32
+ task :specs => :spec
33
+ task :default => [:cov, :spec, :flay, :flog]
@@ -16,7 +16,8 @@ Gem::Specification.new do |s|
16
16
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
17
  s.require_paths = ["lib"]
18
18
 
19
- s.add_dependency('httparty', '~> 0.8.1')
20
- s.add_dependency('multi_json', '~> 1.1.0')
19
+ s.add_dependency('httparty', '>= 0.8.3')
20
+ s.add_dependency('multi_json')
21
+ s.add_dependency('yajl-ruby')
21
22
  s.add_dependency('addressable', '~> 2.2.7')
22
23
  end
@@ -1,21 +1,11 @@
1
1
  class Githubris
2
2
  autoload :API, 'githubris/api'
3
3
  autoload :Base, 'githubris/base'
4
- autoload :Blob, 'githubris/blob'
5
- autoload :Comment, 'githubris/comment'
6
- autoload :Commit, 'githubris/commit'
4
+ autoload :CustomAttributes, 'githubris/custom_attributes'
7
5
  autoload :Error, 'githubris/error'
8
- autoload :Event, 'githubris/event'
9
6
  autoload :Gist, 'githubris/gist'
10
- autoload :Issue, 'githubris/issue'
11
7
  autoload :OAuth, 'githubris/oauth'
12
- autoload :Organization, 'githubris/organization'
13
8
  autoload :Publicity, 'githubris/publicity'
14
- autoload :PullRequest, 'githubris/pull_request'
15
- autoload :Reference, 'githubris/reference'
16
- autoload :Repository, 'githubris/repository'
17
- autoload :Tag, 'githubris/tag'
18
- autoload :Tree, 'githubris/tree'
19
9
  autoload :User, 'githubris/user'
20
10
  autoload :Version, 'githubris/version'
21
11
 
@@ -1,12 +1,14 @@
1
1
  require 'addressable/uri'
2
+ require 'yajl'
2
3
 
3
4
  class Githubris::API
4
-
5
- autoload :HTTP, 'githubris/api/http'
6
- include Githubris::API::HTTP
7
- autoload :Gist, 'githubris/api/gist'
5
+ require 'githubris/api/gist'
8
6
  include Githubris::API::Gist
9
- autoload :User, 'githubris/api/user'
7
+ require 'githubris/api/http'
8
+ include Githubris::API::HTTP
9
+ require 'githubris/api/json'
10
+ include Githubris::API::JSON
11
+ require 'githubris/api/user'
10
12
  include Githubris::API::User
11
13
 
12
14
  def initialize
@@ -18,6 +20,7 @@ class Githubris::API
18
20
  {
19
21
  :scheme => 'https',
20
22
  :host => 'api.github.com',
23
+ :query => ''
21
24
  }
22
25
  end
23
26
 
@@ -29,7 +32,7 @@ class Githubris::API
29
32
  end
30
33
 
31
34
  def oauth(client_id, client_secret)
32
- oauth = Githubris::OAuth.new client_id, client_secret
35
+ Githubris::OAuth.new client_id, client_secret, self
33
36
  end
34
37
 
35
38
  def authenticated?
@@ -39,59 +42,77 @@ class Githubris::API
39
42
  end
40
43
 
41
44
  def get_data_from(path, options={})
42
- this = self
43
- data = get(path, options)
44
- data = MultiJson.decode(data, :symbolize_keys => true)
45
- raise build_error data if error_data?(data)
46
-
47
- embed_self_in data
48
- end
49
-
50
- def build_error data
51
- words = data[:message].split(' ')
52
- error_class_name = words.map {|word| word.capitalize}.join
53
- Githubris::Error.const_get(error_class_name)
45
+ handle_request_data get(path, options)
54
46
  end
55
47
 
56
48
  def get(path, options={})
57
- @target.path = path
49
+ set_request_path(path)
58
50
  @target.query_values = options
59
- MultiJson.encode(_get(@target.to_s))
60
- end
61
-
62
- def error_data?(data)
63
- data.is_a?(Hash) and data[:message]
51
+ dump_json(_get.parsed_response)
64
52
  end
65
53
 
66
54
  def post_oauth_access_token(params)
67
- @target.query ||= ''
68
55
  @target.query += _post(oauth_access_token_url, :query => params)
69
- @target.query_values['access_token']
56
+ @options[:default_params] ||= {}
57
+ @options[:default_params][:access_token] = @target.query_values['access_token']
70
58
  end
71
59
 
72
60
  def post_data_to(path, params)
73
- data = post(path, MultiJson.encode(params))
74
- data = MultiJson.decode(data, :symbolize_keys => true) if data.is_a? String
75
- raise build_error data if error_data?(data)
61
+ handle_request_data post(path, dump_json(params))
62
+ end
63
+
64
+ def post(path, params)
65
+ set_request(path, params)
66
+ dump_json(_post.parsed_response)
67
+ end
68
+
69
+ def patch_data_to(path, params)
70
+ handle_request_data patch(path, dump_json(params))
71
+ end
72
+
73
+ def patch(path, params)
74
+ set_request(path, params)
75
+ dump_json(_patch.parsed_response)
76
+ end
76
77
 
78
+ private
79
+
80
+ def set_request(path, params)
81
+ set_request_path(path)
82
+ set_request_params(params)
83
+ end
84
+
85
+ def set_request_params(params)
86
+ @options[:body] = params
87
+ end
88
+
89
+ def set_request_path(path)
90
+ @target.path = path
91
+ end
92
+
93
+ def handle_request_data(data)
94
+ data = load_json(data) if data.is_a? String
95
+ raise build_error data if error_data?(data)
77
96
  embed_self_in data
78
97
  end
79
98
 
99
+ def build_error data
100
+ words = data[:message].split(' ')
101
+ error_class_name = words.map {|word| word.capitalize}.join
102
+ Githubris::Error.const_get(error_class_name)
103
+ end
104
+
105
+ def error_data?(data)
106
+ data.is_a?(Hash) and data[:message]
107
+ end
108
+
80
109
  def embed_self_in(data)
81
- this = self
82
110
  if data.is_a? Array
83
- data.each {|data_el| data_el[:_api] = this }
111
+ data.map {|data_el| embed_self_in(data_el) }
84
112
  else
85
- data[:_api] = this
113
+ data[:_api] = self
114
+ data
86
115
  end
87
-
88
- data
89
- end
90
-
91
- def post(path, params)
92
- @target.path = path
93
- @options[:body] = params
94
- MultiJson.encode(_post(@target.to_s))
95
116
  end
96
117
 
97
118
  def oauth_access_token_url
@@ -16,10 +16,39 @@ module Githubris::API::Gist
16
16
  get_gist_from(gist_path(id))
17
17
  end
18
18
 
19
+ def put_gist_star(id)
20
+ set_request_path(gist_star_path(id))
21
+ _put(@target.to_s, @options.merge(:headers => {'Content-Length' => '0'}))
22
+ end
23
+
24
+ def delete_gist_star(id)
25
+ set_request_path(gist_star_path(id))
26
+ _delete
27
+ end
28
+
29
+ def delete_gist(id)
30
+ set_request_path(gist_path(id))
31
+ _delete
32
+ end
33
+
34
+ def get_gist_starred?(id)
35
+ set_request_path(gist_star_path(id))
36
+ case _get.code
37
+ when 204
38
+ true
39
+ when 404
40
+ false
41
+ end
42
+ end
43
+
19
44
  def post_gist(params)
20
45
  post_gist_to(gists_path, params)
21
46
  end
22
47
 
48
+ def patch_gist(id, params)
49
+ patch_gist_to(gist_path(id), params)
50
+ end
51
+
23
52
  private
24
53
  def get_gists_from(path, options={})
25
54
  get_data_from(path, options).map do |gist_data|
@@ -35,8 +64,12 @@ module Githubris::API::Gist
35
64
  Githubris::Gist.new post_data_to(path, params)
36
65
  end
37
66
 
67
+ def patch_gist_to(path, params)
68
+ Githubris::Gist.new patch_data_to(path, params)
69
+ end
70
+
38
71
  def public_gists_path
39
- "#{gists_path}/public"
72
+ '/gists/public'
40
73
  end
41
74
 
42
75
  def user_gists_path(login)
@@ -44,11 +77,15 @@ module Githubris::API::Gist
44
77
  end
45
78
 
46
79
  def user_starred_gists_path
47
- "#{gists_path}/starred"
80
+ '/gists/starred'
81
+ end
82
+
83
+ def gist_star_path(id)
84
+ "/gists/#{id}/star"
48
85
  end
49
86
 
50
87
  def gist_path(id)
51
- "#{gists_path}/#{id}"
88
+ "/gists/#{id}"
52
89
  end
53
90
 
54
91
  def gists_path
@@ -2,11 +2,20 @@ require 'httparty'
2
2
 
3
3
  module Githubris::API::HTTP
4
4
  private
5
- def _get(path, options=@options)
6
- HTTParty.get(path, options)
5
+
6
+ def self.define_httparty_method(*http_methods)
7
+ http_methods.each do |http_method|
8
+ module_eval http_method_string(http_method)
9
+ end
7
10
  end
8
11
 
9
- def _post(path, options=@options)
10
- HTTParty.post(path, options)
12
+ def self.http_method_string(http_method)
13
+ """
14
+ def _#{http_method}(path=@target.to_s, options=@options)
15
+ HTTParty.#{http_method}(path, options)
16
+ end
17
+ """
11
18
  end
19
+
20
+ define_httparty_method :get, :post, :patch, :put, :delete
12
21
  end
@@ -0,0 +1,16 @@
1
+ require 'multi_json'
2
+ require 'yajl'
3
+
4
+ module Githubris::API::JSON
5
+ MultiJson.use :yajl
6
+
7
+ private
8
+
9
+ def load_json(json)
10
+ MultiJson.load(json, :symbolize_keys => true)
11
+ end
12
+
13
+ def dump_json(json)
14
+ MultiJson.dump(json)
15
+ end
16
+ end
@@ -1,4 +1,6 @@
1
1
  class Githubris::Base
2
+ extend Githubris::CustomAttributes
3
+
2
4
  def initialize attributes={}
3
5
  @api = attributes.delete(:_api) || Githubris::API.new
4
6
  @attributes = attributes
@@ -13,68 +15,4 @@ class Githubris::Base
13
15
  other.instance_variable_get(:@attributes))
14
16
  self
15
17
  end
16
-
17
- private
18
-
19
- class << self
20
- def accessible_attribute(*attr_names)
21
- attr_names.each do |name|
22
- class_eval accessible_methods_called(name)
23
- end
24
- end
25
-
26
- def accessible_methods_called(name)
27
- """
28
- def #{name}
29
- @attributes[:#{name}]
30
- end
31
-
32
- def #{name}=(#{name})
33
- @attributes[:#{name}] = #{name}
34
- end
35
- """
36
- end
37
-
38
- def uri_attribute(*attr_names)
39
- attr_names.each do |name|
40
- class_eval uri_method_called(name)
41
- end
42
- end
43
-
44
- def uri_method_called(name)
45
- """
46
- def #{name}
47
- URI.parse @attributes[:#{name}]
48
- end
49
- """
50
- end
51
-
52
- def datetime_attribute(*attr_names)
53
- attr_names.each do |name|
54
- class_eval datetime_method_called(name)
55
- end
56
- end
57
-
58
- def datetime_method_called(name)
59
- """
60
- def #{name}
61
- DateTime.parse @attributes[:#{name}]
62
- end
63
- """
64
- end
65
-
66
- def readable_attribute(*attr_names)
67
- attr_names.each do |name|
68
- class_eval readable_method_called(name)
69
- end
70
- end
71
-
72
- def readable_method_called(name)
73
- """
74
- def #{name}
75
- @attributes[:#{name}]
76
- end
77
- """
78
- end
79
- end
80
18
  end
@@ -0,0 +1,40 @@
1
+ module Githubris::CustomAttributes
2
+ private
3
+
4
+ def accessible_attribute(*attr_names)
5
+ readable_attribute(*attr_names)
6
+ writable_attribute(*attr_names)
7
+ end
8
+
9
+ def uri_attribute(*attr_names)
10
+ attr_names.each do |name|
11
+ define_method name do
12
+ URI.parse @attributes[name]
13
+ end
14
+ end
15
+ end
16
+
17
+ def datetime_attribute(*attr_names)
18
+ attr_names.each do |name|
19
+ define_method name do
20
+ DateTime.parse @attributes[name]
21
+ end
22
+ end
23
+ end
24
+
25
+ def readable_attribute(*attr_names)
26
+ attr_names.each do |name|
27
+ define_method name do
28
+ @attributes[name]
29
+ end
30
+ end
31
+ end
32
+
33
+ def writable_attribute(*attr_names)
34
+ attr_names.each do |name|
35
+ define_method "#{name}=" do |new_value|
36
+ @attributes[name] = new_value
37
+ end
38
+ end
39
+ end
40
+ end