Class: ShutterstockAPI::Client
- Inherits:
-
Object
- Object
- ShutterstockAPI::Client
show all
- Includes:
- HTTParty, Singleton
- Defined in:
- lib/shutterstock-client.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/shutterstock-client.rb', line 73
def method_missing(method, *args, &block)
method = method.to_s
options = args.last.is_a?(Hash) ? args.pop : {}
klass = self.class.modulize_string(method.singularize)
if ShutterstockAPI.const_defined?(klass)
klass_as_const = ShutterstockAPI.const_get(klass)
klass_as_const.new(options)
else
super
end
end
|
Instance Attribute Details
#config ⇒ Configuration
18
19
20
|
# File 'lib/shutterstock-client.rb', line 18
def config
@config
end
|
#options ⇒ Object
21
22
23
|
# File 'lib/shutterstock-client.rb', line 21
def options
@options
end
|
Class Method Details
.modulize_string(string) ⇒ Object
92
93
94
95
96
97
98
|
# File 'lib/shutterstock-client.rb', line 92
def self.modulize_string(string)
string.gsub(/__(.?)/){ "::#{$1.upcase}" }.
gsub(/\/(.?)/){ "::#{$1.upcase}" }.
gsub(/(?:_+|-+)([a-z])/){ $1.upcase }.
gsub(/(\A|\s)([a-z])/){ $1 + $2.upcase }
end
|
.snakecase_string(string) ⇒ Object
101
102
103
104
105
106
107
108
109
|
# File 'lib/shutterstock-client.rb', line 101
def self.snakecase_string(string)
string.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
tr('-', '_').
gsub(/\s/, '_').
gsub(/__+/, '_').
downcase
end
|
Instance Method Details
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/shutterstock-client.rb', line 25
def configure
raise ArgumentError, "block not given" unless block_given?
@config = Configuration.new
yield config
@config.api_url ||= "http://api.shutterstock.com"
raise ArgumentError, "API Username not provided" if config.api_username.nil?
raise ArgumentError, "API Key not provided" if config.api_key.nil?
@options = {
base_uri: config.api_url,
basic_auth: {
username: config.api_username,
password: config.api_key
}
}
if (config.username && config.password)
get_auth_token
end
@options.delete_if{|k, v| k == :body}
@options.merge!({
default_params: {
auth_token: config.auth_token
}
})
end
|
56
57
58
|
# File 'lib/shutterstock-client.rb', line 56
def configured?
! config.nil?
end
|
#customers ⇒ Object
87
88
89
|
# File 'lib/shutterstock-client.rb', line 87
def customers
Customer.new({"username" => config.username})
end
|
#get_auth_token ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/shutterstock-client.rb', line 60
def get_auth_token
options=@options
options[:body] = { :username => config.username, :password => config.password}
response = self.class.post( "#{config.api_url}/auth/customer.json", options )
if response.code == 200
config.auth_token = response["auth_token"]
else
raise RuntimeError, "Something went wrong: #{response.code} #{response.message}"
end
return config.auth_token
end
|