nibo 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: aeada7c5a966c601db2cf33c825799f071cae40c
4
+ data.tar.gz: d0d8a396bf4c8d1012060d8b84fc648e15633c02
5
+ SHA512:
6
+ metadata.gz: bd9b07fbf58e2249903f202020668456e5fcafa9ab29cfe19f01afd3cc1c5ba3c819c3a416f93fd5c07a2cb712ea14a9c71ca6b59d336627908f52688fb2e0b5
7
+ data.tar.gz: 1d2f7499473303ae8740202a31287158672a6037731c247163c05ad8d404e1bbeec839da67e88297afebbc7dd6e9b86fc7c753cd3a8378eaa2506845020dc34f
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .idea/
11
+ .ruby*
12
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in nibo.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Arthur Granado
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,41 @@
1
+ # Nibo
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/nibo`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'nibo'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install nibo
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/nibo.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "nibo"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,51 @@
1
+ require "json"
2
+ require "base64"
3
+ require "rest-client"
4
+ require "active_support/all"
5
+
6
+ require "nibo/version"
7
+
8
+ require "nibo/api_resource"
9
+ require "nibo/api_resource/create"
10
+ require "nibo/api_resource/retrieve"
11
+ require "nibo/api_resource/list"
12
+ require "nibo/api_resource/delete"
13
+
14
+ require "nibo/object"
15
+ require "nibo/account"
16
+ require "nibo/category"
17
+ require "nibo/entity"
18
+ require "nibo/schedule"
19
+ require "nibo/entry"
20
+
21
+ module Nibo
22
+ @@api_key
23
+ @@api_secret
24
+ @@user
25
+
26
+ def self.api_key=(api_key)
27
+ @@api_key = api_key
28
+ end
29
+
30
+ def self.api_key
31
+ @@api_key
32
+ end
33
+
34
+ def self.api_secret=(api_secret)
35
+ @@api_secret = api_secret
36
+ end
37
+
38
+ def self.user=(user)
39
+ @@user = user
40
+ end
41
+
42
+ def self.user
43
+ @@user
44
+ end
45
+
46
+ def self.generate_hash(time_stamp)
47
+ Base64.encode64(OpenSSL::HMAC.digest('sha1',
48
+ Base64.decode64(Base64.encode64(@@api_secret).gsub("\n", '')),
49
+ "#{@@api_key}|#{time_stamp}|#{@@user}|#{@@api_key}")).chomp
50
+ end
51
+ end
@@ -0,0 +1,30 @@
1
+ module Nibo
2
+ class Account < Nibo::Object
3
+ include ApiResource
4
+ include ApiResource::Create
5
+ include ApiResource::Retrieve
6
+ include ApiResource::List
7
+ include ApiResource::Delete
8
+
9
+ def self.url_method(method)
10
+ case method
11
+ when :get
12
+ '/GetAccount'
13
+ when :post
14
+ '/CreateAccount'
15
+ when :delete
16
+ '/DeleteAccount'
17
+ end
18
+
19
+ end
20
+
21
+ def self.object_param(param, method)
22
+ case method
23
+ when :get, :delete
24
+ {accountId: param}
25
+ when :post
26
+ param
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,46 @@
1
+ module Nibo
2
+ module ApiResource
3
+ BASE_URL = 'http://api.nibo.com.br/public/v1'
4
+
5
+ def url_encode(key)
6
+ URI.escape(key.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
7
+ end
8
+
9
+ def encode(params)
10
+ params.map { |k,v| "#{k}=#{url_encode(v)}" }.join('&')
11
+ end
12
+
13
+ def api_request(url, method, params = nil)
14
+ time_stamp = Time.now.utc.strftime('%Y%m%d%H%M')
15
+ url = "#{BASE_URL}#{url}"
16
+ headers = {authorization: "Basic #{Base64.strict_encode64("#{Nibo.api_key}:#{Nibo.generate_hash(time_stamp)}")}",
17
+ Timestamp: time_stamp,
18
+ User: Nibo.user,
19
+ content_type: 'application/json',
20
+ accept: 'application/json'}
21
+
22
+ if (method == :get or method == :delete) && params
23
+ params_encoded = encode(params)
24
+ url = "#{url}?#{params_encoded}"
25
+ params = nil
26
+ end
27
+
28
+ response = case method
29
+ when :get
30
+ RestClient.get(url, headers)
31
+ when :post
32
+ RestClient.post(url, params.to_json, headers)
33
+ when :delete
34
+ RestClient.delete(url, headers)
35
+ {}.to_json
36
+ end
37
+
38
+ result = JSON.parse(response)
39
+ (result.is_a? Hash) ? result.deep_symbolize_keys : result.map {|object| object.deep_symbolize_keys}
40
+ end
41
+
42
+ def self.included(base)
43
+ base.extend(ApiResource)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,18 @@
1
+ module Nibo
2
+ module ApiResource
3
+ module Create
4
+ def create(params)
5
+ method = :post
6
+ url_with_method = "#{url}#{url_method(method)}"
7
+ params = object_param(params, method)
8
+
9
+ response = api_request(url_with_method,method,params)
10
+ create_from(response)
11
+ end
12
+
13
+ def self.included(base)
14
+ base.extend(Create)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ module Nibo
2
+ module ApiResource
3
+ module Delete
4
+ def delete(obj_id)
5
+ method = :delete
6
+ url_with_method = "#{url}#{url_method(method)}"
7
+ params = object_param(obj_id, method)
8
+
9
+ 'deleted' if api_request(url_with_method,method,params).empty?
10
+ end
11
+
12
+ def self.included(base)
13
+ base.extend(Delete)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ module Nibo
2
+ module ApiResource
3
+ module List
4
+ def list(type=nil)
5
+ method = :get
6
+ url_with_method = "#{url}#{url_method(method)}s"
7
+ params = object_param(type, method) if type
8
+
9
+ response = api_request(url_with_method,method,params)
10
+
11
+ response.map{ |object| create_from(object)}
12
+ end
13
+
14
+ def self.included(base)
15
+ base.extend(List)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ module Nibo
2
+ module ApiResource
3
+ module Retrieve
4
+ def retrieve(obj_id)
5
+ method = :get
6
+ url_with_method = "#{url}#{url_method(method)}"
7
+ params = object_param(obj_id, method)
8
+
9
+ response = api_request(url_with_method,method,params)
10
+ create_from(response)
11
+ end
12
+
13
+ def self.included(base)
14
+ base.extend(Retrieve)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,22 @@
1
+ module Nibo
2
+ class Category < Nibo::Object
3
+ include ApiResource
4
+ include ApiResource::List
5
+
6
+ def self.url
7
+ "/#{CGI.escape(class_name)}"
8
+ end
9
+
10
+ def self.class_name
11
+ self.name.split('::')[-1]
12
+ end
13
+
14
+ def self.url_method(method)
15
+ case method
16
+ when :get
17
+ '/GetCategory'
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,47 @@
1
+ module Nibo
2
+ class Entity < Nibo::Object
3
+ include ApiResource
4
+ include ApiResource::Create
5
+ include ApiResource::List
6
+
7
+ def self.find_by(params)
8
+ entities = list(params[:type])
9
+ entity_result = nil
10
+ entities.each do |entity|
11
+ result = false
12
+ params.each do |key, value|
13
+ unless entity.send(key.to_s.camelcase) == params[key]
14
+ result = false
15
+ break
16
+ end
17
+
18
+ result = true
19
+ end
20
+ if result
21
+ entity_result = entity
22
+ break
23
+ end
24
+
25
+ end
26
+ entity_result
27
+ end
28
+
29
+ def self.url_method(method)
30
+ case method
31
+ when :get
32
+ '/GetEntitie'
33
+ when :post
34
+ '/CreateEntity'
35
+ end
36
+ end
37
+
38
+ def self.object_param(param, method)
39
+ case method
40
+ when :get
41
+ {type: param}
42
+ when :post
43
+ param
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,20 @@
1
+ module Nibo
2
+ class Entry < Nibo::Object
3
+ include ApiResource
4
+ include ApiResource::Create
5
+
6
+ def self.url_method(method)
7
+ case method
8
+ when :post
9
+ '/CreateEntry'
10
+ end
11
+ end
12
+
13
+ def self.object_param(param, method)
14
+ case method
15
+ when :post
16
+ param
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,85 @@
1
+ module Nibo
2
+ class Object
3
+ @values
4
+ def metaclass
5
+ class << self; self; end
6
+ end
7
+
8
+ def self.create_from(params)
9
+ obj = self.new
10
+ obj.refresh_object(params)
11
+ end
12
+
13
+ def refresh_object(params)
14
+ create_fields(params)
15
+ set_properties(params)
16
+ self
17
+ end
18
+
19
+ def create_fields(params)
20
+ instance_eval do
21
+ metaclass.instance_eval do
22
+ params.keys.each do |key|
23
+ define_method(key) { @values[key] }
24
+
25
+ define_method("#{key}=") do |value|
26
+ @values[key] = value
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ def set_properties(params)
34
+ instance_eval do
35
+ params.each do |key, value|
36
+ if value.class == Hash
37
+ @values[key] = Nibo::Object.create_from(value)
38
+ elsif value.class == Array
39
+ @values[key] = value.map{ |v| Nibo::Object.create_from(v) }
40
+ else
41
+ @values[key] = value
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ def self.url
48
+ "/#{CGI.escape(class_name)}"
49
+ end
50
+
51
+ def self.class_name
52
+ self.name.split('::')[-1]
53
+ end
54
+
55
+ def initialize
56
+ @values = {}
57
+ end
58
+
59
+ def to_hash
60
+ @values.inject({}) do |result, (key,value)|
61
+ if value.is_a? Array
62
+ list = []
63
+ value.each do |obj|
64
+ list.push(obj.respond_to?(:to_hash) ? obj.to_hash : value)
65
+ end
66
+ result[key] = list
67
+ elsif value.respond_to?(:to_hash)
68
+ result[key] = value.to_hash
69
+ else
70
+ result[key] = value
71
+ end
72
+ result
73
+ end
74
+ end
75
+
76
+ def to_json(*a)
77
+ JSON.generate(@values)
78
+ end
79
+
80
+ def to_s
81
+ JSON.generate(@values)
82
+ end
83
+ end
84
+ end
85
+
@@ -0,0 +1,20 @@
1
+ module Nibo
2
+ class Schedule < Nibo::Object
3
+ include ApiResource
4
+ include ApiResource::Create
5
+
6
+ def self.url_method(method)
7
+ case method
8
+ when :post
9
+ '/CreateSchedule'
10
+ end
11
+ end
12
+
13
+ def self.object_param(param, method)
14
+ case method
15
+ when :post
16
+ param
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module Nibo
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'nibo/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "nibo"
8
+ spec.version = Nibo::VERSION
9
+ spec.authors = ["Arthur Granado"]
10
+ spec.email = ["agranado@pluga.co"]
11
+
12
+ spec.summary = %q{Ruby gem that allow to access Nibo API.}
13
+ spec.description = %q{That gem allow the developer to access Nibo's RESTful API resource.}
14
+ spec.homepage = "https://github.com/PlugaDotCo/nibo-ruby"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "json", "~> 1.8"
23
+ spec.add_dependency "rest-client", "~> 1.8"
24
+ spec.add_dependency "activesupport", "~> 4.2"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.10"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec"
29
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nibo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Arthur Granado
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rest-client
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.8'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: That gem allow the developer to access Nibo's RESTful API resource.
98
+ email:
99
+ - agranado@pluga.co
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - bin/console
112
+ - bin/setup
113
+ - lib/nibo.rb
114
+ - lib/nibo/account.rb
115
+ - lib/nibo/api_resource.rb
116
+ - lib/nibo/api_resource/create.rb
117
+ - lib/nibo/api_resource/delete.rb
118
+ - lib/nibo/api_resource/list.rb
119
+ - lib/nibo/api_resource/retrieve.rb
120
+ - lib/nibo/category.rb
121
+ - lib/nibo/entity.rb
122
+ - lib/nibo/entry.rb
123
+ - lib/nibo/object.rb
124
+ - lib/nibo/schedule.rb
125
+ - lib/nibo/version.rb
126
+ - nibo.gemspec
127
+ homepage: https://github.com/PlugaDotCo/nibo-ruby
128
+ licenses:
129
+ - MIT
130
+ metadata: {}
131
+ post_install_message:
132
+ rdoc_options: []
133
+ require_paths:
134
+ - lib
135
+ required_ruby_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ requirements: []
146
+ rubyforge_project:
147
+ rubygems_version: 2.4.8
148
+ signing_key:
149
+ specification_version: 4
150
+ summary: Ruby gem that allow to access Nibo API.
151
+ test_files: []