randomapi 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 58f1b036922c600189cb89ddd6bae4c1f72ab6c3
4
+ data.tar.gz: e22a8502351a98b2426c6ee29baf2abb0720efbc
5
+ SHA512:
6
+ metadata.gz: e6ec12f62661a2611b3c1b1a9cf76fa8f0e2e9ce814ba70bfbed4242ead0440375f4c95254de63e0b0337d6c5564ff1ec45376fa39134d3226c8a56c8428917f
7
+ data.tar.gz: e00a2664bfd7ba7528ad75fe5d1d8b9e92d19e459654d928cafb6f954a22bb3c8fa26aa2e23505ad15935f3a738db190f4ff9cab5fdd9ab8ab943e13f6641be8
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in randomapi.gemspec
4
+ gemspec
@@ -0,0 +1,25 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ randomapi (0.0.2)
5
+ faraday (>= 0.7, < 0.10)
6
+ faraday_middleware (>= 0.8, < 0.10)
7
+ hashie (>= 0.4.0)
8
+ multi_json (~> 1.0, >= 1.0.3)
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ faraday (0.9.1)
14
+ multipart-post (>= 1.2, < 3)
15
+ faraday_middleware (0.9.1)
16
+ faraday (>= 0.7.4, < 0.10)
17
+ hashie (3.4.0)
18
+ multi_json (1.10.1)
19
+ multipart-post (2.0.0)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ randomapi!
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Denis Kuznetsov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,42 @@
1
+ # randomapi-ruby
2
+ API to access data from https://randomapi.com
3
+
4
+ #Installation
5
+ Bundler:
6
+ ------------
7
+ gem install randomapi
8
+
9
+ #Initialization
10
+ If you use only 1 API key & API id, it makes sense to pass them in the initializer:
11
+ ```ruby
12
+ RandomApi.configure do |config|
13
+ config.api_key = "YOUR_API_KEY_HERE"
14
+ config.api_id = "YOUR_API_ID_HERE"
15
+ end
16
+ ```
17
+
18
+ #Client creation
19
+ To make requests you need to create a client:
20
+ ```ruby
21
+ client = RandomApi.client # uses api key & id from initializer
22
+ client = RandomApi.client(api_key: "ANOTHER_API_KEY", api_id: "ANOTHER_API_ID") # passes key & id directly
23
+ ```
24
+
25
+ #Making calls
26
+ ```ruby
27
+ client.request # makes request to API to return 1 result
28
+ client.request(results: 5) # give me 5 results!
29
+ client.request(results: 5, myvar: 1) # give me 5 results with variable "myvar" set to 1
30
+ ```
31
+ ## Contributing to randomapi
32
+ * Fork the project
33
+ * Start a feature/bugfix branch
34
+ * Commit and push until you are happy with your contribution
35
+ * Make pull request
36
+
37
+ License
38
+ -------
39
+ This code is free to use under the terms of the MIT license.
40
+
41
+ ## Copyright
42
+ Copyright (c) 2015 Denis Kuznetsov.
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,13 @@
1
+ require "randomapi/version"
2
+
3
+ require File.expand_path('../randomapi/configuration', __FILE__)
4
+ require File.expand_path('../randomapi/api', __FILE__)
5
+ require File.expand_path('../randomapi/client', __FILE__)
6
+
7
+ module RandomApi
8
+ extend Configuration
9
+
10
+ def self.client(options={})
11
+ RandomApi::Client.new(options)
12
+ end
13
+ end
@@ -0,0 +1,29 @@
1
+ require File.expand_path('../connection', __FILE__)
2
+ require File.expand_path('../request', __FILE__)
3
+
4
+ module RandomApi
5
+ # @private
6
+ class API
7
+ # @private
8
+ attr_accessor *Configuration::VALID_OPTIONS_KEYS
9
+
10
+ # Creates a new API
11
+ def initialize(options={})
12
+ options = RandomApi.options.merge(options)
13
+ Configuration::VALID_OPTIONS_KEYS.each do |key|
14
+ send("#{key}=", options[key])
15
+ end
16
+ end
17
+
18
+ def config
19
+ conf = {}
20
+ Configuration::VALID_OPTIONS_KEYS.each do |key|
21
+ conf[key] = send key
22
+ end
23
+ conf
24
+ end
25
+
26
+ include Connection
27
+ include Request
28
+ end
29
+ end
@@ -0,0 +1,9 @@
1
+ module RandomApi
2
+ class Client < API
3
+
4
+ def request(options = {})
5
+ get("", options)
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,84 @@
1
+ require 'faraday'
2
+ require File.expand_path('../version', __FILE__)
3
+
4
+ module RandomApi
5
+ # Defines constants and methods related to configuration
6
+ module Configuration
7
+ # An array of valid keys in the options hash when configuring API
8
+ VALID_OPTIONS_KEYS = [
9
+ :api_key,
10
+ :api_id,
11
+ :adapter,
12
+ :connection_options,
13
+ :endpoint,
14
+ :format,
15
+ :proxy,
16
+ :user_agent
17
+ ].freeze
18
+
19
+ # By default, don't set api key
20
+ DEFAULT_API_KEY = nil
21
+
22
+ # By default, don't set api id
23
+ DEFAULT_API_ID = nil
24
+
25
+ # The adapter that will be used to connect if none is set
26
+ #
27
+ # @note The default faraday adapter is Net::HTTP.
28
+ DEFAULT_ADAPTER = Faraday.default_adapter
29
+
30
+ # By default, don't set any connection options
31
+ DEFAULT_CONNECTION_OPTIONS = {}
32
+
33
+ # The endpoint that will be used to connect if none is set
34
+ #
35
+ # @note There is no reason to use any other endpoint at this time
36
+ DEFAULT_ENDPOINT = 'https://randomapi.com/api/'.freeze
37
+
38
+ # The response format appended to the path and sent in the 'Accept' header if none is set
39
+ #
40
+ # @note JSON is the only available format at this time
41
+ DEFAULT_FORMAT = :json
42
+
43
+ # By default, don't use a proxy server
44
+ DEFAULT_PROXY = nil
45
+
46
+ DEFAULT_USER_AGENT = ""
47
+
48
+ # An array of valid request/response formats
49
+ VALID_FORMATS = [
50
+ :json].freeze
51
+
52
+ # @private
53
+ attr_accessor *VALID_OPTIONS_KEYS
54
+
55
+ # When this module is extended, set all configuration options to their default values
56
+ def self.extended(base)
57
+ base.reset
58
+ end
59
+
60
+ # Convenience method to allow configuration options to be set in a block
61
+ def configure
62
+ yield self
63
+ end
64
+
65
+ # Create a hash of options and their values
66
+ def options
67
+ VALID_OPTIONS_KEYS.inject({}) do |option, key|
68
+ option.merge!(key => send(key))
69
+ end
70
+ end
71
+
72
+ # Reset all configuration options to defaults
73
+ def reset
74
+ self.api_key = DEFAULT_API_KEY
75
+ self.api_id = DEFAULT_API_ID
76
+ self.adapter = DEFAULT_ADAPTER
77
+ self.connection_options = DEFAULT_CONNECTION_OPTIONS
78
+ self.endpoint = DEFAULT_ENDPOINT
79
+ self.format = DEFAULT_FORMAT
80
+ self.proxy = DEFAULT_PROXY
81
+ self.user_agent = DEFAULT_USER_AGENT
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,30 @@
1
+ require 'faraday_middleware'
2
+ Dir[File.expand_path('../../faraday/*.rb', __FILE__)].each{|f| require f}
3
+
4
+ module RandomApi
5
+ module Connection
6
+ private
7
+
8
+ def connection(raw=false)
9
+ options = {
10
+ :headers => {
11
+ 'Accept' => "application/#{format}",
12
+ 'User-Agent' => user_agent
13
+ },
14
+ :proxy => proxy,
15
+ :url => endpoint,
16
+ }.merge(connection_options)
17
+
18
+ Faraday::Connection.new(options) do |connection|
19
+ connection.use Faraday::Request::UrlEncoded
20
+ connection.use FaradayMiddleware::Mashify unless raw
21
+ unless raw
22
+ case format.to_s.downcase
23
+ when 'json' then connection.use Faraday::Response::ParseJson
24
+ end
25
+ end
26
+ connection.adapter(adapter)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,46 @@
1
+ require 'openssl'
2
+
3
+ module RandomApi
4
+ # Defines HTTP request methods
5
+ module Request
6
+ # Perform an HTTP GET request
7
+ def get(path, options={}, raw=false)
8
+ make_request(:get, path, options, raw)
9
+ end
10
+
11
+ # Perform an HTTP POST request
12
+ def post(path, options={}, raw=false)
13
+ make_request(:post, path, options, raw)
14
+ end
15
+
16
+ # Perform an HTTP PUT request
17
+ def put(path, options={}, raw=false)
18
+ make_request(:put, path, options, raw)
19
+ end
20
+
21
+ # Perform an HTTP DELETE request
22
+ def delete(path, options={}, raw=false)
23
+ make_request(:delete, path, options, raw)
24
+ end
25
+
26
+ private
27
+
28
+ # Perform an HTTP request
29
+ def make_request(method, path, options, raw=false)
30
+ options.merge!({:key => api_key, :id => api_id })
31
+
32
+ response = connection(raw).send(method) do |request|
33
+ case method
34
+ when :get, :delete
35
+ request.url(path, options)
36
+ when :post, :put
37
+ request.path = path
38
+ request.body = options unless options.empty?
39
+ end
40
+ end
41
+ return response if raw
42
+ return response.body
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ module RandomApi
2
+ VERSION = "0.0.1"
3
+ end
@@ -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
+
5
+ require 'randomapi/version'
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "randomapi"
8
+ spec.version = RandomApi::VERSION
9
+ spec.authors = ["Denis Kuznetsov"]
10
+ spec.email = ["denis@f7.ru"]
11
+ spec.summary = %q{Gets data from randomapi.com}
12
+ spec.description = %q{Gets data from randomapi.com}
13
+ spec.homepage = "https://github.com/byteg/randomapi-ruby"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_runtime_dependency('faraday', ['>= 0.7', '< 0.10'])
20
+ spec.add_runtime_dependency('faraday_middleware', ['>= 0.8', '< 0.10'])
21
+ spec.add_runtime_dependency('multi_json', '>= 1.0.3', '~> 1.0')
22
+ spec.add_runtime_dependency('hashie', '>= 0.4.0')
23
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: randomapi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Denis Kuznetsov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0.7'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '0.10'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '0.7'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '0.10'
33
+ - !ruby/object:Gem::Dependency
34
+ name: faraday_middleware
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0.8'
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '0.10'
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0.8'
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '0.10'
53
+ - !ruby/object:Gem::Dependency
54
+ name: multi_json
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 1.0.3
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '1.0'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: 1.0.3
70
+ - - "~>"
71
+ - !ruby/object:Gem::Version
72
+ version: '1.0'
73
+ - !ruby/object:Gem::Dependency
74
+ name: hashie
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: 0.4.0
80
+ type: :runtime
81
+ prerelease: false
82
+ version_requirements: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: 0.4.0
87
+ description: Gets data from randomapi.com
88
+ email:
89
+ - denis@f7.ru
90
+ executables: []
91
+ extensions: []
92
+ extra_rdoc_files: []
93
+ files:
94
+ - Gemfile
95
+ - Gemfile.lock
96
+ - License.txt
97
+ - README.md
98
+ - Rakefile
99
+ - lib/randomapi.rb
100
+ - lib/randomapi/api.rb
101
+ - lib/randomapi/client.rb
102
+ - lib/randomapi/configuration.rb
103
+ - lib/randomapi/connection.rb
104
+ - lib/randomapi/request.rb
105
+ - lib/randomapi/version.rb
106
+ - randomapi.gemspec
107
+ homepage: https://github.com/byteg/randomapi-ruby
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.4.5
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Gets data from randomapi.com
131
+ test_files: []
132
+ has_rdoc: