service_api 0.0.1

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: ba6978a7be8e0338599cebac7b88b19dd2c75820
4
+ data.tar.gz: 756d2a3a25c34a92f7cb5f89ac227472030413c4
5
+ SHA512:
6
+ metadata.gz: 61642864e157207a38556f4f1178dc19bfb6928fb27e3167c07b56918e649cfc64b7804cc0ee7ea6ad27fdaa3c113e229cca03affbd0f651e5e4b56731a12575
7
+ data.tar.gz: cf12d4612c7b6f2b19c045ba0c4e89ba0d719fd0d92b123c91e80a5c46a85a4abb74eac4880f2fb61aeefe51de00f80c930979f585b92b6e69726d2e82a034d2
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in service_api.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Krzysztof Wawer
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,29 @@
1
+ # ServiceApi
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'service_api'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install service_api
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/service_api/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,5 @@
1
+ module ServiceApi; end
2
+
3
+ require 'service_api/version'
4
+ require 'service_api/base_faraday'
5
+ require 'service_api/uri_tokens'
@@ -0,0 +1,109 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+ require 'uri_template'
4
+
5
+ module ServiceApi
6
+ # BaseFaraday is module for Faraday api which should be included in own base Api class.
7
+ #
8
+ # class MyApi
9
+ # include BaseApi
10
+ #
11
+ # private
12
+ #
13
+ # def base_url
14
+ # 'http://example.com'
15
+ # end
16
+ # end
17
+ #
18
+ module BaseFaraday
19
+
20
+ # Initilizer require client class as parameter
21
+ #
22
+ # MyApi.new(client)
23
+ #
24
+ def initialize(client)
25
+ @client = client
26
+ @params = {}
27
+ @mandatory_params = {}
28
+ @optional_params = {}
29
+ end
30
+
31
+ protected
32
+
33
+ # Available get, head, delete, post, put and patch method with path
34
+ # parameter. This method must be call as last method
35
+ #
36
+ # path('/test/request').params(sample: true, api_key: 'abcd').get
37
+ #
38
+ %w[get head delete post put patch].each do |method|
39
+ define_method method do
40
+ connection.send(method, uri, query_params)
41
+ end
42
+ end
43
+
44
+ # Pass path to request
45
+ #
46
+ # path('/test/request')
47
+ #
48
+ def path(path)
49
+ uri_tokens(path)
50
+ self
51
+ end
52
+
53
+ # Pass parameters to request
54
+ #
55
+ # params(sample: true, api_key: 'abcd')
56
+ #
57
+ def params(options)
58
+ @params = options
59
+ self
60
+ end
61
+
62
+ # Last method called in chain which return full url request
63
+ #
64
+ # path('/test/request').params(sample: true, api_key: 'abcd').url
65
+ #
66
+ def url
67
+ "#{base_url}#{uri}"
68
+ end
69
+
70
+ private
71
+
72
+ def connection
73
+ @connection ||= Faraday.new(url: base_url) do |builder|
74
+ builder.response :xml, content_type: /\bxml$/
75
+ builder.response :json, content_type: /\bjson$/
76
+
77
+ if @client.options[:adapter] == :test
78
+ builder.adapter @client.options[:adapter], @client.options[:adapter_options]
79
+ else
80
+ builder.adapter @client.options[:adapter]
81
+ end
82
+ end
83
+ end
84
+
85
+ def token?(value)
86
+ uri_tokens.token_values.include?(value.to_s)
87
+ end
88
+
89
+ def uri_tokens(uri = nil)
90
+ if uri
91
+ @uri_tokens = ServiceApi::UriTokens.new(uri)
92
+ else
93
+ @uri_tokens
94
+ end
95
+ end
96
+
97
+ def uri
98
+ uri_tokens.uri_template.expand(@params)
99
+ end
100
+
101
+ def query_params
102
+ @params.reject{ |request_params| token?(request_params) }
103
+ end
104
+
105
+ def base_url
106
+ raise NotImplementedError
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,19 @@
1
+ module ServiceApi
2
+ class UriTokens
3
+ attr_reader :uri_template
4
+
5
+ def initialize(uri_template)
6
+ @uri_template = URITemplate.new(uri_template)
7
+ end
8
+
9
+ def token_values
10
+ @token_values ||= tokens.map(&:variables).flatten
11
+ end
12
+
13
+ private
14
+
15
+ def tokens
16
+ @tokens ||= @uri_template.tokens.select{ |template| !template.variables.empty? }
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module ServiceApi
2
+ VERSION = '0.0.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 'service_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'service_api'
8
+ spec.version = ServiceApi::VERSION
9
+ spec.authors = ['Krzysztof Wawer']
10
+ spec.email = ['krzysztof.wawer@gmail.com']
11
+ spec.summary = %q{Set of useful modules, classes for api gems}
12
+ spec.description = %q{Set of useful modules, classes for api gems}
13
+ spec.homepage = %q{http://github.com/wafcio/service_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 'faraday', '~> 0.9.0'
22
+ spec.add_runtime_dependency 'uri_template', '~> 0.7.0'
23
+ spec.add_runtime_dependency 'faraday_middleware', '~> 0.9.1'
24
+ spec.add_runtime_dependency 'multi_xml', '~> 0.5.5'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.5'
27
+ spec.add_development_dependency 'rake'
28
+ spec.add_development_dependency 'rspec', '~> 2.14.1'
29
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ class Client
4
+ def options
5
+ { adapter: :test, adapter_options: faraday_stubs }
6
+ end
7
+
8
+ def faraday_stubs
9
+ Faraday::Adapter::Test::Stubs.new do |stub|
10
+ stub.get('/test/request?api_key=abcd&sample=true') { [200, {}, 'OK'] }
11
+ end
12
+ end
13
+ end
14
+
15
+ class MyApi
16
+ include ServiceApi::BaseFaraday
17
+
18
+ private
19
+
20
+ def base_url
21
+ 'http://example.com'
22
+ end
23
+ end
24
+
25
+ class SampleApi < MyApi
26
+ def find
27
+ path('/test/request').params(sample: true, api_key: 'abcd').get
28
+ end
29
+
30
+ def find_url
31
+ path('/test/request').params(sample: true, api_key: 'abcd').url
32
+ end
33
+ end
34
+
35
+ describe ServiceApi::BaseFaraday do
36
+ let(:client) { Client.new }
37
+ let(:model) { SampleApi.new(client) }
38
+
39
+ it 'should return string' do
40
+ model.find.body.should == 'OK'
41
+ end
42
+
43
+ it 'should return full url' do
44
+ model.find_url.should == 'http://example.com/test/request'
45
+ end
46
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe ServiceApi::UriTokens do
4
+ let(:template) { ServiceApi::UriTokens.new('http://{host}/{segments}/{file}.{extensions}') }
5
+
6
+ it 'should return token values' do
7
+ template.token_values.sort.should == ['extensions', 'file', 'host', 'segments']
8
+ end
9
+ end
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'service_api'
4
+
5
+ RSpec.configure do |config|
6
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: service_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Krzysztof Wawer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-20 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.9.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: uri_template
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 0.7.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 0.7.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday_middleware
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 0.9.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.9.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: multi_xml
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.5.5
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 0.5.5
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '1.5'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '1.5'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
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
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ~>
102
+ - !ruby/object:Gem::Version
103
+ version: 2.14.1
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ~>
109
+ - !ruby/object:Gem::Version
110
+ version: 2.14.1
111
+ description: Set of useful modules, classes for api gems
112
+ email:
113
+ - krzysztof.wawer@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - .gitignore
119
+ - Gemfile
120
+ - LICENSE.txt
121
+ - README.md
122
+ - Rakefile
123
+ - lib/service_api.rb
124
+ - lib/service_api/base_faraday.rb
125
+ - lib/service_api/uri_tokens.rb
126
+ - lib/service_api/version.rb
127
+ - service_api.gemspec
128
+ - spec/integrations/base_faraday_spec.rb
129
+ - spec/integrations/uri_tokens_spec.rb
130
+ - spec/spec_helper.rb
131
+ homepage: http://github.com/wafcio/service_api
132
+ licenses:
133
+ - MIT
134
+ metadata: {}
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - '>='
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - '>='
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project:
151
+ rubygems_version: 2.0.3
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: Set of useful modules, classes for api gems
155
+ test_files:
156
+ - spec/integrations/base_faraday_spec.rb
157
+ - spec/integrations/uri_tokens_spec.rb
158
+ - spec/spec_helper.rb