random_api 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4a074c715909f4eebca52ee9739ff7da2ae1437a
4
+ data.tar.gz: 34a296fff2efeb2bba8c1593e102ffb0fe5a32c5
5
+ SHA512:
6
+ metadata.gz: 28e32619217dbf35207807defc4e3b1e14023fbb8eb6702c81dd765246b22d2dcbf8efc4980d664c495394b846ada3dc69d8e16e32c96379a7725c247372b508
7
+ data.tar.gz: 757cb3917e96b0d7fe84e4bd8b9f7d9b060f94c9ec538aa99bc511ab9e207f97fb517914536916cc39e0ad1d7827cc99241dad04bd3c245504b0118515872e5d
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+ *.bundle
12
+ *.so
13
+ *.o
14
+ *.a
15
+ mkmf.log
16
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in random_api.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Brandon Buck
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 all
13
+ 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 THE
21
+ SOFTWARE.
22
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Brandon
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,86 @@
1
+ # RandomAPI
2
+
3
+ Interface with the RandomAPI at https://randomuser.me and provide a ruby wrapper for the responses. Interact with response as a Ruby object with helper methods for fetching all important data.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'random_api'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install random_api
20
+
21
+ ## Usage
22
+
23
+ Using RandomAPI is simple, you can use it anonymously or use your API Key to make requests.
24
+
25
+ ```ruby
26
+ # Create a RandomAPI service with anonymous access to the API
27
+ service = RandomApi::Service.new
28
+
29
+ # Fetch a single random user
30
+ user = service.user
31
+
32
+ # Fetch a male user
33
+ user = service.user(gender: "male")
34
+
35
+ # Fetch a list of users
36
+ user = service.users(10)
37
+
38
+ # Fetch 10 male users
39
+ user = service.users(10, gender: "male")
40
+
41
+ # If you have an API Key, pass it to service
42
+ service = RandomApi::Service.new(api_key)
43
+
44
+ # Then make requests as per the norm
45
+ ```
46
+
47
+ The object returned by the call to `RandomApi::Service#user` and `RandomApi::Service#users` is of the type `RandomApi::User`.
48
+
49
+ ```ruby
50
+ user = service.user
51
+
52
+ user.gender # => "female"
53
+ user.name # => "Ms. Lois Williams"
54
+ user.title # => "Ms."
55
+ user.first_name # => "Lois"
56
+ user.last_name # => "Williams"
57
+ user.street # => "1969 Elgin St"
58
+ user.city # => "Frederick"
59
+ user.state # => "Delaware"
60
+ user.zip # => "56298"
61
+ user.email # => "lois.williams50@example.com"
62
+ user.username # => "heavybutterfly920"
63
+ user.password # => "enterprise"
64
+ user.salt # => ">egEn6YsO"
65
+ user.md5 # => "2dd1894ea9d19bf5479992da95713a3a",
66
+ user.sha1 # => "ba230bc400723f470b68e9609ab7d0e6cf123b59",
67
+ user.sha256 # => "f4f52bf8c5ad7fc759d1d4156b25a4c7b3d1e2eec6c92d80e508aa0b7946d4ba",
68
+ user.registered # => "1288182167",
69
+ user.dob # => "146582153",
70
+ user.phone # => "(555)-942-1322",
71
+ user.cell # => "(178)-341-1520",
72
+ user.SSN # => "137-37-8866",
73
+ user.large_picture # => "http://api.randomuser.me/portraits/women/55.jpg",
74
+ user.medium_picture # => "http://api.randomuser.me/portraits/med/women/55.jpg",
75
+ user.thumbnail # => "http://api.randomuser.me/portraits/thumb/women/55.jpg",
76
+ user.api_version # => "0.4.1"
77
+ user.seed # => "graywolf"
78
+ ```
79
+
80
+ ## Contributing
81
+
82
+ 1. Fork it ( https://github.com/[my-github-username]/random_api/fork )
83
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
84
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
85
+ 4. Push to the branch (`git push origin my-new-feature`)
86
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ begin
4
+ require "rspec/core/rake_task"
5
+ RSpec::Core::RakeTask.new(:spec) do |t|
6
+ t.rspec_opts = "--color --format documentation"
7
+ end
8
+ rescue LoadError
9
+ end
10
+
@@ -0,0 +1,2 @@
1
+ class RandomApi::InvalidUserData < RuntimeError
2
+ end
@@ -0,0 +1,68 @@
1
+ require "httparty"
2
+
3
+ require "random_api/user"
4
+
5
+ class RandomApi::Service
6
+ include HTTParty
7
+
8
+ base_uri "api.randomuser.me/0.4.1"
9
+
10
+ def initialize(api_key = nil)
11
+ @api_key = api_key
12
+ end
13
+
14
+ def user(options = {})
15
+ options = options.dup
16
+ options.delete(:results) # Single user request, no need
17
+ options = {
18
+ query: default_query_options.merge(query_options_from(options))
19
+ }
20
+ RandomApi::User.new(self.class.get("/", options)["results"].first)
21
+ end
22
+
23
+ def users(count, options = {})
24
+ options = options.dup
25
+ options[:results] = count
26
+ options = {
27
+ query: default_query_options.merge(query_options_from(options))
28
+ }
29
+ results = self.class.get("/", options)["results"]
30
+ results.map { |data| RandomApi::User.new(data) }
31
+ end
32
+
33
+ def to_s
34
+ "RandomAPI Service " + (@api_key.nil? ? "no api key given" : @api_key)
35
+ end
36
+
37
+ def inspect
38
+ "#<RandomApi::Service " + (@api_key.nil? ? "anonymous" : "setup with key") + ">"
39
+ end
40
+
41
+ private
42
+
43
+ def query_options_from(options)
44
+ query = {}
45
+ valid_options.each do |key|
46
+ query[key] = options[key] if options.has_key?(key)
47
+ end
48
+ query
49
+ end
50
+
51
+ def valid_options
52
+ @_valid_options ||= [:results, :gender, :seed]
53
+ end
54
+
55
+ def default_query_options
56
+ query = {
57
+ results: 1
58
+ }
59
+ query[:key] = @api_key unless @api_key.nil?
60
+ query
61
+ end
62
+
63
+ def request_options
64
+ opts = {}
65
+ opts[:query] = {}
66
+ opts[:query][:key] = @api_key unless @api_key.nil?
67
+ end
68
+ end
@@ -0,0 +1,63 @@
1
+ require "random_api/invalid_user_data"
2
+
3
+ class RandomApi::User
4
+ attr_accessor :gender, :title, :first_name, :last_name, :name, :street, :city,
5
+ :state, :zip, :email, :username, :password, :salt, :md5, :sha1,
6
+ :sha256, :registered, :dob, :phone, :cell, :ssn, :large_picture,
7
+ :medium_picture, :thumbnail, :api_version, :seed, :api_response
8
+
9
+ def initialize(user_obj)
10
+ begin
11
+ load_from_api_response(user_obj)
12
+ rescue Exception
13
+ raise RandomApi::InvalidUserData, "The user data given is not of the correct format to be parsed."
14
+ end
15
+ end
16
+
17
+ def to_s
18
+ name
19
+ end
20
+
21
+ def inspect
22
+ "#<RandomApi::User version=#{api_version} \"#{name}\">"
23
+ end
24
+
25
+ def to_s
26
+ name
27
+ end
28
+
29
+ def inspect
30
+ "#<RandomApi::User version=#{api_version} \"#{name}\">"
31
+ end
32
+
33
+ private
34
+
35
+ def load_from_api_response(user_obj)
36
+ self.api_response = user_obj
37
+ self.seed = user_obj["seed"]
38
+ user_data = user_obj["user"]
39
+ user_data.each do |key, value|
40
+ next if ["name", "dob", "registered"].include?(key)
41
+ key = key.to_s.downcase
42
+ if respond_to?("#{key}=", true)
43
+ send("#{key}=", value)
44
+ end
45
+ end
46
+ self.title = user_data["name"]["title"]
47
+ self.title += "." unless title == "miss"
48
+ self.title.capitalize!
49
+ self.first_name = user_data["name"]["first"].capitalize
50
+ self.last_name = user_data["name"]["last"].capitalize
51
+ self.name = "#{title} #{first_name} #{last_name}"
52
+ self.dob = DateTime.strptime(user_data["dob"], "%s").to_time
53
+ self.registered = DateTime.strptime(user_data["registered"], "%s").to_time
54
+ self.street = user_data["location"]["street"].split(" ").map(&:capitalize).join(" ")
55
+ self.city = user_data["location"]["city"].capitalize
56
+ self.state = user_data["location"]["state"].capitalize
57
+ self.zip = user_data["location"]["zip"]
58
+ self.large_picture = user_data["picture"]["large"]
59
+ self.medium_picture = user_data["picture"]["medium"]
60
+ self.thumbnail = user_data["picture"]["thumbnail"]
61
+ self.api_version = user_data["version"]
62
+ end
63
+ end
@@ -0,0 +1,3 @@
1
+ module RandomApi
2
+ VERSION = "0.0.1"
3
+ end
data/lib/random_api.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "random_api/version"
2
+ require "random_api/service"
3
+
4
+ module RandomApi
5
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'random_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "random_api"
8
+ spec.version = RandomApi::VERSION
9
+ spec.authors = ["Brandon"]
10
+ spec.email = ["lordizuriel@gmail.com"]
11
+ spec.summary = "Interface with the Random API https://randomuser.me"
12
+ spec.description = "Provide a ruby interface for pulling data from the Random API, fetching random user data."
13
+ spec.homepage = "https://github.com/bbuck/random_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_runtime_dependency "httparty", "~> 0.13.3"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.1"
26
+ end
@@ -0,0 +1,33 @@
1
+ require "spec_helper"
2
+
3
+ describe RandomApi::Service do
4
+ it { should respond_to(:user) }
5
+
6
+ describe "#user" do
7
+ let(:service) { RandomApi::Service.new }
8
+
9
+ it "should return a user" do
10
+ expect(service.user).to be_a(RandomApi::User)
11
+ end
12
+
13
+ it "should accept options" do
14
+ expect(service.user(gender: "female").gender).to eq("female")
15
+ expect(service.user(gender: "male").gender).to eq("male")
16
+ end
17
+ end
18
+
19
+ describe "#users" do
20
+ let(:service) { RandomApi::Service.new }
21
+
22
+ it "should return more than one user" do
23
+ expect(service.users(10).length).to eq(10)
24
+ end
25
+
26
+ it "should accept options" do
27
+ male = "male" * 10
28
+ female = "female" * 10
29
+ expect(service.users(10, gender: "male").map(&:gender).join).to eq(male)
30
+ expect(service.users(10, gender: "female").map(&:gender).join).to eq(female)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,47 @@
1
+ require "spec_helper"
2
+
3
+ describe RandomApi::User do
4
+ subject { RandomApi::User.new(stub_user_return) }
5
+ it { should respond_to(:gender, :title, :first_name, :last_name, :name,
6
+ :street, :city, :state, :zip, :email, :username,
7
+ :password, :salt, :md5, :sha1, :sha256, :registered,
8
+ :dob, :phone, :cell, :ssn, :large_picture,
9
+ :medium_picture, :thumbnail, :api_version, :seed,
10
+ :api_response) }
11
+
12
+ describe "#initialize" do
13
+ describe "should load from api response" do
14
+ let(:user) { RandomApi::User.new(stub_user_return) }
15
+
16
+ it "should contain user data" do
17
+ expect(user.gender).to eq("female")
18
+ expect(user.name).to eq("Ms. Lois Williams")
19
+ expect(user.title).to eq("Ms.")
20
+ expect(user.first_name).to eq("Lois")
21
+ expect(user.last_name).to eq("Williams")
22
+ expect(user.street).to eq("1969 Elgin St")
23
+ expect(user.city).to eq("Frederick")
24
+ expect(user.state).to eq("Delaware")
25
+ expect(user.zip).to eq("56298")
26
+ expect(user.email).to eq("lois.williams50@example.com")
27
+ expect(user.username).to eq("heavybutterfly920")
28
+ expect(user.password).to eq("enterprise")
29
+ expect(user.salt).to eq(">egEn6YsO")
30
+ expect(user.md5).to eq("2dd1894ea9d19bf5479992da95713a3a")
31
+ expect(user.sha1).to eq("ba230bc400723f470b68e9609ab7d0e6cf123b59")
32
+ expect(user.sha256).to eq("f4f52bf8c5ad7fc759d1d4156b25a4c7b3d1e2eec6c92d80e508aa0b7946d4ba")
33
+ expect(user.registered.strftime("%s")).to eq("1288182167")
34
+ expect(user.dob.strftime("%s")).to eq("146582153")
35
+ expect(user.phone).to eq("(555)-942-1322")
36
+ expect(user.cell).to eq("(178)-341-1520")
37
+ expect(user.ssn).to eq("137-37-8866")
38
+ expect(user.large_picture).to eq("http://api.randomuser.me/portraits/women/55.jpg")
39
+ expect(user.medium_picture).to eq("http://api.randomuser.me/portraits/med/women/55.jpg")
40
+ expect(user.thumbnail).to eq("http://api.randomuser.me/portraits/thumb/women/55.jpg")
41
+ expect(user.api_version).to eq("0.4.1")
42
+ expect(user.seed).to eq("graywolf")
43
+ expect(user.api_response).to eq(stub_user_return)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,39 @@
1
+ require "random_api"
2
+
3
+ def stub_user_return
4
+ {
5
+ "user" => {
6
+ "gender" => "female",
7
+ "name" => {
8
+ "title" => "ms",
9
+ "first" => "lois",
10
+ "last" => "williams"
11
+ },
12
+ "location" => {
13
+ "street" => "1969 elgin st",
14
+ "city" => "frederick",
15
+ "state" => "delaware",
16
+ "zip" => "56298"
17
+ },
18
+ "email" => "lois.williams50@example.com",
19
+ "username" => "heavybutterfly920",
20
+ "password" => "enterprise",
21
+ "salt" => ">egEn6YsO",
22
+ "md5" => "2dd1894ea9d19bf5479992da95713a3a",
23
+ "sha1" => "ba230bc400723f470b68e9609ab7d0e6cf123b59",
24
+ "sha256" => "f4f52bf8c5ad7fc759d1d4156b25a4c7b3d1e2eec6c92d80e508aa0b7946d4ba",
25
+ "registered" => "1288182167",
26
+ "dob" => "146582153",
27
+ "phone" => "(555)-942-1322",
28
+ "cell" => "(178)-341-1520",
29
+ "SSN" => "137-37-8866",
30
+ "picture" => {
31
+ "large" => "http://api.randomuser.me/portraits/women/55.jpg",
32
+ "medium" => "http://api.randomuser.me/portraits/med/women/55.jpg",
33
+ "thumbnail" => "http://api.randomuser.me/portraits/thumb/women/55.jpg",
34
+ },
35
+ "version" => "0.4.1"
36
+ },
37
+ "seed" => "graywolf"
38
+ }
39
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: random_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Brandon
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.13.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.13.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.1'
69
+ description: Provide a ruby interface for pulling data from the Random API, fetching
70
+ random user data.
71
+ email:
72
+ - lordizuriel@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - Gemfile
79
+ - LICENSE
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/random_api.rb
84
+ - lib/random_api/invalid_user_data.rb
85
+ - lib/random_api/service.rb
86
+ - lib/random_api/user.rb
87
+ - lib/random_api/version.rb
88
+ - random_api.gemspec
89
+ - spec/random_api/service_spec.rb
90
+ - spec/random_api/user_spec.rb
91
+ - spec/spec_helper.rb
92
+ homepage: https://github.com/bbuck/random_api
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.4.3
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Interface with the Random API https://randomuser.me
116
+ test_files:
117
+ - spec/random_api/service_spec.rb
118
+ - spec/random_api/user_spec.rb
119
+ - spec/spec_helper.rb