facile 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +4 -5
- data/lib/facile/api.rb +9 -50
- data/lib/facile/api/forward_methods.rb +29 -0
- data/lib/facile/api/http_methods.rb +44 -0
- data/lib/facile/api/request_methods.rb +42 -0
- data/lib/facile/version.rb +1 -1
- data/spec/facile/api_spec.rb +119 -22
- metadata +5 -2
data/README.md
CHANGED
@@ -23,20 +23,19 @@ Or install it yourself as:
|
|
23
23
|
|
24
24
|
## Usage
|
25
25
|
|
26
|
-
TODO: Write usage instructions here
|
27
|
-
|
28
26
|
An example script, to fetch the weather data from the BBC.
|
29
27
|
```ruby
|
30
28
|
require 'facile'
|
31
29
|
|
32
|
-
class Foo
|
33
|
-
|
30
|
+
class Foo
|
31
|
+
include ::Facile::Api
|
32
|
+
url 'http://open.live.bbc.co.uk/weather/feeds/en/{id}/3dayforecast.json'
|
34
33
|
end
|
35
34
|
|
36
35
|
foo = Foo.find('2643743')
|
37
36
|
|
38
37
|
# gets forecast date for the first day.
|
39
|
-
foo.
|
38
|
+
foo.forecast_content.forecasts.first.date
|
40
39
|
```
|
41
40
|
|
42
41
|
## Contributing
|
data/lib/facile/api.rb
CHANGED
@@ -1,55 +1,14 @@
|
|
1
1
|
require 'addressable/template'
|
2
|
-
require '
|
2
|
+
require 'forwardable'
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
require 'facile/api/http_methods'
|
5
|
+
require 'facile/api/request_methods'
|
6
|
+
require 'facile/api/forward_methods'
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
def initialize(response = {})
|
13
|
-
@response = response if !response.empty?
|
14
|
-
end
|
15
|
-
|
16
|
-
def method_missing(method, *args, &block)
|
17
|
-
raise NoMethodError, "'#{method}' doesn't exist" if response.nil?
|
18
|
-
|
19
|
-
response.send(method)
|
20
|
-
end
|
21
|
-
|
22
|
-
def find(id, options = {})
|
23
|
-
url = generate_url(id, self.class.url)
|
24
|
-
req_options = request_options(url)
|
25
|
-
|
26
|
-
self.class.new(create_request(req_options).body)
|
27
|
-
end
|
28
|
-
|
29
|
-
protected
|
30
|
-
attr_reader :response
|
31
|
-
|
32
|
-
def create_request(req_options = {})
|
33
|
-
req = Facile::Request.new(req_options)
|
34
|
-
|
35
|
-
req.go
|
36
|
-
end
|
37
|
-
|
38
|
-
def generate_url(id, url)
|
39
|
-
template = Addressable::Template.new(url)
|
40
|
-
expanded = template.partial_expand(:id => id)
|
41
|
-
|
42
|
-
expanded.pattern
|
43
|
-
end
|
44
|
-
|
45
|
-
def request_options(url)
|
46
|
-
{
|
47
|
-
:url => url,
|
48
|
-
:method => :get,
|
49
|
-
:headers => self.class.headers || {},
|
50
|
-
:options => self.class.options || {},
|
51
|
-
:body => self.class.body,
|
52
|
-
:params => self.class.params
|
53
|
-
}
|
8
|
+
module Facile::Api
|
9
|
+
def self.included(base)
|
10
|
+
base.extend(::Facile::Api::HttpMethods)
|
11
|
+
base.extend(::Facile::Api::RequestMethods)
|
12
|
+
base.extend(::Facile::Api::ForwardMethods)
|
54
13
|
end
|
55
14
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
|
3
|
+
module Facile::Api
|
4
|
+
module ForwardMethods
|
5
|
+
include Forwardable
|
6
|
+
|
7
|
+
def self.extended(base)
|
8
|
+
base.instance_variable_set(:@response, nil)
|
9
|
+
base.instance_eval { attr_accessor :response }
|
10
|
+
end
|
11
|
+
|
12
|
+
def create_instance(body = nil)
|
13
|
+
class_instance = new
|
14
|
+
class_instance.response = body
|
15
|
+
|
16
|
+
create_delegators(class_instance)
|
17
|
+
class_instance
|
18
|
+
end
|
19
|
+
|
20
|
+
def create_delegators(class_instance)
|
21
|
+
if class_instance.response && class_instance.response.respond_to?(:keys)
|
22
|
+
class_instance.response.keys.each do |key|
|
23
|
+
class_instance.class.def_delegator :@response, key.to_sym
|
24
|
+
class_instance.class.def_delegator :@response, "#{key}=".to_sym
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Facile::Api
|
2
|
+
module HttpMethods
|
3
|
+
def self.extended(base)
|
4
|
+
base.instance_variable_set(:@defaults, {})
|
5
|
+
end
|
6
|
+
|
7
|
+
def url(url = nil)
|
8
|
+
return defaults[:url] if url.nil?
|
9
|
+
|
10
|
+
defaults[:url] = url
|
11
|
+
end
|
12
|
+
|
13
|
+
def body(body = nil)
|
14
|
+
return defaults[:body] if body.nil?
|
15
|
+
|
16
|
+
defaults[:body] = body
|
17
|
+
end
|
18
|
+
|
19
|
+
def headers(headers = nil)
|
20
|
+
return defaults[:headers] if headers.nil?
|
21
|
+
|
22
|
+
defaults[:headers] = headers
|
23
|
+
end
|
24
|
+
|
25
|
+
def params(params = nil)
|
26
|
+
return defaults[:params] if params.nil?
|
27
|
+
|
28
|
+
defaults[:params] = params
|
29
|
+
end
|
30
|
+
|
31
|
+
def options(options = nil)
|
32
|
+
return defaults[:options] if options.nil?
|
33
|
+
|
34
|
+
defaults[:options] = options
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
protected
|
39
|
+
|
40
|
+
def defaults
|
41
|
+
@defaults
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
|
3
|
+
module Facile::Api
|
4
|
+
module RequestMethods
|
5
|
+
include Forwardable
|
6
|
+
|
7
|
+
def find(*args)
|
8
|
+
id, options = args[0] || nil, args[1] || {}
|
9
|
+
|
10
|
+
url = generate_url(id, self.url)
|
11
|
+
req_options = request_options(url)
|
12
|
+
|
13
|
+
body = create_request(req_options).body
|
14
|
+
|
15
|
+
create_instance(body)
|
16
|
+
end
|
17
|
+
|
18
|
+
def create_request(req_options = {})
|
19
|
+
req = Facile::Request.new(req_options)
|
20
|
+
|
21
|
+
req.go
|
22
|
+
end
|
23
|
+
|
24
|
+
def generate_url(id, url)
|
25
|
+
template = Addressable::Template.new(url)
|
26
|
+
expanded = template.partial_expand(:id => id)
|
27
|
+
|
28
|
+
expanded.pattern
|
29
|
+
end
|
30
|
+
|
31
|
+
def request_options(url)
|
32
|
+
{
|
33
|
+
:url => url,
|
34
|
+
:method => :get,
|
35
|
+
:headers => self.headers || {},
|
36
|
+
:options => self.options || {},
|
37
|
+
:body => self.body,
|
38
|
+
:params => self.params || {}
|
39
|
+
}
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/facile/version.rb
CHANGED
data/spec/facile/api_spec.rb
CHANGED
@@ -1,38 +1,135 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
+
require 'hashie'
|
2
3
|
require 'faraday'
|
3
4
|
|
4
|
-
describe
|
5
|
-
|
6
|
-
|
5
|
+
describe Facile::Api::HttpMethods do
|
6
|
+
before do
|
7
|
+
class TestClass
|
8
|
+
include ::Facile::Api
|
9
|
+
url 'http://bbc.co.uk'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'able to set values' do
|
14
|
+
it 'http methods work' do
|
15
|
+
# there's no real need to test this any further
|
16
|
+
expect(TestClass.url).to eq('http://bbc.co.uk')
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'does not share state' do
|
21
|
+
it 'other classes using the same method have different values' do
|
22
|
+
class Zomg
|
23
|
+
include ::Facile::Api
|
24
|
+
end
|
25
|
+
|
26
|
+
expect(TestClass.url).to eq('http://bbc.co.uk')
|
27
|
+
expect(Zomg.url).to eq(nil)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
7
31
|
|
8
|
-
|
32
|
+
describe Facile::Api::RequestMethods do
|
33
|
+
before do
|
34
|
+
class TestClass
|
35
|
+
include ::Facile::Api
|
36
|
+
|
37
|
+
url 'http://bbc.co.uk'
|
38
|
+
end
|
9
39
|
end
|
10
40
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
41
|
+
context 'request options' do
|
42
|
+
it 'can be generated' do
|
43
|
+
expected = {
|
44
|
+
:url => 'http://bbc.co.uk',
|
45
|
+
:method => :get,
|
46
|
+
:headers => {},
|
47
|
+
:params => {},
|
48
|
+
:body => nil,
|
49
|
+
:options => {}
|
50
|
+
}
|
51
|
+
|
52
|
+
expect(TestClass.request_options(TestClass.url)).to eq(expected)
|
53
|
+
end
|
54
|
+
end
|
17
55
|
|
56
|
+
context 'generate url' do
|
57
|
+
it 'can insert placeholder' do
|
58
|
+
class OtherTestClass
|
59
|
+
include ::Facile::Api
|
18
60
|
|
19
|
-
|
20
|
-
|
61
|
+
url 'http://bbc.co.uk/{id}'
|
62
|
+
end
|
21
63
|
|
22
|
-
|
64
|
+
generated_url = OtherTestClass.generate_url(1, OtherTestClass.url)
|
23
65
|
|
24
|
-
|
66
|
+
expect(generated_url).to eq('http://bbc.co.uk/1')
|
67
|
+
end
|
25
68
|
end
|
26
69
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
70
|
+
context 'request things' do
|
71
|
+
it 'can request things' do
|
72
|
+
faraday_response = Faraday::Response.new({
|
73
|
+
:method => :get,
|
74
|
+
:response_headers => {'content-type' => 'text/plain'},
|
75
|
+
:body => 'foo',
|
76
|
+
:url => 'http://bbc.com'
|
77
|
+
})
|
78
|
+
|
79
|
+
Faraday::Connection.any_instance.stub(:get => faraday_response)
|
80
|
+
|
81
|
+
inst = TestClass.find()
|
82
|
+
|
83
|
+
expect(inst).to be_kind_of(TestClass)
|
84
|
+
expect(inst.response).to eq('foo')
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
describe Facile::Api::ForwardMethods do
|
90
|
+
before do
|
91
|
+
class TestClass
|
92
|
+
include ::Facile::Api
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
context 'can create instance' do
|
97
|
+
it 'create instance of test class' do
|
98
|
+
inst = TestClass.create_instance
|
99
|
+
|
100
|
+
expect(inst).to be_kind_of(TestClass)
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'can create instance with string data' do
|
104
|
+
inst = TestClass.create_instance('test')
|
105
|
+
|
106
|
+
expect(inst.response).to eq('test')
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'can create instance with hash' do
|
110
|
+
hash = Hashie::Mash.new({:foo => 'bar'})
|
111
|
+
inst = TestClass.create_instance(hash)
|
112
|
+
|
113
|
+
expect(inst.response).to eq(hash)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
context 'can delegate methods' do
|
118
|
+
it 'can read from a method' do
|
119
|
+
hash = Hashie::Mash.new({:foo => 'bar'})
|
120
|
+
inst = TestClass.create_instance(hash)
|
121
|
+
|
122
|
+
expect(inst.foo).to eq('bar')
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'can write from a method' do
|
126
|
+
hash = Hashie::Mash.new({:foo => 'bar'})
|
127
|
+
inst = TestClass.create_instance(hash)
|
33
128
|
|
34
|
-
|
129
|
+
expect(inst.foo).to eq('bar')
|
35
130
|
|
36
|
-
|
131
|
+
inst.foo = 'zomg'
|
132
|
+
expect(inst.foo).to eq('zomg')
|
133
|
+
end
|
37
134
|
end
|
38
135
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: facile
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.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-
|
12
|
+
date: 2013-12-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: faraday
|
@@ -190,6 +190,9 @@ files:
|
|
190
190
|
- facile.gemspec
|
191
191
|
- lib/facile.rb
|
192
192
|
- lib/facile/api.rb
|
193
|
+
- lib/facile/api/forward_methods.rb
|
194
|
+
- lib/facile/api/http_methods.rb
|
195
|
+
- lib/facile/api/request_methods.rb
|
193
196
|
- lib/facile/request.rb
|
194
197
|
- lib/facile/response.rb
|
195
198
|
- lib/facile/response/wrapper.rb
|