ga_example_gem 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +3 -0
- data/lib/ga_example_gem.rb +13 -1
- data/lib/ga_example_gem/client.rb +3 -1
- data/lib/ga_example_gem/configuration.rb +2 -1
- data/lib/ga_example_gem/version.rb +1 -1
- data/spec/ga_example_gem/client_spec.rb +7 -0
- data/spec/spec_helper.rb +8 -0
- metadata +1 -1
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# GaExampleGem
|
2
2
|
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/ga_example_gem.png)](http://badge.fury.io/rb/ga_example_gem)
|
4
|
+
|
3
5
|
[![Coverage Status](https://coveralls.io/repos/tibbon/ga-example-gem/badge.png)](https://coveralls.io/r/tibbon/ga-example-gem)
|
4
6
|
|
5
7
|
[![Build Status](https://travis-ci.org/tibbon/ga-example-gem.png?branch=master)](https://travis-ci.org/tibbon/ga-example-gem)
|
@@ -29,6 +31,7 @@ Setup a new instance:
|
|
29
31
|
```ruby
|
30
32
|
client = GaExampleGem.configure do |config|
|
31
33
|
config.api_key = "your_api_key"
|
34
|
+
config.oauth_key = "foo"
|
32
35
|
end
|
33
36
|
```
|
34
37
|
|
data/lib/ga_example_gem.rb
CHANGED
@@ -3,8 +3,12 @@ require 'ga_example_gem/client'
|
|
3
3
|
|
4
4
|
# This is our GaExampleGem namespace
|
5
5
|
# for our gem
|
6
|
+
|
6
7
|
module GaExampleGem
|
7
|
-
|
8
|
+
# This is weird.
|
9
|
+
# These are now class methods.
|
10
|
+
# So you no longer have to call def self.method_missing
|
11
|
+
class << self
|
8
12
|
# Alias for GaExampleGem::Client.new
|
9
13
|
def new
|
10
14
|
@client ||= GaExampleGem::Client.new
|
@@ -14,6 +18,7 @@ module GaExampleGem
|
|
14
18
|
# This is a weird trick that allows for calling
|
15
19
|
# on methods without calling .new first
|
16
20
|
def method_missing(method, *args, &block)
|
21
|
+
# Normally this raises an error
|
17
22
|
return super unless new.respond_to?(method)
|
18
23
|
new.send(method, *args, &block)
|
19
24
|
end
|
@@ -24,5 +29,12 @@ module GaExampleGem
|
|
24
29
|
def respond_to?(method, include_private=false)
|
25
30
|
new.respond_to?(method, include_private) || super(method, include_private)
|
26
31
|
end
|
32
|
+
|
33
|
+
# The logic for above
|
34
|
+
# 1) Call a method, GaExampleGem.configure
|
35
|
+
# 2) Run .method_missing, because we don't have that method
|
36
|
+
# 3) Name of method is 'configure'
|
37
|
+
# 4) Does a new instance of the client respond to configure?
|
38
|
+
# 5) If yes, create a new client and run the method 'configure'
|
27
39
|
end
|
28
40
|
end
|
@@ -18,7 +18,9 @@ module GaExampleGem
|
|
18
18
|
# This method gets the JSON for a single XKCD comic
|
19
19
|
# by number
|
20
20
|
def get_xkcd(number)
|
21
|
-
|
21
|
+
# This works too
|
22
|
+
# JSON.parse self.class.get("http://xkcd-unofficial-api.herokuapp.com/xkcd?num=#{number}")
|
23
|
+
JSON.parse HTTParty.get("http://xkcd-unofficial-api.herokuapp.com/xkcd?num=#{number}")
|
22
24
|
end
|
23
25
|
end
|
24
26
|
end
|
@@ -8,7 +8,8 @@ module GaExampleGem
|
|
8
8
|
# This is our API key. We aren't really using it
|
9
9
|
# anywhere, but you could imagine using it
|
10
10
|
# with HTTP requests
|
11
|
-
|
11
|
+
# This is tricky, this adds these attr_accessors to the CLASS
|
12
|
+
attr_accessor :api_key, :oauth_key
|
12
13
|
|
13
14
|
# This yields a block that can be iterated on
|
14
15
|
# Its useful for being able to set values to the
|
@@ -10,13 +10,20 @@ describe GaExampleGem::Client do
|
|
10
10
|
|
11
11
|
describe '#get_xkcd' do
|
12
12
|
before do
|
13
|
+
# Intercept an HTTP request that looks like this,
|
14
|
+
# and return a specific JSON file instead for the body
|
13
15
|
stub_request(:get, 'http://xkcd-unofficial-api.herokuapp.com/xkcd?num=1').
|
14
16
|
to_return(body: fixture('single_xkcd.json'))
|
15
17
|
end
|
16
18
|
|
17
19
|
it "fetchs a single xkcd webcomic" do
|
20
|
+
# Make the request
|
18
21
|
comic = @client.get_xkcd(1)
|
22
|
+
|
23
|
+
# Expect that you made a request
|
19
24
|
expect(a_request(:get, 'http://xkcd-unofficial-api.herokuapp.com/xkcd?num=1')).to have_been_made
|
25
|
+
|
26
|
+
# Check the results of the JSON file/body of HTTP request that we intercepted
|
20
27
|
expect(comic.first["title"]).to eq "Barrel - Part 1"
|
21
28
|
end
|
22
29
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
+
# Do these first
|
1
2
|
require 'simplecov'
|
3
|
+
# Coveralls only reports when Travis runs the tests
|
2
4
|
require 'coveralls'
|
3
5
|
|
4
6
|
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
@@ -7,11 +9,15 @@ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
|
7
9
|
]
|
8
10
|
SimpleCov.start
|
9
11
|
|
12
|
+
# Require my gem itself
|
10
13
|
require 'ga_example_gem'
|
14
|
+
|
15
|
+
# Require testing dependencies
|
11
16
|
require 'rspec'
|
12
17
|
require 'webmock/rspec'
|
13
18
|
require 'json'
|
14
19
|
|
20
|
+
# This is where you put additional rSpec configuration
|
15
21
|
RSpec.configure do |config|
|
16
22
|
config.order = 'random'
|
17
23
|
config.expect_with :rspec do |c|
|
@@ -19,10 +25,12 @@ RSpec.configure do |config|
|
|
19
25
|
end
|
20
26
|
end
|
21
27
|
|
28
|
+
# Helper methods for accessing fixture path
|
22
29
|
def fixture_path
|
23
30
|
File.expand_path('../fixtures', __FILE__)
|
24
31
|
end
|
25
32
|
|
33
|
+
# Helper method for accessing a real file in the fixture path
|
26
34
|
def fixture(file)
|
27
35
|
File.new(fixture_path + '/' + file)
|
28
36
|
end
|