shutterstock-client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/shutterstock-client.rb +111 -0
  2. metadata +222 -0
@@ -0,0 +1,111 @@
1
+ require 'httparty'
2
+ require 'active_support/all'
3
+ require 'singleton'
4
+
5
+ module ShutterstockAPI
6
+ require_relative './client/driver'
7
+ require_relative './client/configuration'
8
+ require_relative './client/customer'
9
+ require_relative './client/images'
10
+ require_relative './client/image'
11
+ require_relative './client/lightbox'
12
+
13
+ class Client
14
+ include HTTParty
15
+ include Singleton
16
+
17
+ # @return [Configuration] Config instance
18
+ attr_reader :config
19
+
20
+ # Request options
21
+ attr_accessor :options
22
+
23
+ # Creates a new {Client} instance and yields {#config}.
24
+ # Requires a block to be given.
25
+ def configure
26
+ raise ArgumentError, "block not given" unless block_given?
27
+
28
+ @config = Configuration.new
29
+ yield config
30
+
31
+ @config.api_url ||= "http://api.shutterstock.com"
32
+ raise ArgumentError, "API Username not provided" if config.api_username.nil?
33
+ raise ArgumentError, "API Key not provided" if config.api_key.nil?
34
+
35
+ @options = {
36
+ base_uri: config.api_url,
37
+ basic_auth: {
38
+ username: config.api_username,
39
+ password: config.api_key
40
+ }
41
+ }
42
+
43
+ if (config.username && config.password)
44
+ get_auth_token
45
+ end
46
+
47
+ @options.delete_if{|k, v| k == :body}
48
+
49
+ @options.merge!({
50
+ default_params: {
51
+ auth_token: config.auth_token
52
+ }
53
+ })
54
+ end
55
+
56
+ def configured?
57
+ ! config.nil?
58
+ end
59
+
60
+ def get_auth_token
61
+ options=@options
62
+ options[:body] = { :username => config.username, :password => config.password}
63
+ response = self.class.post( "#{config.api_url}/auth/customer.json", options )
64
+
65
+ if response.code == 200
66
+ config.auth_token = response["auth_token"]
67
+ else
68
+ raise RuntimeError, "Something went wrong: #{response.code} #{response.message}"
69
+ end
70
+ return config.auth_token
71
+ end
72
+
73
+ def method_missing(method, *args, &block)
74
+ method = method.to_s
75
+ options = args.last.is_a?(Hash) ? args.pop : {}
76
+
77
+ klass = self.class.modulize_string(method.singularize)
78
+
79
+ if ShutterstockAPI.const_defined?(klass)
80
+ klass_as_const = ShutterstockAPI.const_get(klass)
81
+ klass_as_const.new(options)
82
+ else
83
+ super
84
+ end
85
+ end
86
+
87
+ def customers
88
+ Customer.new({"username" => config.username})
89
+ end
90
+
91
+ # From https://github.com/rubyworks/facets/blob/master/lib/core/facets/string/modulize.rb
92
+ def self.modulize_string(string)
93
+ #gsub('__','/'). # why was this ever here?
94
+ string.gsub(/__(.?)/){ "::#{$1.upcase}" }.
95
+ gsub(/\/(.?)/){ "::#{$1.upcase}" }.
96
+ gsub(/(?:_+|-+)([a-z])/){ $1.upcase }.
97
+ gsub(/(\A|\s)([a-z])/){ $1 + $2.upcase }
98
+ end
99
+
100
+ # From https://github.com/rubyworks/facets/blob/master/lib/core/facets/string/snakecase.rb
101
+ def self.snakecase_string(string)
102
+ #gsub(/::/, '/').
103
+ string.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
104
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
105
+ tr('-', '_').
106
+ gsub(/\s/, '_').
107
+ gsub(/__+/, '_').
108
+ downcase
109
+ end
110
+ end
111
+ end
metadata ADDED
@@ -0,0 +1,222 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shutterstock-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - jhogue, vkajjam, flindiakos, wusher, alicht
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-02-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: httparty
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: activesupport
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: pry
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: vcr
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: webmock
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: rake
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: guard
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ - !ruby/object:Gem::Dependency
143
+ name: guard-rspec
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ - !ruby/object:Gem::Dependency
159
+ name: rb-readline
160
+ requirement: !ruby/object:Gem::Requirement
161
+ none: false
162
+ requirements:
163
+ - - ! '>='
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ type: :development
167
+ prerelease: false
168
+ version_requirements: !ruby/object:Gem::Requirement
169
+ none: false
170
+ requirements:
171
+ - - ! '>='
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ - !ruby/object:Gem::Dependency
175
+ name: rubocop
176
+ requirement: !ruby/object:Gem::Requirement
177
+ none: false
178
+ requirements:
179
+ - - ! '>='
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ type: :development
183
+ prerelease: false
184
+ version_requirements: !ruby/object:Gem::Requirement
185
+ none: false
186
+ requirements:
187
+ - - ! '>='
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
190
+ description: see summary
191
+ email: vkajjam@shutterstock.com
192
+ executables: []
193
+ extensions: []
194
+ extra_rdoc_files: []
195
+ files:
196
+ - lib/shutterstock-client.rb
197
+ homepage: https://github.com/shutterstock/ruby-shutterstock-api
198
+ licenses:
199
+ - Copyright shutterstock.com 2014
200
+ post_install_message:
201
+ rdoc_options: []
202
+ require_paths:
203
+ - lib
204
+ required_ruby_version: !ruby/object:Gem::Requirement
205
+ none: false
206
+ requirements:
207
+ - - ! '>='
208
+ - !ruby/object:Gem::Version
209
+ version: '0'
210
+ required_rubygems_version: !ruby/object:Gem::Requirement
211
+ none: false
212
+ requirements:
213
+ - - ! '>='
214
+ - !ruby/object:Gem::Version
215
+ version: '0'
216
+ requirements: []
217
+ rubyforge_project:
218
+ rubygems_version: 1.8.23
219
+ signing_key:
220
+ specification_version: 3
221
+ summary: A ruby client to work with shutterstock api
222
+ test_files: []