ll_wk_api 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +54 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/ll.rb +5 -0
- data/lib/ll/wk.rb +6 -0
- data/lib/ll/wk/api.rb +34 -0
- data/lib/ll/wk/api/connection.rb +24 -0
- data/lib/ll/wk/api/connection/base.rb +147 -0
- data/lib/ll/wk/api/connection/curb.rb +108 -0
- data/lib/ll/wk/api/connection/httparty.rb +45 -0
- data/lib/ll/wk/api/connection/new_curb.rb +53 -0
- data/lib/ll/wk/api/test +3 -0
- data/lib/ll/wk/api/version.rb +7 -0
- data/ll_wk_api.gemspec +28 -0
- metadata +149 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 977438fa8a5d0cdb8e7a090a6f21efaa99e3d749
|
4
|
+
data.tar.gz: cbb11660ca267b71584f7bf9f5a5297ce1f7f8f9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 94631c1e61b027b73ad7aa47fc98c4fe231d1c3dcd97c5b1167c2b15648f2cf34e095f0f72ae64db1f9ba062b00bee033b34a473a876f52763e59a50d8e33d12
|
7
|
+
data.tar.gz: 00a7cf829f516aaed440122be0a0afe0e65075a9b211bb5f4a3344a88b551e23daca4785968cbb60abf1caf88310e3ee8b732fb491530be2e99fcef8c3f085d5
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at m.mofrad@livelinktechnology.net. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Matt Mofrad
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# LL::WK::Api
|
2
|
+
|
3
|
+
## Roughly how works:
|
4
|
+
```
|
5
|
+
require 'll_wk_api'
|
6
|
+
api = LL::WK::API.connect(url: 'api_url', username: 'username', password: 'password')
|
7
|
+
context = { date_from: Time.now.to_i -7200
|
8
|
+
date_to: Time.now.to_i,
|
9
|
+
per_page: 1000
|
10
|
+
}
|
11
|
+
api.from_api('users', context) do |u|
|
12
|
+
puts u['id']
|
13
|
+
end
|
14
|
+
api.search_for_users(1533824907) #Some random time ago
|
15
|
+
api.search_for_user_album_items(some_user_id) # returns the array of user
|
16
|
+
|
17
|
+
```
|
18
|
+
## Installation
|
19
|
+
|
20
|
+
Add this line to your application's Gemfile:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
gem 'll_wk_api'
|
24
|
+
```
|
25
|
+
|
26
|
+
And then execute:
|
27
|
+
|
28
|
+
$ bundle
|
29
|
+
|
30
|
+
Or install it yourself as:
|
31
|
+
|
32
|
+
$ gem install ll_wk_api
|
33
|
+
|
34
|
+
## Usage
|
35
|
+
|
36
|
+
TODO: Write usage instructions here
|
37
|
+
|
38
|
+
## Development
|
39
|
+
|
40
|
+
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.
|
41
|
+
|
42
|
+
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).
|
43
|
+
|
44
|
+
## Contributing
|
45
|
+
|
46
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ll_wk_api. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
47
|
+
|
48
|
+
## License
|
49
|
+
|
50
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
51
|
+
|
52
|
+
## Code of Conduct
|
53
|
+
|
54
|
+
Everyone interacting in the LL::WK::API project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/ll_wk_api/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'll'
|
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(__FILE__)
|
data/bin/setup
ADDED
data/lib/ll.rb
ADDED
data/lib/ll/wk.rb
ADDED
data/lib/ll/wk/api.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'll/wk/api/connection'
|
2
|
+
module LL
|
3
|
+
module WK
|
4
|
+
# API Functionality for clients of the WebKiosk
|
5
|
+
module API
|
6
|
+
RUN_BENCHMARKS = true
|
7
|
+
|
8
|
+
module_function
|
9
|
+
|
10
|
+
attr_writer :logger
|
11
|
+
|
12
|
+
def logger
|
13
|
+
@logger ||= Logger.new(STDERR)
|
14
|
+
end
|
15
|
+
|
16
|
+
def connect(url:, email:, password:)
|
17
|
+
API::Connection.factory(url: url, email: email, password: password)
|
18
|
+
end
|
19
|
+
|
20
|
+
def benchmark(name = nil)
|
21
|
+
return yield unless RUN_BENCHMARKS
|
22
|
+
require 'benchmark'
|
23
|
+
logger.debug("Execution of block #{name} commencing at #{Time.now}")
|
24
|
+
res = nil
|
25
|
+
time = Benchmark.measure do
|
26
|
+
res = yield
|
27
|
+
end
|
28
|
+
readable = Time.at(time.real).strftime('%Mm, %S.%Ls')
|
29
|
+
logger.debug("Execution of block #{name} complete at #{Time.now}. Executed in: {#{readable}}")
|
30
|
+
res
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'll/wk/api/connection/base'
|
2
|
+
#require 'll/wk/api/connection/httparty'
|
3
|
+
#require 'll/wk/api/connection/curb'
|
4
|
+
module LL
|
5
|
+
module WK
|
6
|
+
module API
|
7
|
+
module Connection
|
8
|
+
SUPPORTS_CURSOR = false
|
9
|
+
|
10
|
+
module_function
|
11
|
+
|
12
|
+
def register(name, klass)
|
13
|
+
@@connectors ||= {}
|
14
|
+
@@connectors[name] = klass
|
15
|
+
end
|
16
|
+
|
17
|
+
def factory(url:, email:, password:, type: 'httparty')
|
18
|
+
require "ll/wk/api/connection/#{type}"
|
19
|
+
@@connectors[type].new(url: url, email: email, password: password)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,147 @@
|
|
1
|
+
module LL
|
2
|
+
module WK
|
3
|
+
module API
|
4
|
+
module Connection
|
5
|
+
class Base
|
6
|
+
attr_reader :token, :token_issued, :url, :email, :password
|
7
|
+
|
8
|
+
def self.authenticate_payload(email, password)
|
9
|
+
{
|
10
|
+
user: {
|
11
|
+
email: email,
|
12
|
+
password: password
|
13
|
+
}
|
14
|
+
}.to_json
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.trap_resp_code(code, klass = LL::WK::API::Connection::Error)
|
18
|
+
case code
|
19
|
+
when 403
|
20
|
+
LL::WK::API.logger.debug('API Unautorized')
|
21
|
+
raise LL::WK::API::Connection::AuthenticationError, 'Unauthorized'
|
22
|
+
when 404
|
23
|
+
LL::WK::API.logger.debug('API invalid endpoint')
|
24
|
+
raise klass, 'Invalid endpoint'
|
25
|
+
when 400..499
|
26
|
+
LL::WK::API.logger.debug("API Client Error: #{code}")
|
27
|
+
raise klass, 'Client error #{code}'
|
28
|
+
when 500..599
|
29
|
+
LL::WK::API.logger.debug("API Server Error: #{code}")
|
30
|
+
raise klass, "Server Error #{code}"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def initialize(url:, email:, password:)
|
35
|
+
LL::WK::API.logger.debug("API Connection initialize(url: #{url}, email: #{email}, password: xxxx)")
|
36
|
+
@url = url
|
37
|
+
@email = email
|
38
|
+
@password = password
|
39
|
+
end
|
40
|
+
|
41
|
+
def auth
|
42
|
+
"Token token=\"#{@token}\", email=\"#{@email}\""
|
43
|
+
end
|
44
|
+
|
45
|
+
def authenticate!(force = false)
|
46
|
+
LL::WK::API.logger.debug("API authenitcate!(#{force})")
|
47
|
+
return self unless token_expired? or force
|
48
|
+
|
49
|
+
retries = 0
|
50
|
+
begin
|
51
|
+
self.class.authenticate(email, password, url) do |resp|
|
52
|
+
self.class.trap_resp_code(resp.code, LL::WK::API::Connection::AuthenticationError)
|
53
|
+
@token = resp.parsed_response['token']
|
54
|
+
@token_issued = Time.now
|
55
|
+
end
|
56
|
+
rescue LL::WK::API::Connection::AuthenticationError => e
|
57
|
+
retries += 1
|
58
|
+
sleep retries
|
59
|
+
retry if retries < 2
|
60
|
+
raise e
|
61
|
+
end
|
62
|
+
self
|
63
|
+
end
|
64
|
+
|
65
|
+
def from_api(endpoint, params, &block)
|
66
|
+
# with_page modifies the context, so need to clone it otherwise you
|
67
|
+
# get unexpected behaviour from the calling context
|
68
|
+
p = params.clone
|
69
|
+
LL::WK::API.logger.debug("API from_api ep: #{endpoint} params: #{params}")
|
70
|
+
retries = 0
|
71
|
+
return with_cursor(endpoint, p, &block) if LL::WK::API::Connection::SUPPORTS_CURSOR
|
72
|
+
with_page(endpoint, p, &block)
|
73
|
+
rescue AuthenticationError => e
|
74
|
+
authenticate!
|
75
|
+
retries += 1
|
76
|
+
retry if retries < 3
|
77
|
+
raise e
|
78
|
+
end
|
79
|
+
|
80
|
+
def page_count(resp)
|
81
|
+
resp['paging']&.[]('total')
|
82
|
+
end
|
83
|
+
|
84
|
+
def token_expired?
|
85
|
+
return true unless token
|
86
|
+
true unless (Time.now - token_issued.to_i).to_i < 76400
|
87
|
+
end
|
88
|
+
|
89
|
+
def with_cursor(endpoint, params)
|
90
|
+
LL::WK::API.logger.debug("API call uses cursors")
|
91
|
+
params[:cursor] ||= 0
|
92
|
+
array = []
|
93
|
+
while !params[:cursor].nil?
|
94
|
+
retrieve_data(endpoint, params) do |resp|
|
95
|
+
resp['data'].each do |item|
|
96
|
+
array << item
|
97
|
+
yield(item) if block_given?
|
98
|
+
end
|
99
|
+
params[:cursor] = resp[:cursor]
|
100
|
+
end
|
101
|
+
end
|
102
|
+
array
|
103
|
+
end
|
104
|
+
|
105
|
+
def with_page(endpoint, params)
|
106
|
+
LL::WK::API.logger.debug("API call uses pagination")
|
107
|
+
params[:page] ||= 1
|
108
|
+
pages_remain = params[:page] + 1
|
109
|
+
array = []
|
110
|
+
while params[:page] < pages_remain
|
111
|
+
retrieve_data(endpoint, params) do |resp|
|
112
|
+
resp['data'].each do |item|
|
113
|
+
array << item
|
114
|
+
yield(item) if block_given?
|
115
|
+
end
|
116
|
+
pages_remain = page_count(resp)
|
117
|
+
LL::WK::API.logger.debug("finished with page #{params[:page]} of #{pages_remain}")
|
118
|
+
params[:page] += 1
|
119
|
+
end
|
120
|
+
end
|
121
|
+
LL::WK::API.logger.debug "found or worked on #{array.size} items"
|
122
|
+
array
|
123
|
+
end
|
124
|
+
|
125
|
+
def retrieve_data(endpoint, params, &block)
|
126
|
+
retries = 0
|
127
|
+
LL::WK::API.benchmark("http call #{endpoint}") do
|
128
|
+
response_from_api(endpoint, params, &block)
|
129
|
+
end
|
130
|
+
rescue LL::WK::API::Connection::Error => e
|
131
|
+
authenticate!(true)
|
132
|
+
retries += 1
|
133
|
+
sleep 1
|
134
|
+
retry if retries < 3
|
135
|
+
raise e
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
class Error < StandardError
|
140
|
+
end
|
141
|
+
|
142
|
+
class AuthenticationError < Error
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'curb'
|
2
|
+
require 'json'
|
3
|
+
module LL
|
4
|
+
module WK
|
5
|
+
module API
|
6
|
+
module Connection
|
7
|
+
class Curb
|
8
|
+
attr_accessor :token, :endpoint, :params,
|
9
|
+
:config, :email, :token_issued
|
10
|
+
|
11
|
+
def auth
|
12
|
+
"Token token=\"#{@token}\", email=\"#{@email}\""
|
13
|
+
end
|
14
|
+
|
15
|
+
def curl_easy(uri)
|
16
|
+
Curl::Easy.new(uri) do |c|
|
17
|
+
c.follow_location = true
|
18
|
+
c.headers['Authorization'] = auth unless token_expired?
|
19
|
+
yield(c) if block_given?
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def initialize(url:, email:, password:)
|
24
|
+
@api_url = url
|
25
|
+
@email = email
|
26
|
+
@password = password
|
27
|
+
generate_token
|
28
|
+
end
|
29
|
+
|
30
|
+
def token_payload
|
31
|
+
{ user: { email: @email, password: @password } }.to_json
|
32
|
+
end
|
33
|
+
|
34
|
+
def token_expired?
|
35
|
+
return true unless token
|
36
|
+
token_age = (Time.now - token_issued.to_i).to_i
|
37
|
+
true unless token_age < 76400
|
38
|
+
end
|
39
|
+
|
40
|
+
def generate_token
|
41
|
+
Curl::Easy.new("#{@api_url}/session/new") do |curl|
|
42
|
+
curl.headers['Content-Type'] = 'application/json'
|
43
|
+
curl.http_post(token_payload)
|
44
|
+
resp = JSON.parse(curl.body_str)
|
45
|
+
raise 'Invalid' if resp['status'] && resp['status'] == 'unauthenticated'
|
46
|
+
@token_issued = Time.now
|
47
|
+
@token = resp['token']
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def search_for_users(from_date = 0, to_date = Time.now.to_i)
|
52
|
+
from_api('users?', "date_from=#{from_date}&date_to=#{to_date}")
|
53
|
+
end
|
54
|
+
|
55
|
+
def search_for_user_album_items(userid)
|
56
|
+
from_api('user_album_items?', "user_id=#{userid}")
|
57
|
+
end
|
58
|
+
|
59
|
+
def with_each_page_data(resp)
|
60
|
+
resp['data'].each { |d| yield d }
|
61
|
+
rescue NoMethodError => e
|
62
|
+
raise e
|
63
|
+
end
|
64
|
+
|
65
|
+
def page_count(resp)
|
66
|
+
resp['paging']&.[]('total')
|
67
|
+
end
|
68
|
+
|
69
|
+
def from_api(end_point, params)
|
70
|
+
# gather data from the api
|
71
|
+
array = []
|
72
|
+
curl_easy("#{@api_url}/#{end_point}#{params}") do |curl|
|
73
|
+
with_get(curl) do |out|
|
74
|
+
with_each_page_data(out) do |result|
|
75
|
+
array << result
|
76
|
+
end
|
77
|
+
array << do_pagination(end_point, params, page_count(out)) unless page_count(out).zero?
|
78
|
+
end
|
79
|
+
end
|
80
|
+
array.flatten!
|
81
|
+
array
|
82
|
+
rescue StandardError => e
|
83
|
+
raise e
|
84
|
+
end
|
85
|
+
|
86
|
+
def with_get(http)
|
87
|
+
http.perform
|
88
|
+
out = JSON.parse(http.body_str)
|
89
|
+
yield(out)
|
90
|
+
out
|
91
|
+
end
|
92
|
+
|
93
|
+
def do_pagination(end_point, params, pages)
|
94
|
+
# grabs data from each of the pages returned by the API
|
95
|
+
results = []
|
96
|
+
(2..pages).each do |page|
|
97
|
+
curl_easy("#{@api_url}/#{end_point}#{params}&page=#{page}") do |curl|
|
98
|
+
with_get(curl) { |resp| with_each_page_data(resp) { |result| results << result } }
|
99
|
+
end
|
100
|
+
end
|
101
|
+
results
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
require 'logger'
|
3
|
+
require 'thread'
|
4
|
+
|
5
|
+
module LL
|
6
|
+
module WK
|
7
|
+
module API
|
8
|
+
module Connection
|
9
|
+
class HTTParty < Base
|
10
|
+
include ::HTTParty
|
11
|
+
::LL::WK::API::Connection.register('httparty', self)
|
12
|
+
|
13
|
+
def self.authenticate(email, password, url)
|
14
|
+
base_uri url
|
15
|
+
resp = post('/session/new',
|
16
|
+
body: authenticate_payload(email, password),
|
17
|
+
headers: { 'Content-Type' => 'application/json' })
|
18
|
+
yield(resp) if block_given?
|
19
|
+
resp
|
20
|
+
end
|
21
|
+
|
22
|
+
def initialize(url:, email:, password:)
|
23
|
+
self.class.base_uri url
|
24
|
+
super(url: url, email: email, password: password)
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
def response_from_api(endpoint, params)
|
29
|
+
self.class.base_uri url
|
30
|
+
resp = self.class.get(sanitize_endpoint(endpoint), query: params, headers: { 'Authorization' => auth })
|
31
|
+
self.class.trap_resp_code(resp.code)
|
32
|
+
yield(resp) if block_given?
|
33
|
+
resp
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def sanitize_endpoint(endpoint)
|
39
|
+
File.join('', endpoint)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'curb'
|
2
|
+
require 'json'
|
3
|
+
require 'addressable/uri'
|
4
|
+
|
5
|
+
class Hash
|
6
|
+
def to_query
|
7
|
+
uri = Addressable::URI.new
|
8
|
+
uri.query_values = self
|
9
|
+
uri.query
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module LL
|
14
|
+
module WK
|
15
|
+
module API
|
16
|
+
module Connection
|
17
|
+
class Curb < Base
|
18
|
+
::LL::WK::API::Connection.register('curb', self)
|
19
|
+
|
20
|
+
def self.autenticate(email, password, url)
|
21
|
+
curl_easy("#{url}/session/new") do |curl|
|
22
|
+
curl.headers['Content-Type'] = 'application/json'
|
23
|
+
curl.http_post(authenticate_payload(email, password))
|
24
|
+
resp = Json.parse(curl.body_str)
|
25
|
+
self.class.trap_resp_code(resp['status'])
|
26
|
+
return resp
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def response_from_api(endpoint, params)
|
31
|
+
curl_easy("#{url}/#{endpoint}?#{params.to_query}") do |curl|
|
32
|
+
curl.perform
|
33
|
+
resp = JSON.parse(curl.body_str)
|
34
|
+
self.class.trap_resp_code(curl.status)
|
35
|
+
yield(resp) if block_given?
|
36
|
+
return resp
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def curl_easy(uri)
|
43
|
+
Curl::Easy.new(uri) do |c|
|
44
|
+
c.follow_location = true
|
45
|
+
c.headers['Authorization'] = auth unless token_expired?
|
46
|
+
yield(c) if block_given?
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/lib/ll/wk/api/test
ADDED
data/ll_wk_api.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'll/wk/api/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'll_wk_api'
|
7
|
+
spec.version = LL::WK::API::VERSION
|
8
|
+
spec.authors = ['Matt Mofrad']
|
9
|
+
spec.email = ['m.mofrad@livelinktechnology.net']
|
10
|
+
|
11
|
+
spec.summary = 'Gem for interacting with livelink webkiosks'
|
12
|
+
spec.homepage = 'https://github.com/Zibby/wk_api_gem'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
16
|
+
f.match(%r{^(test|spec|features)/})
|
17
|
+
end
|
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_development_dependency 'bundler', '~> 1.16'
|
23
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
24
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
25
|
+
spec.add_dependency 'curb'
|
26
|
+
spec.add_dependency 'httparty'
|
27
|
+
spec.add_dependency 'addressable'
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ll_wk_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matt Mofrad
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-08-20 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.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
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
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: curb
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: httparty
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: addressable
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description:
|
98
|
+
email:
|
99
|
+
- m.mofrad@livelinktechnology.net
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- ".travis.yml"
|
107
|
+
- CODE_OF_CONDUCT.md
|
108
|
+
- Gemfile
|
109
|
+
- LICENSE.txt
|
110
|
+
- README.md
|
111
|
+
- Rakefile
|
112
|
+
- bin/console
|
113
|
+
- bin/setup
|
114
|
+
- lib/ll.rb
|
115
|
+
- lib/ll/wk.rb
|
116
|
+
- lib/ll/wk/api.rb
|
117
|
+
- lib/ll/wk/api/connection.rb
|
118
|
+
- lib/ll/wk/api/connection/base.rb
|
119
|
+
- lib/ll/wk/api/connection/curb.rb
|
120
|
+
- lib/ll/wk/api/connection/httparty.rb
|
121
|
+
- lib/ll/wk/api/connection/new_curb.rb
|
122
|
+
- lib/ll/wk/api/test
|
123
|
+
- lib/ll/wk/api/version.rb
|
124
|
+
- ll_wk_api.gemspec
|
125
|
+
homepage: https://github.com/Zibby/wk_api_gem
|
126
|
+
licenses:
|
127
|
+
- MIT
|
128
|
+
metadata: {}
|
129
|
+
post_install_message:
|
130
|
+
rdoc_options: []
|
131
|
+
require_paths:
|
132
|
+
- lib
|
133
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - ">="
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: '0'
|
138
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
139
|
+
requirements:
|
140
|
+
- - ">="
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: '0'
|
143
|
+
requirements: []
|
144
|
+
rubyforge_project:
|
145
|
+
rubygems_version: 2.6.13
|
146
|
+
signing_key:
|
147
|
+
specification_version: 4
|
148
|
+
summary: Gem for interacting with livelink webkiosks
|
149
|
+
test_files: []
|