any_api 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3931bd443ce1a3d9701ea5476ef05918700fd9a7
4
+ data.tar.gz: 08f14997f3102779b09f0598448f68ba37a58d8e
5
+ SHA512:
6
+ metadata.gz: 627844734297c3bb080eb0194ec922d24a5001c750752ae3deb88393c5c8e002943a1ae4283e0bbe8997c08ef9c75c1aa0ba3f3c95108a97e4ed4fcb8c09ec3a
7
+ data.tar.gz: 3065622d9b7829c1281249ac7d8e6e49065b5535b029bc09d3d3351b0b41d93ed1f715c6ca4f0055844cadb1b5490bb5b70a52a7ef56edc2878dbd3d69d3b16e
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ /test/fixtures/secrets.yml
16
+ **/.DS_Store
17
+ *.gem
18
+ *.rbc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in any_api.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 anthony
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # AnyApi is the worlds smallest API client :-)
2
+
3
+ Decided to share this thanks to some research I did on APIs. Most of them have a base URL, then API endpoint, API username, a API password and transmits data in form of JSON. Then there are requests and responses. This could be a simple solution to access and work with a big or small APIs.
4
+
5
+ Please give Ruby's Net::HTTP Library a go. That is the backbone behind this. Net::HTTP simple, does the job, it is fast and build on top Ruby's IO so straight to the point.
6
+
7
+ ## Installation
8
+
9
+
10
+ ```ruby
11
+ gem 'any_api'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install any_api
21
+
22
+ ## Usage
23
+
24
+
25
+ If you are going to use this with Rails. Add bellow code your application.rb file, or the application_controller.rb file or as an initialiser file. It would work fine with any other Ruby project.
26
+
27
+ ```ruby
28
+ AnyApi.configure do |config|
29
+ config.username = "me@example.com"
30
+ 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
+ end
34
+ ```
35
+
36
+ Then you can call any API with
37
+
38
+ ```ruby
39
+ response = AnyApi::Request.new("Get", '/products.json' )
40
+ ```
41
+
42
+ The first parameter is the HTTP method. Secondly it is the url endpoint.
43
+
44
+ If you want to do Post or Update calls please send parameters with your request
45
+
46
+
47
+ ```ruby
48
+ my_params = {"year"=>"2014", "country"=>"Australia", "first_name"=>"True", "last_name"=>"Colours"}
49
+ response = AnyApi::Request.new( "Post", '/users/new', my_params)
50
+ ```
51
+
52
+ To parse the response
53
+
54
+ ```ruby
55
+ response.parser_response
56
+ ```
57
+
58
+ Thanks
59
+
60
+
61
+ ## Contributing
62
+
63
+ 1. Fork it ( https://github.com/iamfree-com/any_api/fork )
64
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
65
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
66
+ 4. Push to the branch (`git push origin my-new-feature`)
67
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "test"
7
+ t.pattern = "test/**/*_test.rb"
8
+ end
9
+
10
+ task :default => :test
data/any_api.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'any_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "any_api"
8
+ spec.version = AnyApi::VERSION
9
+ spec.authors = ["anthony"]
10
+ spec.email = ["anthony@iamfree.com"]
11
+ spec.summary = %q{Access any API you like}
12
+ spec.description = %q{This is probably the world smallest API client. Give the infomation any API asking for you to access it. Then fill the blanks and you are good to go}
13
+ spec.homepage = "https://github.com/iamfree-com/any_api"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
@@ -0,0 +1,25 @@
1
+ module AnyApi
2
+ class Configuration
3
+ attr_accessor :username, :password, :api_base_url, :log_level
4
+
5
+ def initialize
6
+ self.username = nil
7
+ self.password = nil
8
+ self.api_base_url = nil
9
+ self.log_level = 'info'
10
+ end
11
+ end
12
+
13
+ class << self
14
+ attr_accessor :configuration
15
+ end
16
+
17
+ def self.configuration
18
+ @configuration ||= Configuration.new
19
+ end
20
+
21
+ def self.configure
22
+ yield(configuration) if block_given?
23
+ end
24
+
25
+ end
@@ -0,0 +1,3 @@
1
+ module AnyApi
2
+ VERSION = "0.0.2"
3
+ end
data/lib/any_api.rb ADDED
@@ -0,0 +1,49 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'json'
4
+
5
+
6
+ require_relative "any_api/configuration"
7
+
8
+ module AnyApi
9
+
10
+ class Request
11
+
12
+ attr_accessor :apiresult
13
+ HTTPMETHODS = ["Get", "Head", "Post", "Patch", "Put", "Proppatch", "Lock", "Unlock", "Options", "Propfind", "Delete", "Move", "Copy", "Mkcol", "Trace"]
14
+
15
+ def initialize(http_method, endpoint, params_hsh = nil)
16
+
17
+ uri = URI.parse("#{AnyApi.configuration.api_base_url}/#{endpoint}")
18
+
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
21
+ request = eval "Net::HTTP::#{http_method if HTTPMETHODS.include?(http_method)}.new uri"
22
+ request.basic_auth(AnyApi.configuration.username, AnyApi.configuration.password)
23
+ request["Content-Type"] = "application/json"
24
+ if params_hsh
25
+ request.body = params_hsh.to_json
26
+ end
27
+ http.request request
28
+ end
29
+
30
+ @apiresult = res
31
+ end
32
+
33
+
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
+ end
47
+
48
+
49
+ end
@@ -0,0 +1,15 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
+
3
+ class AnyApiTest < Minitest::Test
4
+
5
+
6
+ def test_connection
7
+ response = AnyApi::Request.new( "Get", '/tickets.json')
8
+ #p response
9
+ #p response.apiresult.body
10
+ p response.parser_response
11
+ assert_equal "200", response.apiresult.code
12
+ end
13
+
14
+
15
+ end
@@ -0,0 +1,4 @@
1
+ conf:
2
+ username: myemail@example.com
3
+ password: BINQa85IY81rfPa6SiA35IY81re1@IY8
4
+ api_base_url: https://iamfree.com/api/v1
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ require "minitest/autorun"
3
+ require 'net/http'
4
+ require 'uri'
5
+ require 'json'
6
+ require 'yaml'
7
+
8
+
9
+ libpath = File.dirname(__FILE__)
10
+ $LOAD_PATH.unshift File.join(libpath, "..", "lib")
11
+
12
+ require_relative "../lib/any_api"
13
+
14
+ secretfile = YAML.load_file("#{Dir.pwd}/test/fixtures/secrets.yml")
15
+
16
+ AnyApi.configure do |config|
17
+ config.username = secretfile["conf"]["username"]
18
+ config.password = secretfile["conf"]["password"]
19
+ config.api_base_url = secretfile["conf"]["api_base_url"]
20
+
21
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: any_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - anthony
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: This is probably the world smallest API client. Give the infomation any
42
+ API asking for you to access it. Then fill the blanks and you are good to go
43
+ email:
44
+ - anthony@iamfree.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - any_api.gemspec
55
+ - lib/any_api.rb
56
+ - lib/any_api/configuration.rb
57
+ - lib/any_api/version.rb
58
+ - test/any_api_test.rb
59
+ - test/fixtures/secrets_example.yml
60
+ - test/test_helper.rb
61
+ homepage: https://github.com/iamfree-com/any_api
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.2.2
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Access any API you like
85
+ test_files:
86
+ - test/any_api_test.rb
87
+ - test/fixtures/secrets_example.yml
88
+ - test/test_helper.rb