edis_client 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +203 -0
- data/README.md +28 -0
- data/lib/edis_client.rb +345 -0
- metadata +137 -0
data/LICENSE
ADDED
@@ -0,0 +1,203 @@
|
|
1
|
+
/*
|
2
|
+
* Apache License
|
3
|
+
* Version 2.0, January 2004
|
4
|
+
* http://www.apache.org/licenses/
|
5
|
+
*
|
6
|
+
* TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
*
|
8
|
+
* 1. Definitions.
|
9
|
+
*
|
10
|
+
* "License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
* and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
*
|
13
|
+
* "Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
* the copyright owner that is granting the License.
|
15
|
+
*
|
16
|
+
* "Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
* other entities that control, are controlled by, or are under common
|
18
|
+
* control with that entity. For the purposes of this definition,
|
19
|
+
* "control" means (i) the power, direct or indirect, to cause the
|
20
|
+
* direction or management of such entity, whether by contract or
|
21
|
+
* otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
* outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
*
|
24
|
+
* "You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
* exercising permissions granted by this License.
|
26
|
+
*
|
27
|
+
* "Source" form shall mean the preferred form for making modifications,
|
28
|
+
* including but not limited to software source code, documentation
|
29
|
+
* source, and configuration files.
|
30
|
+
*
|
31
|
+
* "Object" form shall mean any form resulting from mechanical
|
32
|
+
* transformation or translation of a Source form, including but
|
33
|
+
* not limited to compiled object code, generated documentation,
|
34
|
+
* and conversions to other media types.
|
35
|
+
*
|
36
|
+
* "Work" shall mean the work of authorship, whether in Source or
|
37
|
+
* Object form, made available under the License, as indicated by a
|
38
|
+
* copyright notice that is included in or attached to the work
|
39
|
+
* (an example is provided in the Appendix below).
|
40
|
+
*
|
41
|
+
* "Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
* form, that is based on (or derived from) the Work and for which the
|
43
|
+
* editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
* represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
* of this License, Derivative Works shall not include works that remain
|
46
|
+
* separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
* the Work and Derivative Works thereof.
|
48
|
+
*
|
49
|
+
* "Contribution" shall mean any work of authorship, including
|
50
|
+
* the original version of the Work and any modifications or additions
|
51
|
+
* to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
* submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
* or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
* the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
* means any form of electronic, verbal, or written communication sent
|
56
|
+
* to the Licensor or its representatives, including but not limited to
|
57
|
+
* communication on electronic mailing lists, source code control systems,
|
58
|
+
* and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
* Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
* excluding communication that is conspicuously marked or otherwise
|
61
|
+
* designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
*
|
63
|
+
* "Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
* on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
* subsequently incorporated within the Work.
|
66
|
+
*
|
67
|
+
* 2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
* this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
* worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
* copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
* publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
* Work and such Derivative Works in Source or Object form.
|
73
|
+
*
|
74
|
+
* 3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
* this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
* worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
* (except as stated in this section) patent license to make, have made,
|
78
|
+
* use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
* where such license applies only to those patent claims licensable
|
80
|
+
* by such Contributor that are necessarily infringed by their
|
81
|
+
* Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
* with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
* institute patent litigation against any entity (including a
|
84
|
+
* cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
* or a Contribution incorporated within the Work constitutes direct
|
86
|
+
* or contributory patent infringement, then any patent licenses
|
87
|
+
* granted to You under this License for that Work shall terminate
|
88
|
+
* as of the date such litigation is filed.
|
89
|
+
*
|
90
|
+
* 4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
* Work or Derivative Works thereof in any medium, with or without
|
92
|
+
* modifications, and in Source or Object form, provided that You
|
93
|
+
* meet the following conditions:
|
94
|
+
*
|
95
|
+
* (a) You must give any other recipients of the Work or
|
96
|
+
* Derivative Works a copy of this License; and
|
97
|
+
*
|
98
|
+
* (b) You must cause any modified files to carry prominent notices
|
99
|
+
* stating that You changed the files; and
|
100
|
+
*
|
101
|
+
* (c) You must retain, in the Source form of any Derivative Works
|
102
|
+
* that You distribute, all copyright, patent, trademark, and
|
103
|
+
* attribution notices from the Source form of the Work,
|
104
|
+
* excluding those notices that do not pertain to any part of
|
105
|
+
* the Derivative Works; and
|
106
|
+
*
|
107
|
+
* (d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
* distribution, then any Derivative Works that You distribute must
|
109
|
+
* include a readable copy of the attribution notices contained
|
110
|
+
* within such NOTICE file, excluding those notices that do not
|
111
|
+
* pertain to any part of the Derivative Works, in at least one
|
112
|
+
* of the following places: within a NOTICE text file distributed
|
113
|
+
* as part of the Derivative Works; within the Source form or
|
114
|
+
* documentation, if provided along with the Derivative Works; or,
|
115
|
+
* within a display generated by the Derivative Works, if and
|
116
|
+
* wherever such third-party notices normally appear. The contents
|
117
|
+
* of the NOTICE file are for informational purposes only and
|
118
|
+
* do not modify the License. You may add Your own attribution
|
119
|
+
* notices within Derivative Works that You distribute, alongside
|
120
|
+
* or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
* that such additional attribution notices cannot be construed
|
122
|
+
* as modifying the License.
|
123
|
+
*
|
124
|
+
* You may add Your own copyright statement to Your modifications and
|
125
|
+
* may provide additional or different license terms and conditions
|
126
|
+
* for use, reproduction, or distribution of Your modifications, or
|
127
|
+
* for any such Derivative Works as a whole, provided Your use,
|
128
|
+
* reproduction, and distribution of the Work otherwise complies with
|
129
|
+
* the conditions stated in this License.
|
130
|
+
*
|
131
|
+
* 5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
* any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
* by You to the Licensor shall be under the terms and conditions of
|
134
|
+
* this License, without any additional terms or conditions.
|
135
|
+
* Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
* the terms of any separate license agreement you may have executed
|
137
|
+
* with Licensor regarding such Contributions.
|
138
|
+
*
|
139
|
+
* 6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
* names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
* except as required for reasonable and customary use in describing the
|
142
|
+
* origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
*
|
144
|
+
* 7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
* agreed to in writing, Licensor provides the Work (and each
|
146
|
+
* Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
* implied, including, without limitation, any warranties or conditions
|
149
|
+
* of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
* PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
* appropriateness of using or redistributing the Work and assume any
|
152
|
+
* risks associated with Your exercise of permissions under this License.
|
153
|
+
*
|
154
|
+
* 8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
* whether in tort (including negligence), contract, or otherwise,
|
156
|
+
* unless required by applicable law (such as deliberate and grossly
|
157
|
+
* negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
* liable to You for damages, including any direct, indirect, special,
|
159
|
+
* incidental, or consequential damages of any character arising as a
|
160
|
+
* result of this License or out of the use or inability to use the
|
161
|
+
* Work (including but not limited to damages for loss of goodwill,
|
162
|
+
* work stoppage, computer failure or malfunction, or any and all
|
163
|
+
* other commercial damages or losses), even if such Contributor
|
164
|
+
* has been advised of the possibility of such damages.
|
165
|
+
*
|
166
|
+
* 9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
* the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
* and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
* or other liability obligations and/or rights consistent with this
|
170
|
+
* License. However, in accepting such obligations, You may act only
|
171
|
+
* on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
* of any other Contributor, and only if You agree to indemnify,
|
173
|
+
* defend, and hold each Contributor harmless for any liability
|
174
|
+
* incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
* of your accepting any such warranty or additional liability.
|
176
|
+
*
|
177
|
+
* END OF TERMS AND CONDITIONS
|
178
|
+
*
|
179
|
+
* APPENDIX: How to apply the Apache License to your work.
|
180
|
+
*
|
181
|
+
* To apply the Apache License to your work, attach the following
|
182
|
+
* boilerplate notice, with the fields enclosed by brackets "[]"
|
183
|
+
* replaced with your own identifying information. (Don't include
|
184
|
+
* the brackets!) The text should be enclosed in the appropriate
|
185
|
+
* comment syntax for the file format. We also recommend that a
|
186
|
+
* file or class name and description of purpose be included on the
|
187
|
+
* same "printed page" as the copyright notice for easier
|
188
|
+
* identification within third-party archives.
|
189
|
+
*
|
190
|
+
* Copyright [yyyy] [name of copyright owner]
|
191
|
+
*
|
192
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
193
|
+
* you may not use this file except in compliance with the License.
|
194
|
+
* You may obtain a copy of the License at
|
195
|
+
*
|
196
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
197
|
+
*
|
198
|
+
* Unless required by applicable law or agreed to in writing, software
|
199
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
200
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
201
|
+
* See the License for the specific language governing permissions and
|
202
|
+
* limitations under the License.
|
203
|
+
*/
|
data/README.md
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# edis_client
|
2
|
+
|
3
|
+
The EDIS Data Webservice is a service provided by the United States International Trade Commission. For more information on either the USITC or EDIS please visit their respective websites ([www.usitc.gov](http://www.usitc.gov) or [edis.usitc.gov](http://edis.usitc.gov))
|
4
|
+
|
5
|
+
Documentation on the EDIS data webserivce can be found here
|
6
|
+
|
7
|
+
## Testing
|
8
|
+
Before running the tests via Rake you will need to add ./test/config.rb with a hash with the following keys
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
CREDS = { username: 'sean', password: 'password' }
|
12
|
+
```
|
13
|
+
|
14
|
+
## Contributing to edis_client
|
15
|
+
|
16
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
17
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
18
|
+
* Fork the project
|
19
|
+
* Start a feature/bugfix branch
|
20
|
+
* Commit and push until you are happy with your contribution
|
21
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
22
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
23
|
+
|
24
|
+
## Copyright
|
25
|
+
|
26
|
+
Copyright (c) 2011 The United States International Trade Commission. See LICENSE for
|
27
|
+
further details.
|
28
|
+
|
data/lib/edis_client.rb
ADDED
@@ -0,0 +1,345 @@
|
|
1
|
+
require 'base64'
|
2
|
+
require 'cgi'
|
3
|
+
require 'crack/xml'
|
4
|
+
require 'net/https'
|
5
|
+
require 'recursive_open_struct'
|
6
|
+
|
7
|
+
#
|
8
|
+
# Friendly little GEM for interacting with the United States International
|
9
|
+
# Trade Commission's EDIS data REST services.
|
10
|
+
#
|
11
|
+
module EDIS
|
12
|
+
class Client
|
13
|
+
#
|
14
|
+
# Construct a new instance. The following options are available:
|
15
|
+
#
|
16
|
+
# - :proxy_uri
|
17
|
+
# - :proxy_user
|
18
|
+
# - :proxy_pass
|
19
|
+
# - :timeout - Defaults to 10s
|
20
|
+
#
|
21
|
+
# == Example:
|
22
|
+
#
|
23
|
+
# 'require 'edis_client'
|
24
|
+
#
|
25
|
+
# edis = EDIS::Client.new({
|
26
|
+
# timeout: 5s,
|
27
|
+
# proxy: {
|
28
|
+
# uri: 'https://my.domain.com',
|
29
|
+
# user: 'matz',
|
30
|
+
# password: 'changeit'
|
31
|
+
# }
|
32
|
+
# })
|
33
|
+
#
|
34
|
+
def initialize(config = {})
|
35
|
+
config[:timeout] = 10 unless config[:timeout]
|
36
|
+
if config[:proxy]
|
37
|
+
config[:proxy][:uri] = URI.parse(config[:proxy][:uri])
|
38
|
+
end
|
39
|
+
|
40
|
+
@config = config
|
41
|
+
end
|
42
|
+
|
43
|
+
#
|
44
|
+
# Generates a digest for api usage. Users must be preregistered with
|
45
|
+
# the edis app. The digest can be retained (default) for the life of this
|
46
|
+
# instance (session), ensuring all subseqent api calls pass the digest to
|
47
|
+
# the endpoint. In this mode clients need not worry about retaining
|
48
|
+
# and passing this to other api calls.
|
49
|
+
#
|
50
|
+
# - :username - your EDIS registered username [REQUIRED]
|
51
|
+
# - :password - your EDIS registered password [REQUIRED]
|
52
|
+
# - :retain - retain your digest for subsequent api calls [Defaults to true]
|
53
|
+
#
|
54
|
+
def gen_digest(username, password, retain = true)
|
55
|
+
results = post_resource "/secretKey/#{username}", { password: password }
|
56
|
+
raise ArgumentError, results.errors if results.errors
|
57
|
+
secret_key = results.results.secretKey
|
58
|
+
digest = Base64.strict_encode64 "#{username}:#{secret_key}"
|
59
|
+
@config[:digest] = digest if retain
|
60
|
+
digest
|
61
|
+
end
|
62
|
+
|
63
|
+
#
|
64
|
+
# Find investigations. The following options are available:
|
65
|
+
#
|
66
|
+
# - :investigation_number - the investigation number.
|
67
|
+
# - :investigation_phase - the name of the investigation phase. :investgation_number is required when using this option
|
68
|
+
# - :investigation_type - the name of the investigation type
|
69
|
+
# - :investigation_status - the name of the investigation status
|
70
|
+
# - :page - the page number for result pagination.
|
71
|
+
# - :digest - the authorization digest returned from gen_digest
|
72
|
+
#
|
73
|
+
def find_investigations(options = {})
|
74
|
+
validate_investigation_options options
|
75
|
+
path = build_path '/investigation', options, investigation_paths
|
76
|
+
params = build_params options, investigation_params
|
77
|
+
get_resource path, params, options
|
78
|
+
end
|
79
|
+
|
80
|
+
#
|
81
|
+
# Fetch document metadata. The following options are available:
|
82
|
+
#
|
83
|
+
# - :document_id - the document id.
|
84
|
+
# - :security_level - the security level name.
|
85
|
+
# - :investigation_number - the investigation number.
|
86
|
+
# - :investigation_phase - the name of the investigation phase.
|
87
|
+
# - :document_type - the document type
|
88
|
+
# - :official_received_date - the document's official received date comparision. this should be a hash of the following keys:
|
89
|
+
# :type => :between, :before, :after or :exact
|
90
|
+
# when the type is :exact, :before, :after then
|
91
|
+
# the hash must also contain :date
|
92
|
+
# for :between the hash must contain the 2 following
|
93
|
+
# keys :from_date, :to_date
|
94
|
+
# - :modified_date - the docuemnt's last modified date comparision.
|
95
|
+
# this should be a hash of the following keys:
|
96
|
+
# :comparision_type => :between, :before, :after or :exact
|
97
|
+
# when the type is :exact, :before, :after then
|
98
|
+
# the hash must also contain :date
|
99
|
+
# for :between the hash must contain the 2 following
|
100
|
+
# keys :from_date, :to_date
|
101
|
+
# - :firm_org - the firm that filed the doc
|
102
|
+
# - :page - the page number for result pagination.
|
103
|
+
# - :digest - the authorization digest returned from gen_digest
|
104
|
+
#
|
105
|
+
def find_documents(options = {})
|
106
|
+
path = build_path '/document', options, [:document_id]
|
107
|
+
params = build_params(options, document_params).merge \
|
108
|
+
build_date_params options, document_date_params
|
109
|
+
get_resource path, options, params
|
110
|
+
end
|
111
|
+
|
112
|
+
#
|
113
|
+
# Fetch a document's attachments. Returns a hash rooted at the
|
114
|
+
# rest API's results node. Accepts an hash for the following options:
|
115
|
+
#
|
116
|
+
# - :document_id - the document id [REQUIRED]
|
117
|
+
# - :digest - The authorization digest returned from gen_digest
|
118
|
+
#
|
119
|
+
def find_attachments(options = {})
|
120
|
+
validate_presence_of [:document_id], options
|
121
|
+
get_resource "/attachment/#{options[:document_id]}", options
|
122
|
+
end
|
123
|
+
|
124
|
+
#
|
125
|
+
# Fetch a document. The result is streamed and therefore
|
126
|
+
# clients must provide a block to read each chunk of the
|
127
|
+
# response. The following options are available:
|
128
|
+
#
|
129
|
+
# - :document_id - the document id [REQUIRED]
|
130
|
+
# - :attachment_id - the actual attachment id [REQUIRED]
|
131
|
+
# - :digest - the authorization digest returned from gen_digest [REQUIRED]
|
132
|
+
#
|
133
|
+
def download_attachment(options = {})
|
134
|
+
raise ArgumentError, "Missing block." unless block_given?
|
135
|
+
validate_download options
|
136
|
+
path = build_path '/download', options, download_paths
|
137
|
+
stream_resource(path, options) { |chunk| yield chunk }
|
138
|
+
end
|
139
|
+
|
140
|
+
######################################################################################
|
141
|
+
private # :nodoc:
|
142
|
+
|
143
|
+
# document related
|
144
|
+
def document_params
|
145
|
+
[:page, :firm_org, :document_type, :security_level, :investigation_phase, :investigation_number]
|
146
|
+
end
|
147
|
+
def document_date_params
|
148
|
+
[:official_received_date, :modified_date]
|
149
|
+
end
|
150
|
+
|
151
|
+
# investigation related
|
152
|
+
def investigation_paths
|
153
|
+
[:investigation_number, :investigation_phase]
|
154
|
+
end
|
155
|
+
def investigation_params
|
156
|
+
[:page, :investigation_type, :investigation_status]
|
157
|
+
end
|
158
|
+
|
159
|
+
# download released
|
160
|
+
def download_paths
|
161
|
+
[:document_id, :attachment_id]
|
162
|
+
end
|
163
|
+
|
164
|
+
#
|
165
|
+
# Validates the requires are present in the options. Raises
|
166
|
+
# ArgumentError if not.
|
167
|
+
#
|
168
|
+
def validate_presence_of(requires, options, msg = nil)
|
169
|
+
requires.each do |required|
|
170
|
+
unless options[required]
|
171
|
+
raise ArgumentError, msg || "Missing one or more required options #{requires}"
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
#
|
177
|
+
# Validate that is digest is available and the required fields are present
|
178
|
+
#
|
179
|
+
def validate_download(options)
|
180
|
+
validate_digest options
|
181
|
+
validate_presence_of [:document_id, :attachment_id], options
|
182
|
+
end
|
183
|
+
|
184
|
+
#
|
185
|
+
# Validate either the digest is specified or that it is set in the env.
|
186
|
+
#
|
187
|
+
def validate_digest(options)
|
188
|
+
unless options[:digest] || @config[:digest]
|
189
|
+
raise ArgumentError, "A digest is required. Please use gen_digest."
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
#
|
194
|
+
# Validates that when :investigation_phase is
|
195
|
+
# specified so is :investigation_number
|
196
|
+
#
|
197
|
+
def validate_investigation_options(options)
|
198
|
+
if options[:investigation_phase]
|
199
|
+
validate_presence_of [:investigation_number], options,
|
200
|
+
":investigation_number is required when :investigation_phase is specified."
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
#
|
205
|
+
# Get the resource at the given path.
|
206
|
+
#
|
207
|
+
def get_resource(path, options, params = {})
|
208
|
+
connect.start do |http|
|
209
|
+
path = path_with_params(path, params) unless params.empty?
|
210
|
+
xml = http.get("/data/#{path}", header(options) || {}).body
|
211
|
+
RecursiveOpenStruct.new Crack::XML.parse(xml)
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
#
|
216
|
+
# Get the resource at the given path streaming the result, in chunks, to
|
217
|
+
# the block.
|
218
|
+
#
|
219
|
+
def stream_resource(path, options)
|
220
|
+
connect.start do |http|
|
221
|
+
http.get("/data/#{path}", header(options) || {}) do |chunk|
|
222
|
+
yield chunk
|
223
|
+
end
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
#
|
228
|
+
# Post resource.
|
229
|
+
#
|
230
|
+
def post_resource(path, params)
|
231
|
+
connect.start do |http|
|
232
|
+
req = Net::HTTP::Post.new("/data/#{path}") and req.set_form_data params
|
233
|
+
xml = http.request(req).body
|
234
|
+
RecursiveOpenStruct.new Crack::XML.parse(xml)
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
#
|
239
|
+
# Generate a query string reprsentation of the params and append to the path.
|
240
|
+
#
|
241
|
+
def path_with_params(path, params)
|
242
|
+
"#{path}?".concat \
|
243
|
+
params.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.reverse.join('&')
|
244
|
+
end
|
245
|
+
|
246
|
+
#
|
247
|
+
# Get the correct net:http class based on config
|
248
|
+
#
|
249
|
+
def connect
|
250
|
+
uri = URI.parse('https://edis.usitc.gov/')
|
251
|
+
http = net_http_class.new(uri.host, uri.port)
|
252
|
+
http.use_ssl = true
|
253
|
+
http.read_timeout = @config[:timeout] and http
|
254
|
+
end
|
255
|
+
|
256
|
+
#
|
257
|
+
# Creates the authorization header if the digest is present in the
|
258
|
+
# options or if it was specified as being retained when gen_disgest
|
259
|
+
# was called.
|
260
|
+
#
|
261
|
+
def header(options)
|
262
|
+
digest = if options[:digest]
|
263
|
+
options[:digest]
|
264
|
+
elsif @config[:digest]
|
265
|
+
@config[:digest]
|
266
|
+
else
|
267
|
+
false
|
268
|
+
end
|
269
|
+
{'Authorization' => "Basic #{digest}"} if digest
|
270
|
+
end
|
271
|
+
|
272
|
+
#
|
273
|
+
# Proxy connections?
|
274
|
+
#
|
275
|
+
def proxy?
|
276
|
+
@config.key? :proxy
|
277
|
+
end
|
278
|
+
|
279
|
+
#
|
280
|
+
# Get the correct class based on proxy settings.
|
281
|
+
#
|
282
|
+
def net_http_class
|
283
|
+
if proxy?
|
284
|
+
proxy = @config[:proxy]
|
285
|
+
Net::HTTP::Proxy(proxy[:uri].host, proxy[:uri].port, proxy[:user], proxy[:password])
|
286
|
+
else
|
287
|
+
Net::HTTP
|
288
|
+
end
|
289
|
+
end
|
290
|
+
|
291
|
+
#
|
292
|
+
# Builds a path with optional resources paths if specified.
|
293
|
+
#
|
294
|
+
def build_path(root, options, optional_resources)
|
295
|
+
optional_resources.inject(root) do |path, resource|
|
296
|
+
path << "/#{options[resource]}" if options[resource]
|
297
|
+
path
|
298
|
+
end
|
299
|
+
end
|
300
|
+
|
301
|
+
#
|
302
|
+
# Builds the params hash from the options.
|
303
|
+
#
|
304
|
+
def build_params(options, optional_params)
|
305
|
+
optional_params.inject({}) do |params, param|
|
306
|
+
params[camelize(param.to_s, false)] = options[param] if options[param]
|
307
|
+
params
|
308
|
+
end
|
309
|
+
end
|
310
|
+
|
311
|
+
#
|
312
|
+
# Appends to the params date comparisions.
|
313
|
+
#
|
314
|
+
def build_date_params(options, optional_date_params)
|
315
|
+
optional_date_params.inject({}) do |params, date_param|
|
316
|
+
if options[date_param]
|
317
|
+
comparison = options[date_param]
|
318
|
+
case comparison[:type]
|
319
|
+
when :between
|
320
|
+
params[camelize(date_param)] =
|
321
|
+
"#BETWEEN:#{date_param[:from_date]}:#{date_param[:to_date]}"
|
322
|
+
when :before, :after, :exact
|
323
|
+
params[camelize(date_param)] =
|
324
|
+
"#{comparison[:type].uppercase}:#{comparision[:date]}"
|
325
|
+
else
|
326
|
+
raise ArgumentError,
|
327
|
+
"Unknown comparison type #{comparison[:comparison_type]}"
|
328
|
+
end
|
329
|
+
end
|
330
|
+
params
|
331
|
+
end
|
332
|
+
end
|
333
|
+
|
334
|
+
#
|
335
|
+
# Camelize a string, lifted from Rails.
|
336
|
+
#
|
337
|
+
def camelize(s, first_letter_in_uppercase = true)
|
338
|
+
if first_letter_in_uppercase
|
339
|
+
s.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
|
340
|
+
else
|
341
|
+
s.to_s[0].chr.downcase + camelize(s)[1..-1]
|
342
|
+
end
|
343
|
+
end
|
344
|
+
end
|
345
|
+
end
|
metadata
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: edis_client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Sean McDaniel
|
9
|
+
- Carlos Fernandez
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2011-08-02 00:00:00.000000000 -04:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: crack
|
18
|
+
requirement: &2152510260 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ! '>='
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.1.8
|
24
|
+
type: :runtime
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: *2152510260
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: recursive-open-struct
|
29
|
+
requirement: &2152509780 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 0.2.1
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: *2152509780
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: rdoc
|
40
|
+
requirement: &2152509300 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
type: :runtime
|
47
|
+
prerelease: false
|
48
|
+
version_requirements: *2152509300
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: shoulda
|
51
|
+
requirement: &2152508820 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
type: :development
|
58
|
+
prerelease: false
|
59
|
+
version_requirements: *2152508820
|
60
|
+
- !ruby/object:Gem::Dependency
|
61
|
+
name: bundler
|
62
|
+
requirement: &2152508340 !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ~>
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: 1.0.0
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: *2152508340
|
71
|
+
- !ruby/object:Gem::Dependency
|
72
|
+
name: jeweler
|
73
|
+
requirement: &2152507860 !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ~>
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: 1.6.2
|
79
|
+
type: :development
|
80
|
+
prerelease: false
|
81
|
+
version_requirements: *2152507860
|
82
|
+
- !ruby/object:Gem::Dependency
|
83
|
+
name: rcov
|
84
|
+
requirement: &2152507380 !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ! '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: *2152507380
|
93
|
+
description: Provides easy access to the USITC's EDIS data web service (contains international
|
94
|
+
trade investigation data) via an object spewing ruby class - no fuss no muss and
|
95
|
+
definately no XML! This gem is not officially supported by the USITC. This gem is
|
96
|
+
pretty experimental and should see modifications in the near future.
|
97
|
+
email:
|
98
|
+
executables: []
|
99
|
+
extensions: []
|
100
|
+
extra_rdoc_files:
|
101
|
+
- LICENSE
|
102
|
+
- README.md
|
103
|
+
files:
|
104
|
+
- lib/edis_client.rb
|
105
|
+
- LICENSE
|
106
|
+
- README.md
|
107
|
+
has_rdoc: true
|
108
|
+
homepage: https://github.com/morologous/ruby-edis-data-client
|
109
|
+
licenses:
|
110
|
+
- Apache
|
111
|
+
post_install_message:
|
112
|
+
rdoc_options: []
|
113
|
+
require_paths:
|
114
|
+
- lib
|
115
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
116
|
+
none: false
|
117
|
+
requirements:
|
118
|
+
- - ! '>='
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
segments:
|
122
|
+
- 0
|
123
|
+
hash: 4158961942739418671
|
124
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
|
+
none: false
|
126
|
+
requirements:
|
127
|
+
- - ! '>='
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
requirements: []
|
131
|
+
rubyforge_project:
|
132
|
+
rubygems_version: 1.6.2
|
133
|
+
signing_key:
|
134
|
+
specification_version: 3
|
135
|
+
summary: Ruby client to the EDIS Data Webservice, a service provided by the United
|
136
|
+
States International Trade Commission.
|
137
|
+
test_files: []
|