restafari 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gemspec CHANGED
@@ -14,7 +14,7 @@ Gem::Specification.new do |s|
14
14
 
15
15
  s.files = `git ls-files`.split("\n")
16
16
  s.test_files = `git ls-files -- {spec}/*`.split("\n")
17
- s.require_paths = %w("lib")
17
+ s.require_paths = %w(lib)
18
18
 
19
19
  s.add_dependency "faraday"
20
20
 
data/Gemfile.lock ADDED
@@ -0,0 +1,36 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ addressable (2.2.8)
5
+ crack (0.3.1)
6
+ diff-lcs (1.1.3)
7
+ faraday (0.8.0)
8
+ multipart-post (~> 1.1)
9
+ multipart-post (1.1.5)
10
+ rack (1.4.1)
11
+ rack-test (0.6.1)
12
+ rack (>= 1.0)
13
+ rake (0.9.2.2)
14
+ rspec (2.10.0)
15
+ rspec-core (~> 2.10.0)
16
+ rspec-expectations (~> 2.10.0)
17
+ rspec-mocks (~> 2.10.0)
18
+ rspec-core (2.10.0)
19
+ rspec-expectations (2.10.0)
20
+ diff-lcs (~> 1.1.3)
21
+ rspec-mocks (2.10.1)
22
+ spork (0.9.1)
23
+ webmock (1.8.7)
24
+ addressable (>= 2.2.7)
25
+ crack (>= 0.1.7)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ faraday
32
+ rack-test
33
+ rake
34
+ rspec
35
+ spork
36
+ webmock
data/README.md CHANGED
@@ -1,4 +1,38 @@
1
1
  restafari
2
2
  =========
3
3
 
4
- A ruby DSL for consuming RESTful APIs
4
+ A ruby DSL for consuming RESTful APIs
5
+
6
+
7
+ usage
8
+ ========
9
+
10
+ ```ruby
11
+ @require "restafari"
12
+ class MyClass
13
+ @include Restafari::Action
14
+
15
+ action :sign_in, "/path", {
16
+ default: "value"
17
+ }
18
+ end
19
+ ```
20
+
21
+ Now, from your code, you simply do:
22
+
23
+ ```ruby
24
+ result = MyClass.sign_in
25
+
26
+ result[:success] == true #you can access the response body via hash-like
27
+ result.success == true #or as a method
28
+ ```
29
+
30
+ You can also hook into the request and add your own filters(i.e for signing and other global params)
31
+
32
+ ```ruby
33
+ Restafari.config.before_request do |params|
34
+ params[:test] = true
35
+ end
36
+ ```
37
+
38
+ this will add the "test" parameter to each request.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler'
2
+ require_relative 'lib/restafari'
3
+ #Bundler::GemHelper.install_tasks
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ task :default => :spec
7
+ task :test => :spec
@@ -0,0 +1,46 @@
1
+ require 'singleton'
2
+
3
+
4
+ module Restafari
5
+ class Configuration
6
+ include Singleton
7
+
8
+ def initialize
9
+
10
+ @defaults = {
11
+ base_url: "",
12
+ http_method: :post,
13
+ scheme: :http,
14
+ before_request: nil,
15
+ }
16
+
17
+ define_singleton_method :defaults do
18
+ @defaults
19
+ end
20
+ end
21
+
22
+ def method_missing(name, *args)
23
+ name_stripped = name.to_s.sub(/[\?=]$/, '').to_sym
24
+ return super unless @defaults.key?(name_stripped)
25
+
26
+ if name.to_s.end_with?('=')
27
+ @defaults[name_stripped] = args.first
28
+ else
29
+ @defaults[name_stripped]
30
+ end
31
+ end
32
+
33
+ def url
34
+ "#{Restafari.config.scheme}://#{Restafari::config.base_url}"
35
+ end
36
+
37
+ def before_request(&block)
38
+ @before_request = block if block_given?
39
+ end
40
+
41
+ def run_before_request_hook(*params)
42
+ @before_request.call(*params)
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,25 @@
1
+ require 'json'
2
+ module Restafari
3
+ class Response
4
+ attr_reader :data, :resp
5
+
6
+ def initialize(resp)
7
+ @resp = resp
8
+ @data = JSON.parse(@resp.body)
9
+ end
10
+
11
+ def [](i)
12
+ case i
13
+ when Symbol
14
+ @data[i.to_s]
15
+ else
16
+ @data[i]
17
+ end
18
+ end
19
+
20
+ def method_missing(method_id, *args)
21
+ return super unless @data.key?(method_id.to_s)
22
+ self[method_id]
23
+ end
24
+ end
25
+ end
@@ -1,3 +1,3 @@
1
1
  module Restafari
