somefixtures 0.1.6 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -13,6 +13,32 @@ Install
13
13
 
14
14
  Usage
15
15
  -----
16
+
17
+ Create a fixture object with the necessary details. The example given
18
+ is from github, if you need to authenticate, supply your github username and
19
+ password.
20
+
21
+ Format:
22
+
23
+ Fixture.new
24
+
25
+ Takes the following parameters:
26
+
27
+ :save_to
28
+ Location to save to.
29
+
30
+ :format
31
+ Format expected to be returned. (Only used for adding a file extension at the moment.)
32
+
33
+ :base_uri
34
+ The base URI location to the API/Service.
35
+
36
+ :login
37
+ :token
38
+ Your authentication details.
39
+
40
+ Example:
41
+
16
42
  foo = Fixture.new(
17
43
  :save_to => "/Users/jbw/Desktop/",
18
44
  :format => "json"
@@ -20,15 +46,50 @@ Usage
20
46
  :login => "jbw",
21
47
  :token => "account_password" )
22
48
 
23
- foo.add(:get, "/user/search/jbw", "search")
24
- foo.add_get("/user/search/jbw", "search")
49
+ The following shows some examples of adding fixtures to a data structure.
50
+
51
+ Format:
52
+
53
+ add_get
25
54
 
26
- foo.add_get("/user/show/jbw", "show_unauthenticated")
27
- foo.add_get("/user/show", "show_authenticated", true)
28
- foo.add_get("/user/show/jbw/followers", "followers")
55
+
56
+ Takes the following:
57
+
58
+ :route <Hash>
59
+ The API route.
60
+
61
+ output_filename <String>
62
+ The name you want your file to have.
63
+
64
+ authenticate <Boolean>
65
+ Whether you need to authenticate with the server (Uses your :login, :token values).
66
+
67
+ Format:
68
+
69
+ add_post
70
+
71
+ Takes the following:
72
+
73
+ :route <Hash>
74
+ output_filename <String>
75
+ authenticate <Boolean>
76
+ (See add_get)
77
+
78
+ :values <Hash>
79
+ Values to post to the server.
80
+
81
+
82
+ Example:
83
+
84
+ foo.add_get( { :route => "/user/show/jbw" }, "show")
85
+
86
+ foo.add_post({ :route => "/user/follow/jbw" }, "follow", true)
87
+
88
+ foo.add_post({ :route => "/repos/show/jbw/emacs", :values => "values[description]=My Emacs configs" },"repo_update_description", true )
89
+
29
90
 
30
- foo.add(:post, "/user/follow/jbw", "follow", true)
31
- foo.add_post("/user/follow/jbw", "follow", true)
91
+ Once all the routes are added you can execute the following command to fetch
92
+ the fixtures:
32
93
 
33
94
  foo.make_fixtures!
34
95
 
@@ -38,6 +99,5 @@ MIT
38
99
 
39
100
  TODO
40
101
  ----
41
- * Make more generic
42
102
  * Documentation
43
103
 
data/Rakefile CHANGED
@@ -2,8 +2,8 @@ require 'rake/gempackagetask'
2
2
 
3
3
  spec = Gem::Specification.new do |s|
4
4
  s.name = 'somefixtures'
5
- s.version = '0.1.5'
6
- s.date = '2010-07-30'
5
+ s.version = '0.2.0'
6
+ s.date = '2010-08-08'
7
7
  s.description = "Testing fixture automator. Easies the pain of getting sample data for testing."
8
8
  s.summary = s.description
9
9
 
@@ -1,6 +1,8 @@
1
1
  require 'net/http'
2
2
  module SomeFixtures
3
3
  class Fixture
4
+
5
+ attr_accessor :format, :base_uri, :save_to, :login, :token, :fixtures
4
6
 
5
7
  def initialize fixture_info
6
8
  @format = fixture_info[:format]
@@ -12,78 +14,69 @@ module SomeFixtures
12
14
  @fixtures = {}
13
15
  end
14
16
 
15
- def add option, query, name, authenticate=false
16
- @fixtures.each_key { |key| print name == key ? "You have added \"#{key}\" for a file name already. Each filename must be unique!\n" : nil }
17
- @fixtures[name] = { :option => option, :query => query, :authenticate => authenticate }
18
- end
19
-
17
+ def add_post(*args); add_fixture(:post, *args) end
18
+ def add_get(*args); add_fixture(:get, *args) end
20
19
 
21
20
  def make_fixtures!
22
- save query
21
+ save query_fixtures
23
22
  end
