gistation 1.0.0 → 1.1.0

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/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ .rvmrc
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Gistation
2
2
 
3
- Gistation provides an API layer for reading an user Github Gists detailed info
3
+ Gistation provides an API layer to manipulate Github Gists
4
4
 
5
5
  ## Installation
6
6
 
@@ -35,6 +35,11 @@ compdef _git gst=git-status
35
35
  alias gpl='git pull'
36
36
  ...
37
37
  =end
38
+
39
+ gist_files = []
40
+ gist_files << Gistation::GistFile.new filename: 'file1.txt', content: 'String file contents'
41
+
42
+ Gistation::Gist.create gist_files, description: 'the description for this gist'
38
43
  ```
39
44
 
40
45
  ## Avaiable Properties
data/gistation.gemspec CHANGED
@@ -4,8 +4,8 @@ require File.expand_path('../lib/gistation/version', __FILE__)
4
4
  Gem::Specification.new do |gem|
5
5
  gem.authors = ["Lukas Alexandre"]
6
6
  gem.email = ["lukeskytm@gmail.com"]
7
- gem.description = %q{Github Gist read layer}
8
- gem.summary = %q{Gistation provides an API layer for reading an user Github Gists detailed info}
7
+ gem.description = %q{Gistation provides an API layer to manipulate Github Gists}
8
+ gem.summary = %q{Gistation provides an API layer to manipulate Github Gists}
9
9
  gem.homepage = "https://github.com/lukelex/gistation"
10
10
 
11
11
  gem.files = `git ls-files`.split($\)
@@ -17,6 +17,8 @@ Gem::Specification.new do |gem|
17
17
 
18
18
  gem.add_dependency 'json', '~> 1.7.6'
19
19
 
20
- gem.add_development_dependency 'minitest', '~> 4.5.0'
21
- gem.add_development_dependency 'fakeweb', '~> 1.3.0'
20
+ gem.add_development_dependency 'minitest', '~> 4.5.0'
21
+ gem.add_development_dependency 'fakeweb', '~> 1.3.0'
22
+ gem.add_development_dependency 'turn', '~> 0.9.6'
23
+ gem.add_development_dependency 'pry'
22
24
  end
data/lib/gistation.rb CHANGED
@@ -7,10 +7,7 @@ require 'gistation/gist_user'
7
7
 
8
8
  module Gistation
9
9
  def self.request_to_gist(uri)
10
- http = Net::HTTP.new(uri.host, uri.port)
11
- http.use_ssl = true if uri.scheme == 'https'
12
-
13
- response = http.start do |http|
10
+ response = setup_https(uri).start do |http|
14
11
  http.request Net::HTTP::Get.new(uri.request_uri)
15
12
  end
16
13
 
@@ -18,4 +15,30 @@ module Gistation
18
15
  return response.body unless block_given?
19
16
  yield JSON.parse(response.body)
20
17
  end
18
+
19
+ def self.send_to_gist(uri, params)
20
+ response = setup_https(uri).start do |http|
21
+ req = Net::HTTP::Post.new(uri.request_uri)
22
+
23
+ # req.basic_auth user, password
24
+ req.body = params
25
+
26
+ http.request req
27
+ end
28
+
29
+ yield response if block_given?
30
+ return response
31
+ end
32
+
33
+ def self.asd
34
+ file = Gistation::GistFile.new filename: 'teste.txt', content: 'rails console'
35
+ Gistation::Gist.create [file], public: true
36
+ end
37
+
38
+ private
39
+ def self.setup_https(uri)
40
+ http = Net::HTTP.new uri.host, uri.port
41
+ http.use_ssl = true if uri.scheme == 'https'
42
+ http
43
+ end
21
44
  end
@@ -1,4 +1,6 @@
1
- require_relative 'gist_file'
1
+ require 'gistation/gist_file'
2
+
3
+ require 'pry'
2
4
 
3
5
  class Gistation::Gist
4
6
  FIELDS = [
@@ -10,36 +12,81 @@ class Gistation::Gist
10
12
 
11
13
  attr_accessor *FIELDS
12
14
 
13
- attr
14
-
15
- def initialize(params={})
15
+ def initialize(files, params={})
16
16
  params.each do |key, value|
17
- if FIELDS.include? key
17
+ if FIELDS.include? key.to_sym
18
18
  self.send "#{key}=", value
19
19
  end
20
20
  end
21
21
 
22
- @raw_files = params['files']
22
+ @files = files
23
+ end
24
+
25
+ def self.build(params={})
26
+ inst = new nil, params
27
+
28
+ inst.instance_variable_set "@raw_files", params['files']
29
+
30
+ inst
23
31
  end
24
32
 
25
33
  def self.gists_for(user)
26
34
  self.request_user_gists_info user
27
35
  end
28
36
 
37
+ def self.create(files, opts={ public: true })
38
+ self.create_remote_gist new files, opts
39
+ end
40
+
29
41
  def files
30
42
  @files ||= Gistation::GistFile.from_hash @raw_files
31
43
  end
32
44
 
45
+ def to_json
46
+ {
47
+ 'description' => self.description,
48
+ 'public' => self.public,
49
+ 'files' => self.files_as_json
50
+ }.to_json
51
+ end
52
+
53
+ def fill_with(hash)
54
+ hash.each_pair do |key, value|
55
+ if FIELDS.include? key.to_sym
56
+ self.send "#{key}=", value
57
+ end
58
+ end
59
+ end
60
+
33
61
  protected
62
+ def files_as_json
63
+ files_json = {}
64
+ self.files.each do |file|
65
+ files_json.merge! file.to_json(false)
66
+ end
67
+ files_json
68
+ end
69
+
34
70
  def self.request_user_gists_info(user)
35
71
  uri = URI("https://api.github.com/users/#{user.login}/gists")
36
72
 
37
73
  Gistation.request_to_gist uri do |response|
38
- gists = []
39
- response.each do |gist_info|
40
- gists << Gistation::Gist.new(gist_info)
41
- end
42
- gists
74
+ tap_all response
75
+ end
76
+ end
77
+
78
+ def self.create_remote_gist(gist)
79
+ uri = URI('https://api.github.com/gists')
80
+
81
+ Gistation.send_to_gist uri, gist.to_json do |response|
82
+ gist.fill_with JSON.parse(response.body)
83
+ return gist
84
+ end
85
+ end
86
+
87
+ def self.tap_all(response)
88
+ response.map do |gist_info|
89
+ build gist_info
43
90
  end
44
91
  end
45
92
  end
@@ -1,15 +1,21 @@
1
1
  class Gistation::GistFile
2
2
  FIELDS = [
3
3
  :filename, :type, :language,
4
- :size, :raw_url
4
+ :size, :raw_url, :content
5
5
  ]
6
6
 
7
7
  attr_accessor *FIELDS
8
8
 
9
9
  def initialize(params={})
10
- params.values.first.each do |key, value|
10
+ if params.values.first.is_a? Hash
11
+ fields = params.values.first
12
+ else
13
+ fields = params
14
+ end
15
+
16
+ fields.each do |key, value|
11
17
  if FIELDS.include? key.to_sym
12
- instance_variable_set "@#{key}", value
18
+ send "#{key}=", value
13
19
  end
14
20
  end
15
21
  end
@@ -23,4 +29,13 @@ class Gistation::GistFile
23
29
  def content
24
30
  @content ||= Gistation.request_to_gist URI(self.raw_url)
25
31
  end
32
+
33
+ def to_json(parse=true)
34
+ json_str = {
35
+ filename => {
36
+ 'content' => self.content
37
+ }
38
+ }
39
+ return parse ? json_str.to_json : json_str
40
+ end
26
41
  end
@@ -1,3 +1,3 @@
1
1
  module Gistation
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
data/test/aux_methods.rb CHANGED
@@ -16,6 +16,10 @@ module AuxMethods
16
16
  FakeWeb.register_uri(:put, uri, :body => response)
17
17
  end
18
18
 
19
+ def fake_post_url(uri, response)
20
+ FakeWeb.register_uri(:post, uri, :body => response)
21
+ end
22
+
19
23
  def with_params(url, params)
20
24
  url.request_uri + "?" + params.map {|k,v| CGI.escape(k.to_s)+'='+CGI.escape(v.to_s) }.join("&")
21
25
  end
@@ -0,0 +1 @@
1
+ {"file1.txt":{"content":"String file contents"}}
@@ -0,0 +1 @@
1
+ {"url":"https://api.github.com/gists/4661049","history":[{"url":"https://api.github.com/gists/4661049/fab9671e3e85ea30e7913bca05f3081ab6ca8b14","committed_at":"2013-01-29T02:02:57Z","user":null,"version":"fab9671e3e85ea30e7913bca05f3081ab6ca8b14","change_status":{"total":1,"additions":1,"deletions":0}}],"forks_url":"https://api.github.com/gists/4661049/forks","user":null,"updated_at":"2013-01-29T02:02:57Z","commits_url":"https://api.github.com/gists/4661049/commits","comments":0,"git_pull_url":"https://gist.github.com/4661049.git","public":true,"comments_url":"https://api.github.com/gists/4661049/comments","git_push_url":"https://gist.github.com/4661049.git","created_at":"2013-01-29T02:02:57Z","description":"the description for this gist","html_url":"https://gist.github.com/4661049","id":"4661049","forks":[],"files":{"teste.txt":{"type":"text/plain","filename":"teste.txt","raw_url":"https://gist.github.com/raw/4661049/7ee9562ef17fe31943a87127ee834726ab9f80b6/teste.txt","content":"rails console","size":13,"language":null}}}
@@ -0,0 +1 @@
1
+ {"description":"the description for this gist","public":true,"files":{"file1.txt":{"content":"String file contents"}}}
@@ -0,0 +1,9 @@
1
+ require_relative 'test_helper'
2
+
3
+ describe Gistation::GistFile do
4
+ describe '#to_json' do
5
+ it 'simple text' do
6
+ custom_gist_file.to_json.must_equal gist_file_to_json
7
+ end
8
+ end
9
+ end
data/test/gist_test.rb CHANGED
@@ -40,4 +40,34 @@ describe Gistation::Gist do
40
40
  gist_file.content.must_equal gist_file_content
41
41
  end
42
42
  end
43
+
44
+ describe '#to_json' do
45
+ let (:gist) do
46
+ Gistation::Gist.new(
47
+ [custom_gist_file],
48
+ description: 'the description for this gist',
49
+ public: true
50
+ )
51
+ end
52
+
53
+ it 'with one file' do
54
+ gist.to_json.must_equal gist_to_json
55
+ end
56
+ end
57
+
58
+ describe '.create' do
59
+ it 'creating a public remotely' do
60
+ created_gist = Gistation::Gist.create [custom_gist_file], description: 'the description for this gist'
61
+ created_gist.url.must_equal 'https://api.github.com/gists/4661049'
62
+ created_gist.description.must_equal 'the description for this gist'
63
+ created_gist.forks_url.must_equal 'https://api.github.com/gists/4661049/forks'
64
+ created_gist.updated_at.must_equal '2013-01-29T02:02:57Z'
65
+ created_gist.html_url.must_equal 'https://gist.github.com/4661049'
66
+ created_gist.id.must_equal '4661049'
67
+ created_gist.public.must_equal true
68
+ created_gist.comments_url.must_equal 'https://api.github.com/gists/4661049/comments'
69
+ created_gist.git_push_url.must_equal 'https://gist.github.com/4661049.git'
70
+ created_gist.created_at.must_equal '2013-01-29T02:02:57Z'
71
+ end
72
+ end
43
73
  end
data/test/test_helper.rb CHANGED
@@ -11,12 +11,20 @@ class MiniTest::Spec
11
11
  let (:gist_user) { Gistation::GistUser.new 'lukelex' }
12
12
  let (:gist) { gist_user.gists.first }
13
13
  let (:gist_file) { gist.files.first }
14
+ let (:custom_gist_file) do
15
+ Gistation::GistFile.new filename: 'file1.txt', content: 'String file contents'
16
+ end
14
17
 
15
18
  let (:gist_user_response) { fetch_fixture_path('gist_user.json') }
16
19
  let (:user_gists_response) { fetch_fixture_path('user_gists.json') }
17
20
  let (:gist_file_content_response) { fetch_fixture_path('gist_file_content.txt') }
21
+ let (:gist_response) { fetch_fixture_path('gist_response.json') }
22
+ let (:gist_file_to_json_path) { fetch_fixture_path('gist_file_to_json.json') }
23
+ let (:gist_to_json_path) { fetch_fixture_path('gist_to_json.json') }
18
24
 
19
25
  let (:gist_file_content) { File.read gist_file_content_response }
26
+ let (:gist_file_to_json) { File.read gist_file_to_json_path }
27
+ let (:gist_to_json) { File.read gist_to_json_path }
20
28
 
21
29
  before do
22
30
  FakeWeb.allow_net_connect = false
@@ -24,5 +32,6 @@ class MiniTest::Spec
24
32
  fake_get_url 'https://api.github.com/users/lukelex', gist_user_response
25
33
  fake_get_url 'https://api.github.com/users/lukelex/gists', user_gists_response
26
34
  fake_get_url 'https://gist.github.com/raw/4643343/f77f1e49e1aece457b0095ac847afc01af2d335d/git.plugin.zsh', gist_file_content_response
35
+ fake_post_url 'https://api.github.com/gists', gist_response
27
36
  end
28
37
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gistation
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-27 00:00:00.000000000 Z
12
+ date: 2013-01-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -59,7 +59,39 @@ dependencies:
59
59
  - - ~>
60
60
  - !ruby/object:Gem::Version
61
61
  version: 1.3.0
62
- description: Github Gist read layer
62
+ - !ruby/object:Gem::Dependency
63
+ name: turn
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 0.9.6
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 0.9.6
78
+ - !ruby/object:Gem::Dependency
79
+ name: pry
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Gistation provides an API layer to manipulate Github Gists
63
95
  email:
64
96
  - lukeskytm@gmail.com
65
97
  executables: []
@@ -79,8 +111,12 @@ files:
79
111
  - lib/gistation/version.rb
80
112
  - test/aux_methods.rb
81
113
  - test/fixtures/gist_file_content.txt
114
+ - test/fixtures/gist_file_to_json.json
115
+ - test/fixtures/gist_response.json
116
+ - test/fixtures/gist_to_json.json
82
117
  - test/fixtures/gist_user.json
83
118
  - test/fixtures/user_gists.json
119
+ - test/gist_file_test.rb
84
120
  - test/gist_test.rb
85
121
  - test/gist_user_test.rb
86
122
  - test/test_helper.rb
@@ -107,13 +143,16 @@ rubyforge_project:
107
143
  rubygems_version: 1.8.25
108
144
  signing_key:
109
145
  specification_version: 3
110
- summary: Gistation provides an API layer for reading an user Github Gists detailed
111
- info
146
+ summary: Gistation provides an API layer to manipulate Github Gists
112
147
  test_files:
113
148
  - test/aux_methods.rb
114
149
  - test/fixtures/gist_file_content.txt
150
+ - test/fixtures/gist_file_to_json.json
151
+ - test/fixtures/gist_response.json
152
+ - test/fixtures/gist_to_json.json
115
153
  - test/fixtures/gist_user.json
116
154
  - test/fixtures/user_gists.json
155
+ - test/gist_file_test.rb
117
156
  - test/gist_test.rb
118
157
  - test/gist_user_test.rb
119
158
  - test/test_helper.rb