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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3931bd443ce1a3d9701ea5476ef05918700fd9a7
4
- data.tar.gz: 08f14997f3102779b09f0598448f68ba37a58d8e
3
+ metadata.gz: 738ff177c3d4c3d3a5a9de5263708d69657e9fa0
4
+ data.tar.gz: 251d96c30a9f2b4d21a79fabebffeb20d2cee244
5
5
  SHA512:
6
- metadata.gz: 627844734297c3bb080eb0194ec922d24a5001c750752ae3deb88393c5c8e002943a1ae4283e0bbe8997c08ef9c75c1aa0ba3f3c95108a97e4ed4fcb8c09ec3a
7
- data.tar.gz: 3065622d9b7829c1281249ac7d8e6e49065b5535b029bc09d3d3351b0b41d93ed1f715c6ca4f0055844cadb1b5490bb5b70a52a7ef56edc2878dbd3d69d3b16e
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", '/products.json' )
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", '/users/new', my_params)
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
 
@@ -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 for internal use to give http method - Get, Put, Post, Delete
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
- 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?"}
51
+ true
42
52
  else
43
- {'error' => "there seems to be an error in the server, please try again"}
53
+ false
44
54
  end
55
+
45
56
  end
46
- end
47
57
 
48
58
 
59
+
60
+ end
61
+
49
62
  end
@@ -3,9 +3,9 @@ module AnyApi
3
3
  attr_accessor :username, :password, :api_base_url, :log_level
4
4
 
5
5
  def initialize
6
+ self.api_base_url = nil
6
7
  self.username = nil
7
8
  self.password = nil
8
- self.api_base_url = nil
9
9
  self.log_level = 'info'
10
10
  end
11
11
  end
@@ -1,3 +1,3 @@
1
1
  module AnyApi
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -4,12 +4,24 @@ class AnyApiTest < Minitest::Test
4
4
 
5
5
 
6
6
  def test_connection
7
- response = AnyApi::Request.new( "Get", '/tickets.json')
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
@@ -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.2
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-09 00:00:00.000000000 Z
11
+ date: 2014-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler