any_api 0.0.2 → 0.0.3
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.
- checksums.yaml +4 -4
- data/README.md +23 -4
- data/lib/any_api.rb +21 -8
- data/lib/any_api/configuration.rb +1 -1
- data/lib/any_api/version.rb +1 -1
- data/test/any_api_test.rb +13 -1
- data/test/test_helper.rb +1 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 738ff177c3d4c3d3a5a9de5263708d69657e9fa0
|
4
|
+
data.tar.gz: 251d96c30a9f2b4d21a79fabebffeb20d2cee244
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 99e00542da16e16108b47314cb48079d66ff2f9efc8932021c65ecbaad6f89f7f121b63fbd0f0f2acba31190537f7de8d3a669025ad7a198079dae3d4fdb88c7
|
7
|
+
data.tar.gz: 151d3eb3ed2456d022a8158ff8b38525b923388b695d36cce4de8cd01dd749f5e236e5f11b8da3414d4355715d3472ca11109ac8320aad8327b4fbb75e616525
|
data/README.md
CHANGED
@@ -26,17 +26,18 @@ If you are going to use this with Rails. Add bellow code your application.rb fil
|
|
26
26
|
|
27
27
|
```ruby
|
28
28
|
AnyApi.configure do |config|
|
29
|
+
#please dont put a "/" at the end of the api_base_url
|
30
|
+
config.api_base_url = "https://iamfree.com/api/v1"
|
31
|
+
#Dont need bellow authentication information if the API does not need username and password
|
29
32
|
config.username = "me@example.com"
|
30
33
|
config.password = "my-sectret-password"
|
31
|
-
config.api_base_url = "https://iamfree.com/api/v1"
|
32
|
-
#please dont put a "/" at the end of the api_base_url
|
33
34
|
end
|
34
35
|
```
|
35
36
|
|
36
37
|
Then you can call any API with
|
37
38
|
|
38
39
|
```ruby
|
39
|
-
response = AnyApi::Request.new("Get", '
|
40
|
+
response = AnyApi::Request.new("Get", 'products.json' )
|
40
41
|
```
|
41
42
|
|
42
43
|
The first parameter is the HTTP method. Secondly it is the url endpoint.
|
@@ -46,7 +47,7 @@ If you want to do Post or Update calls please send parameters with your request
|
|
46
47
|
|
47
48
|
```ruby
|
48
49
|
my_params = {"year"=>"2014", "country"=>"Australia", "first_name"=>"True", "last_name"=>"Colours"}
|
49
|
-
response = AnyApi::Request.new( "Post", '
|
50
|
+
response = AnyApi::Request.new( "Post", 'users/new', my_params)
|
50
51
|
```
|
51
52
|
|
52
53
|
To parse the response
|
@@ -55,6 +56,24 @@ To parse the response
|
|
55
56
|
response.parser_response
|
56
57
|
```
|
57
58
|
|
59
|
+
|
60
|
+
If you want assert that the API call is a success before save your information database you could use the is_ok? method
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
if response.is_ok?
|
64
|
+
good_json = response.parser_response
|
65
|
+
......
|
66
|
+
else
|
67
|
+
bad_json_error_message = response.parser_response
|
68
|
+
.....
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
```
|
73
|
+
|
74
|
+
|
75
|
+
|
76
|
+
|
58
77
|
Thanks
|
59
78
|
|
60
79
|
|
data/lib/any_api.rb
CHANGED
@@ -17,7 +17,7 @@ module AnyApi
|
|
17
17
|
uri = URI.parse("#{AnyApi.configuration.api_base_url}/#{endpoint}")
|
18
18
|
|
19
19
|
res = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
|
20
|
-
# a safe eval as only
|
20
|
+
# a safe eval as only one of the above HTTPMETHODS would be allowed ie: Get, Put, Post, Delete
|
21
21
|
request = eval "Net::HTTP::#{http_method if HTTPMETHODS.include?(http_method)}.new uri"
|
22
22
|
request.basic_auth(AnyApi.configuration.username, AnyApi.configuration.password)
|
23
23
|
request["Content-Type"] = "application/json"
|
@@ -32,18 +32,31 @@ module AnyApi
|
|
32
32
|
|
33
33
|
|
34
34
|
def parser_response
|
35
|
+
case apiresult
|
36
|
+
when Net::HTTPSuccess
|
37
|
+
JSON.parse apiresult.body
|
38
|
+
when Net::HTTPUnauthorized
|
39
|
+
{'error' => "#{ apiresult.message}: username and password set and correct?"}
|
40
|
+
when Net::HTTPServerError
|
41
|
+
{'error' => "#{ apiresult.message}: try again later?"}
|
42
|
+
else
|
43
|
+
{'error' => "there seems to be an error in the server, please try again"}
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
def is_ok?
|
35
49
|
case apiresult
|
36
50
|
when Net::HTTPSuccess
|
37
|
-
|
38
|
-
when Net::HTTPUnauthorized
|
39
|
-
{'error' => "#{ apiresult.message}: username and password set and correct?"}
|
40
|
-
when Net::HTTPServerError
|
41
|
-
{'error' => "#{ apiresult.message}: try again later?"}
|
51
|
+
true
|
42
52
|
else
|
43
|
-
|
53
|
+
false
|
44
54
|
end
|
55
|
+
|
45
56
|
end
|
46
|
-
end
|
47
57
|
|
48
58
|
|
59
|
+
|
60
|
+
end
|
61
|
+
|
49
62
|
end
|
data/lib/any_api/version.rb
CHANGED
data/test/any_api_test.rb
CHANGED
@@ -4,12 +4,24 @@ class AnyApiTest < Minitest::Test
|
|
4
4
|
|
5
5
|
|
6
6
|
def test_connection
|
7
|
-
response = AnyApi::Request.new( "Get", '
|
7
|
+
response = AnyApi::Request.new( "Get", 'tickets.json')
|
8
8
|
#p response
|
9
9
|
#p response.apiresult.body
|
10
10
|
p response.parser_response
|
11
|
+
|
11
12
|
assert_equal "200", response.apiresult.code
|
13
|
+
assert response.is_ok?
|
12
14
|
end
|
13
15
|
|
14
16
|
|
17
|
+
def test_faliour
|
18
|
+
|
19
|
+
AnyApi.configure do |config|
|
20
|
+
config.api_base_url = "https://iamfree.com/api/v1"
|
21
|
+
end
|
22
|
+
response = AnyApi::Request.new( "Get", 'hello.json')
|
23
|
+
p response.parser_response
|
24
|
+
refute response.is_ok?
|
25
|
+
end
|
26
|
+
|
15
27
|
end
|
data/test/test_helper.rb
CHANGED
@@ -14,8 +14,7 @@ require_relative "../lib/any_api"
|
|
14
14
|
secretfile = YAML.load_file("#{Dir.pwd}/test/fixtures/secrets.yml")
|
15
15
|
|
16
16
|
AnyApi.configure do |config|
|
17
|
+
config.api_base_url = secretfile["conf"]["api_base_url"]
|
17
18
|
config.username = secretfile["conf"]["username"]
|
18
19
|
config.password = secretfile["conf"]["password"]
|
19
|
-
config.api_base_url = secretfile["conf"]["api_base_url"]
|
20
|
-
|
21
20
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: any_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- anthony
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|