Module: Concurrency

Defined in:
lib/concurrency.rb,
lib/concurrency/version.rb,
lib/concurrency/configuration.rb

Defined Under Namespace

Classes: Configuration

Constant Summary collapse

VERSION =
"0.1.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



8
9
10
# File 'lib/concurrency.rb', line 8

def configuration
  @configuration
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Yields:



19
20
21
# File 'lib/concurrency.rb', line 19

def self.configure
    yield(configuration)
end

.conversion_rate(from = Concurrency.configuration.from_currency, to = Concurrency.configuration.to_currency) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/concurrency.rb', line 33

def self.conversion_rate(from = Concurrency.configuration.from_currency, to = Concurrency.configuration.to_currency)
    if from == to
        return 1.0
    else
        rate = Concurrency.get_rate(from, to)
        return rate
    end
end

.convert(*args) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/concurrency.rb', line 23

def self.convert(*args)
    if args.length == 3
        Concurrency.convert_full(*args)
    elsif args.length == 2
        Concurrency.convert_full(args[0], Concurrency.configuration.from_currency, args[1])
    else
        Concurrency.convert_full(args[0], Concurrency.configuration.from_currency, Concurrency.configuration.to_currency)
    end
end

.convert_full(initial, from, to) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/concurrency.rb', line 44

def self.convert_full(initial, from, to)
    if from == to
        return initial
    end
    rate = Concurrency.get_rate(from, to)
    if rate == nil
        return nil
    else
        return initial*rate
    end
end

.get_rate(from, to) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/concurrency.rb', line 56

def self.get_rate(from, to)
    url = "http://api.fixer.io/latest?base=#{from}&symbols=#{to}"
    uri = URI(url)
    response = Net::HTTP.get(uri)
    if response == nil
        return nil
    else
        parsed_response = JSON.parse(response)
        rate = (parsed_response["rates"]["#{to}"]).to_f
        return rate
    end
end

.resetObject



15
16
17
# File 'lib/concurrency.rb', line 15

def self.reset
    @configuration = Configuration.new
end