24
23
 
25
- def add_post(*args); add(:post, *args) end
26
- def add_get(*args); add(:get, *args) end
27
-
28
24
  private
29
-
30
- def query
25
+ def is_duplicate? name
26
+ value = false
27
+ @fixtures.each_key{ |key| value = true if key == name }
28
+ print "WARNING: You have added a file named: \"#{name}\" already. Each filename must be unique!\n" if value
29
+ return value
30
+ end
31
+ def add_fixture option, query, name, authenticate=false
32
+ return true if is_duplicate? name
33
+ @fixtures[name] = { :option => option, :query => query, :authenticate => authenticate }
34
+ end
35
+ def query_fixtures
31
36
  responses = []
32
- @fixtures.each_key do |name|
33
- if @fixtures[name][:option] == :post
34
- responses << (post name)
37
+ @fixtures.each_key do |fixture|
38
+ if @fixtures[fixture][:option] == :post
39
+ responses << ( post fixture )
35
40
  else
36
- responses << (get name)
41
+ responses << ( get fixture )
37
42
  end
38
43
  end
39
44
  responses
40
45
  end
41
-
42
- def authenticated? name
43
- @authenticate = @fixtures[name][:authenticate];
44
- auth_params
45
- end
46
-
47
- def get name
48
- Net::HTTP.start((split_uri @base_uri)[1]) do |http|
49
- req = Net::HTTP::Get.new((split_uri @base_uri)[2] + @fixtures[name][:query])
50
- if @fixtures[name][:authenticate] == true
51
- req.basic_auth (authenticated? name)[:login], (authenticated? name)[:token]
52
- else
53
- req.basic_auth @login, @token
54
- end
55
- response = http.request(req)
56
- response.body
46
+ def get fixture
47
+ Net::HTTP.start(get_uri[:location]) do |http|
48
+ request = Net::HTTP::Get.new(get_uri[:route] + @fixtures[fixture][:query][:route])
49
+ authenticate_if_values_given fixture, request
50
+ response = http.request(request)
51
+ return response.body
57
52
  end
58
53
  end
59
-
60
- def post name
61
- Net::HTTP.start((split_uri @base_uri)[1]) do |http|
62
- req = Net::HTTP::Post.new((split_uri @base_uri)[2] + @fixtures[name][:query])
63
- req.basic_auth @login, @token
64
- response = http.request(req)
65
- response.body
54
+ def post fixture
55
+ Net::HTTP.start(get_uri[:location]) do |http|
56
+ request = Net::HTTP::Post.new(get_uri[:route] + @fixtures[fixture][:query][:route])
57
+ request.body = @fixtures[fixture][:query][:values]
58
+ authenticate_if_values_given fixture, request
59
+ response = http.request(request)
60
+ return response.body
66
61
  end
67
62
  end
68
-
69
- def split_uri uri
70
- if uri.match(/(http:\/\/)([a-zA-Z.]+)([a-zA-Z\d\/]+)/)
71
- [$1, $2, $3]
63
+ def get_uri
64
+ if @base_uri.match(/(http:\/\/)([a-zA-Z.]+)([a-zA-Z\d\/]+)/)
65
+ { :location => $2, :route => $3 }
72
66
  end
73
67
  end
74
-
75
68
  def save fixtures
76
69
  name = @fixtures.keys
77
-
78
70
  fixtures.each do |f|
79
- File.new(@save_to + name.first + "." + @format, "w").puts f
71
+ File.new(File.join(@save_to, name.first + "." + @format), "w").puts f
80
72
  name.shift
81
- end
82
- end
83
-
84
- def auth_params
85
- if @authenticate; (@login.nil? ? {} : { :login => @login, :token => @token }); end
73
+ return f.gsub("\"", "")
74
+ end
86
75
  end
87
-
76
+ def authenticate_if_values_given fixture, request
77
+ if @fixtures[fixture][:authenticate] == true
78
+ request.basic_auth @login, @token
79
+ end
80
+ end
88
81
  end
89
82
  end
data/somefixtures.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'somefixtures'
3
- s.version = '0.1.6'
4
- s.date = '2010-07-30'
3
+ s.version = '0.2.0'
4
+ s.date = '2010-08-08'
5
5
  s.description = "Testing fixture automator. Easies the pain of getting sample data for testing."
6
6
  s.summary = s.description
7
7
 
metadata CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 6
10
- version: 0.1.6
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jason Watson
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-07-30 00:00:00 +01:00
18
+ date: 2010-08-08 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies: []
21
21