2
- VERSION = 0.1
2
+ VERSION = 0.2
3
3
  end
data/lib/restafari.rb ADDED
@@ -0,0 +1,12 @@
1
+ require_relative 'restafari/action.rb'
2
+
3
+ module Restafari
4
+
5
+ autoload :Configuration, 'restafari/configuration'
6
+ autoload :Response, 'restafari/response'
7
+
8
+ def self.config
9
+ Configuration.instance
10
+ end
11
+
12
+ end
@@ -0,0 +1,22 @@
1
+ require 'rubygems'
2
+ require 'rspec'
3
+ require "webmock"
4
+ require 'restafari'
5
+
6
+ RSpec.configure do |config|
7
+ config.treat_symbols_as_metadata_keys_with_true_values = true
8
+ config.run_all_when_everything_filtered = true
9
+ config.filter_run :focus
10
+ end
11
+
12
+
13
+ Restafari.config.base_url = "www.example.com"
14
+ WebMock::stub_request(:post, "http://#{Restafari.config.base_url}/").to_return(:status => 200, :body => "{}", :headers => {})
15
+
16
+ unless defined?(RM_HOME)
17
+ # this is here to allow teamcity to run with spork
18
+ RM_HOME=File.expand_path("~/BuildAgent/plugins/rake-runner")
19
+ $:.unshift(RM_HOME + "/rb/patch/bdd")
20
+ $:.unshift(RM_HOME + "/rb/patch/common")
21
+ end
22
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restafari
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.2'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-13 00:00:00.000000000Z
12
+ date: 2012-05-14 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
16
- requirement: &70133075400800 !ruby/object:Gem::Requirement
16
+ requirement: &70360396333360 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70133075400800
24
+ version_requirements: *70360396333360
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: webmock
27
- requirement: &70133075400040 !ruby/object:Gem::Requirement
27
+ requirement: &70360396332940 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70133075400040
35
+ version_requirements: *70360396332940
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: spork
38
- requirement: &70133075399120 !ruby/object:Gem::Requirement
38
+ requirement: &70360396332520 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70133075399120
46
+ version_requirements: *70360396332520
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rack-test
49
- requirement: &70133075398360 !ruby/object:Gem::Requirement
49
+ requirement: &70360396332100 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70133075398360
57
+ version_requirements: *70360396332100
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rspec
60
- requirement: &70133075397380 !ruby/object:Gem::Requirement
60
+ requirement: &70360396331680 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70133075397380
68
+ version_requirements: *70360396331680
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rake
71
- requirement: &70133075396380 !ruby/object:Gem::Requirement
71
+ requirement: &70360396331260 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,7 +76,7 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70133075396380
79
+ version_requirements: *70360396331260
80
80
  description: Helper class to work with the restful apis
81
81
  email:
82
82
  - - dannifriedland@gmail.com
@@ -87,16 +87,22 @@ files:
87
87
  - .gemspec
88
88
  - .gitignore
89
89
  - Gemfile
90
+ - Gemfile.lock
90
91
  - README.md
92
+ - Rakefile
93
+ - lib/restafari.rb
91
94
  - lib/restafari/action.rb
95
+ - lib/restafari/configuration.rb
96
+ - lib/restafari/response.rb
92
97
  - lib/restafari/version.rb
93
98
  - spec/restafari/action_spec.rb
99
+ - spec/spec_helper.rb
94
100
  homepage: https://github.com/TheGiftsProject/restafari
95
101
  licenses: []
96
102
  post_install_message:
97
103
  rdoc_options: []
98
104
  require_paths:
99
- - ! '"lib"'
105
+ - lib
100
106
  required_ruby_version: !ruby/object:Gem::Requirement
101
107
  none: false
102
108
  requirements: